This repository has been archived by the owner on Jan 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
refactor: checkAssignmentDeadline
#525
Open
yudukikun5120
wants to merge
2
commits into
main
Choose a base branch
from
refactor/checkAssignmentDeadline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,130 +9,117 @@ import evalDiff from "./evalDiff" | |
dayjs.extend(customParseFormat) | ||
|
||
const checkAssignmentDeadline = (): void => { | ||
let notSubmitted = false | ||
let deadlineString = "" | ||
let deadlineTh: HTMLElement | ||
const lang = checkLang() | ||
|
||
const ths = Array.from( | ||
document.querySelectorAll(".stdlist th") | ||
) as HTMLElement[] | ||
for (const th of ths) { | ||
if (th.innerText === "状態" || th.innerText === "Status") { | ||
if (th.nextElementSibling) { | ||
const innerText = (th.nextElementSibling as HTMLElement).innerText | ||
if ( | ||
innerText.includes("提出していません") || | ||
innerText.includes("Not submitted") | ||
) { | ||
notSubmitted = true | ||
} | ||
} | ||
} | ||
if (th.innerText === "受付終了日時" || th.innerText === "End") { | ||
if (th.nextElementSibling) { | ||
deadlineString = (th.nextElementSibling as HTMLElement).innerText | ||
deadlineTh = th | ||
} | ||
} | ||
} | ||
|
||
const validateDeadlineString = (string: string) => { | ||
const match = new RegExp("(\\d{4}-+\\d{2}-+\\d{2} \\d{2}:+\\d{2})", "g") | ||
return match.test(string) | ||
} | ||
const notSubmitted = ( | ||
ths.find( | ||
(th) => th.innerText === trans[lang].status && th.nextElementSibling | ||
)?.nextElementSibling as HTMLElement | ||
)?.innerText.includes(trans[lang].notSubmitted) | ||
|
||
const deadlineString = ( | ||
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. Similar blocks of code found in 2 locations. Consider refactoring. |
||
ths.find( | ||
(th) => th.innerText === trans[lang].deadline && th.nextElementSibling | ||
)?.nextElementSibling as HTMLElement | ||
)?.innerText | ||
|
||
const deadlineTh = ths.find( | ||
(th) => th.innerText === trans[lang].deadline && th.nextElementSibling | ||
) | ||
if (!deadlineTh) return | ||
|
||
if (notSubmitted && validateDeadlineString(deadlineString)) { | ||
const now = dayjs() | ||
const deadline = dayjs(deadlineString, "YYYY-MM-DD HH:mm") | ||
|
||
const lang = checkLang() | ||
|
||
const createMessage = ( | ||
text: string, | ||
msgStatus: "normal" | "caution" | "danger" | ||
) => { | ||
const message = document.createElement("span") | ||
message.innerText = text | ||
message.style.marginLeft = "1em" | ||
message.style.padding = ".2em .5em" | ||
if (msgStatus === "normal") { | ||
message.style.backgroundColor = "#d3ebd3" | ||
message.style.color = "#244f24" | ||
} else if (msgStatus === "caution") { | ||
message.style.backgroundColor = "#fff4d1" | ||
message.style.color = "#433200" | ||
} else if (msgStatus === "danger") { | ||
message.style.backgroundColor = "#ffdce0" | ||
message.style.color = "#5d000b" | ||
} | ||
if (deadlineTh && deadlineTh.nextElementSibling) { | ||
deadlineTh.nextElementSibling.appendChild(message) | ||
} | ||
} | ||
|
||
const diff = evalDiff(now, deadline) | ||
if (!diff.unit) return | ||
|
||
if (diff.value > 0) { | ||
switch (diff.unit) { | ||
case "day": | ||
switch (lang) { | ||
case "ja": | ||
createMessage( | ||
`あと${diff.value}日`, | ||
diff.value > 2 ? "normal" : "caution" | ||
) | ||
break | ||
case "en": | ||
createMessage( | ||
diff.value > 1 | ||
? `${diff.value} days remaining` | ||
: `${diff.value} day remaining`, | ||
diff.value > 2 ? "normal" : "caution" | ||
) | ||
break | ||
} | ||
break | ||
case "hour": | ||
switch (lang) { | ||
case "ja": | ||
createMessage(`あと${diff.value}時間`, "danger") | ||
break | ||
case "en": | ||
createMessage( | ||
diff.value > 1 | ||
? `${diff.value} hours remaining` | ||
: `${diff.value} hour remaining`, | ||
"danger" | ||
) | ||
break | ||
} | ||
break | ||
case "minute": | ||
switch (lang) { | ||
case "ja": | ||
createMessage(`あと${diff.value}分`, "danger") | ||
break | ||
case "en": | ||
createMessage( | ||
diff.value > 1 | ||
? `${diff.value} minutes remaining` | ||
: `${diff.value} minute remaining`, | ||
"danger" | ||
) | ||
break | ||
} | ||
} | ||
} else { | ||
switch (lang) { | ||
case "ja": | ||
createMessage("受付終了", "danger") | ||
break | ||
case "en": | ||
createMessage("Deadline is over", "danger") | ||
break | ||
} | ||
} | ||
createMessageOnDiff(diff, lang, deadlineTh) | ||
} | ||
} | ||
|
||
const trans = { | ||
ja: { | ||
status: "状態", | ||
notSubmitted: "提出していません", | ||
deadline: "受付終了日時", | ||
day: "日", | ||
hour: "時間", | ||
minute: "分", | ||
remaining: (number: number, unit: "day" | "hour" | "minute") => | ||
`あと${number}${trans.ja[unit]}`, | ||
deadlineOver: "受付終了", | ||
}, | ||
en: { | ||
status: "Status", | ||
notSubmitted: "Not submitted", | ||
deadline: "End", | ||
day: "day", | ||
hour: "hour", | ||
minute: "minute", | ||
remaining: (number: number, unit: "day" | "hour" | "minute") => | ||
number > 1 | ||
? `${number} ${trans.en[unit]}s remaining` | ||
: `${number} ${trans.en[unit]} remaining`, | ||
deadlineOver: "Deadline is over", | ||
}, | ||
} | ||
|
||
const messageStyle = { | ||
normal: { | ||
backgroundColor: "#d3ebd3", | ||
color: "#244f24", | ||
}, | ||
caution: { | ||
backgroundColor: "#fff4d1", | ||
color: "#433200", | ||
}, | ||
danger: { | ||
backgroundColor: "#ffdce0", | ||
color: "#5d000b", | ||
}, | ||
} | ||
|
||
const validateDeadlineString = (string: string) => | ||
new RegExp("(\\d{4}-+\\d{2}-+\\d{2} \\d{2}:+\\d{2})", "g").test(string) | ||
|
||
const createMessage = ( | ||
text: string, | ||
msgStatus: "normal" | "caution" | "danger", | ||
deadlineTh: HTMLElement | ||
) => { | ||
const message = document.createElement("span") | ||
message.innerText = text | ||
message.style.marginLeft = "1em" | ||
message.style.padding = ".2em .5em" | ||
message.style.backgroundColor = messageStyle[msgStatus].backgroundColor | ||
message.style.color = messageStyle[msgStatus].color | ||
if (deadlineTh && deadlineTh.nextElementSibling) | ||
deadlineTh.nextElementSibling.appendChild(message) | ||
} | ||
|
||
const createMessageOnDiff = ( | ||
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. Function |
||
diff: ReturnType<typeof evalDiff>, | ||
lang: checkLang.langCode, | ||
deadlineTh: HTMLElement | ||
) => | ||
diff.unit && | ||
(diff.value > 0 | ||
? createMessage( | ||
trans[lang].remaining(diff.value, diff.unit), | ||
diff.unit === "day" | ||
? diff.value > 2 | ||
? "normal" | ||
: "caution" | ||
: "danger", | ||
deadlineTh | ||
) | ||
: createMessage(trans[lang].deadlineOver, "danger", deadlineTh)) | ||
|
||
export default checkAssignmentDeadline |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Similar blocks of code found in 2 locations. Consider refactoring.