-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
卒業証書のPDFファイルをアップロードできる機能を追加 #8190
base: main
Are you sure you want to change the base?
Changes from 9 commits
496b8ec
8385b8a
3d8dc4e
f3bc5cc
d16a661
fd1d8db
b2ff53d
3aeb25a
6b5d653
9b55b38
7d82332
97b19ed
ba692b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,45 @@ function extractField(elements) { | |
} | ||
} | ||
|
||
function initializePdfUploadField() { | ||
const uploadField = document.getElementById('js-pdf-upload-field') | ||
if (!uploadField) return | ||
|
||
const removeButton = document.getElementById('js-remove-pdf-button') | ||
const fileLink = document.getElementById('js-pdf-file-link') | ||
const removeFlag = document.getElementById('js-remove-pdf-flag') | ||
const fileNameDisplay = document.getElementById('js-pdf-name') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 提案です! |
||
const fileInput = uploadField.querySelector('input[type="file"]') | ||
|
||
const updateFileNameDisplay = (name = '') => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 提案です! |
||
if (name) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こんな感じで書けそうです🙆 fileNameDisplay.textContent = name
const displayedStatus = name ? 'block' : 'none'
fileNameDisplay.style.display = displayedStatus There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 指摘された点とfileInputのイベントリスナーのロジックもリファクタリングしました。 |
||
fileNameDisplay.textContent = name | ||
fileNameDisplay.style.display = 'block' | ||
} else { | ||
fileNameDisplay.textContent = '' | ||
fileNameDisplay.style.display = 'none' | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 今回実装する部分を関数にして↑の DOMContentLoadedに入れてはどうでしょうか? あと画面の読み込み時に実行していますが、該当する画面でない場合のreturnがなさそうです。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 変更しました |
||
removeButton.addEventListener('click', () => { | ||
if (fileLink) fileLink.style.display = 'none' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ファイルがアップロードされている時のみ削除ボタンが表示されていれば、if文は不要になるかもです There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fileLinkはHTMLが生成されない場合があるのでエラーになるようです。 |
||
uploadField.style.display = 'flex' | ||
fileInput.value = '' | ||
removeFlag.value = '1' | ||
updateFileNameDisplay() | ||
}) | ||
|
||
fileInput.addEventListener('change', () => { | ||
if (fileInput.files && fileInput.files[0]) { | ||
const fileName = fileInput.files[0].name | ||
updateFileNameDisplay(fileName) | ||
removeFlag.value = '0' | ||
} else { | ||
updateFileNameDisplay() | ||
} | ||
}) | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
const ref = document.querySelector('#reference_books') | ||
if (ref) { | ||
|
@@ -84,4 +123,5 @@ document.addEventListener('DOMContentLoaded', () => { | |
}) | ||
} | ||
initializeFileInput(document) | ||
initializePdfUploadField() | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,48 @@ | ||
.a-file-input | ||
label | ||
height: 10rem | ||
max-width: 100% | ||
margin-inline: auto | ||
border: solid 1px var(--input-border) | ||
background-color: var(--base) | ||
display: flex | ||
align-items: center | ||
justify-content: center | ||
+position(relative) | ||
overflow: hidden | ||
cursor: pointer | ||
border-radius: .25rem | ||
padding: .5rem .5rem 2.75rem .5rem | ||
&.is-thumbnail | ||
label | ||
height: 15rem | ||
img | ||
max-height: 100% | ||
transition: all .2s ease-out | ||
&:hover | ||
opacity: .6 | ||
&.is-square img | ||
+size(7rem) | ||
object-fit: cover | ||
&.is-book img | ||
max-height: 100% | ||
max-width: 100% | ||
object-fit: contain | ||
border: solid 1px var(--border-tint) | ||
.a-pdf-input | ||
display: flex | ||
gap: .75rem | ||
+media-breakpoint-down(sm) | ||
flex-direction: column | ||
|
||
.a-pdf-input__inner | ||
flex: 1 | ||
background-color: var(--base) | ||
display: flex | ||
align-items: center | ||
justify-content: center | ||
+position(relative) | ||
overflow: hidden | ||
cursor: pointer | ||
border-radius: .25rem | ||
height: 2.25rem | ||
input | ||
overflow: hidden | ||
+size(0) | ||
+position(absolute, left 0, top 0) | ||
opacity: 0 | ||
p | ||
+size(100% 2.25rem) | ||
background-color: var(--base) | ||
+position(absolute, left 0, bottom 0, right 0) | ||
+text-block(.8125rem 1, flex 600) | ||
justify-content: center | ||
align-items: center | ||
border-top: solid 1px var(--input-border) | ||
transition: all .2s ease-out | ||
&:hover | ||
background-color: #e8e8e8 | ||
|
||
a.a-pdf-input__inner | ||
text-decoration: none | ||
color: var(--default-text) | ||
|
||
.a-pdf-input__file | ||
padding: .5rem | ||
flex: 1 | ||
border-radius: .25rem 0 0 .25rem | ||
border: solid 1px var(--input-border) | ||
border-right: none | ||
height: 2.25rem | ||
|
||
.a-pdf-input__file-name | ||
font-size: .875rem | ||
white-space: nowrap | ||
overflow: hidden | ||
text-overflow: ellipsis | ||
display: block | ||
+media-breakpoint-up(md) | ||
max-width: 18rem | ||
+media-breakpoint-down(sm) | ||
max-width: 8rem | ||
|
||
.a-pdf-input__upload.a-button | ||
border-radius: 0 .25rem .25rem 0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
.a-pdf-input | ||
display: flex | ||
gap: .75rem | ||
+media-breakpoint-down(sm) | ||
flex-direction: column | ||
|
||
.a-pdf-input__inner | ||
flex: 1 | ||
background-color: var(--base) | ||
display: flex | ||
align-items: center | ||
justify-content: center | ||
+position(relative) | ||
overflow: hidden | ||
cursor: pointer | ||
border-radius: .25rem | ||
height: 2.25rem | ||
input | ||
overflow: hidden | ||
+size(0) | ||
+position(absolute, left 0, top 0) | ||
opacity: 0 | ||
|
||
a.a-pdf-input__inner | ||
text-decoration: none | ||
color: var(--default-text) | ||
|
||
.a-pdf-input__file | ||
padding: .5rem | ||
flex: 1 | ||
border-radius: .25rem 0 0 .25rem | ||
border: solid 1px var(--input-border) | ||
border-right: none | ||
height: 2.25rem | ||
|
||
.a-pdf-input__file-name | ||
font-size: .875rem | ||
white-space: nowrap | ||
overflow: hidden | ||
text-overflow: ellipsis | ||
display: block | ||
+media-breakpoint-up(md) | ||
max-width: 18rem | ||
+media-breakpoint-down(sm) | ||
max-width: 8rem | ||
|
||
.a-pdf-input__upload.a-button | ||
border-radius: 0 .25rem .25rem 0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,26 @@ | |
.form-item-block__item | ||
= render 'users/form/graduate', f: f | ||
|
||
.form-item | ||
= f.hidden_field :remove_diploma, value: '0', id: 'js-remove-pdf-flag' | ||
= f.label :diploma_file, class: 'a-form-label' | ||
.a-pdf-input | ||
- if @user.diploma_file.attached? | ||
= link_to url_for(@user.diploma_file), class: 'a-pdf-input__inner', id: 'js-pdf-file-link', target: '_blank', rel: 'noopener' do | ||
.a-pdf-input__file | ||
span.a-pdf-input__file-name | ||
= @user.diploma_file.filename | ||
.a-pdf-input__upload.a-button.is-md.is-secondary | ||
| PDFを確認 | ||
- display_style = @user.diploma_file.attached? ? 'display: none;' : 'display: flex;' | ||
label.a-pdf-input__inner#js-pdf-upload-field(style="#{display_style}") | ||
= f.file_field :diploma_file | ||
.a-pdf-input__file | ||
span.a-pdf-input__file-name#js-pdf-name | ||
.a-pdf-input__upload.a-button.is-md.is-secondary | ||
| PDFを選択 | ||
= button_tag '削除', type: 'button', class: 'a-button is-md is-secondary', id: 'js-remove-pdf-button' | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. button_tagが使えそうです! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 変更しました There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ファイルがアップロードされていない状態で削除ボタンがあるのはおかしい気がします🤔 |
||
.form-item-block | ||
.form-item-block__inner | ||
header.form-item-block__header | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="ja"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>卒業証書</title> | ||
</head> | ||
<body> | ||
<h1>卒業証書</h1> | ||
</body> | ||
</html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
提案です!
initializeDiplomaUploadField
の方がいいかも?