Skip to content
This repository has been archived by the owner on Jan 17, 2025. It is now read-only.

refactor: checkAssignmentDeadline #525

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 97 additions & 110 deletions src/methods/checkAssignmentDeadline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Copy link

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.

ths.find(
(th) => th.innerText === trans[lang].status && th.nextElementSibling
)?.nextElementSibling as HTMLElement
)?.innerText.includes(trans[lang].notSubmitted)

const deadlineString = (
Copy link

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.

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 = (
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function createMessageOnDiff has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.

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