♻️ unify exam status label mapping into examStatusMessageKey
Browse files at this commit57b96fff22aa0b02d09cd7ce5d66bff160eb59fcParent: d8a273c- refactor
- ♻️ unify exam status label mapping into examStatusMessageKey
5 changed files +63 -31
- src/components/exams/exam-card.tsx +2 -10
- src/lib/exam-status.test.ts +39 -0
- src/lib/exam-status.ts +14 -1
- src/pages/exam-detail-page.tsx +6 -11
- src/pages/exams-page.tsx +2 -9
src/components/exams/exam-card.tsxmodified+2 -10
| @@ -5,7 +5,7 @@ import { Badge } from "@/components/ui/badge"; | |||
| 5 | import { ExamLink } from "@/components/exams/exam-link"; | 5 | import { ExamLink } from "@/components/exams/exam-link"; |
| 6 | import { cn } from "@/lib/cn"; | 6 | import { cn } from "@/lib/cn"; |
| 7 | import { examKindLabel } from "@/lib/exam-labels"; | 7 | import { examKindLabel } from "@/lib/exam-labels"; |
| 8 | -import { examDisplayStatus, examStatusTone } from "@/lib/exam-status"; | 8 | +import { examDisplayStatus, examStatusMessageKey, examStatusTone } from "@/lib/exam-status"; |
| 9 | import { examWeight } from "@/lib/exam-weight"; | 9 | import { examWeight } from "@/lib/exam-weight"; |
| 10 | import { examDurationMs, formatDateTime, formatDurationMinutes } from "@/lib/format"; | 10 | import { examDurationMs, formatDateTime, formatDurationMinutes } from "@/lib/format"; |
| 11 | import { scheduleStatusClass, scheduleStatusDotClass } from "@/lib/schedule-status"; | 11 | import { scheduleStatusClass, scheduleStatusDotClass } from "@/lib/schedule-status"; |
| @@ -27,15 +27,7 @@ export function ExamCard(props: { exam: Exam; courseTitle?: string; now?: number | |||
| 27 | }; | 27 | }; |
| 28 | 28 | ||
| 29 | const statusLabel = () => { | 29 | const statusLabel = () => { |
| 30 | - const s = status(); | 30 | + return t(examStatusMessageKey(status())); |
| 31 | - if (s === "draft") return t("exams.draft"); | ||
| 32 | - if (s === "unscheduled") return t("exams.unscheduled"); | ||
| 33 | - if (s === "submitted") return t("attempt.submitted"); | ||
| 34 | - if (s === "expired") return t("attempt.expired"); | ||
| 35 | - if (s === "no_attempts_left") return t("attempt.noAttemptsLeft"); | ||
| 36 | - if (s === "finished") return t("exams.finished"); | ||
| 37 | - if (s === "upcoming") return t("exams.upcoming"); | ||
| 38 | - return t("exams.active"); | ||
| 39 | }; | 31 | }; |
| 40 | 32 | ||
| 41 | return ( | 33 | return ( |
src/lib/exam-status.test.tsadded+39 -0
| @@ -0,0 +1,39 @@ | |||
| 1 | +import type { Exam } from "@/api/client"; | ||
| 2 | +import { type ExamDisplayStatus, examDisplayStatus, examStatusMessageKey } from "./exam-status"; | ||
| 3 | + | ||
| 4 | +const exam = { | ||
| 5 | + id: "exam-1", | ||
| 6 | + course: "course-1", | ||
| 7 | + title: "Retake exam", | ||
| 8 | + description: "", | ||
| 9 | + kind: "quiz", | ||
| 10 | + mode: "sync", | ||
| 11 | + starts_at: 1, | ||
| 12 | + ends_at: 10_000, | ||
| 13 | + duration_ms: 10_000, | ||
| 14 | + max_attempts: 2, | ||
| 15 | + allow_rejoin: false, | ||
| 16 | + draft: false, | ||
| 17 | + creator: "teacher-1", | ||
| 18 | +} satisfies Exam; | ||
| 19 | + | ||
| 20 | +it("shows submitted after a completed attempt even when retakes remain", () => { | ||
| 21 | + expect(examDisplayStatus(exam, 5_000, { status: "submitted", attempts_used: 1, max_attempts: 2 })).toBe("submitted"); | ||
| 22 | +}); | ||
| 23 | + | ||
| 24 | +it("maps every exam display status to the intended message key", () => { | ||
| 25 | + const expected: Record<ExamDisplayStatus, string> = { | ||
| 26 | + draft: "exams.draft", | ||
| 27 | + unscheduled: "exams.unscheduled", | ||
| 28 | + upcoming: "exams.upcoming", | ||
| 29 | + active: "exams.active", | ||
| 30 | + finished: "exams.finished", | ||
| 31 | + submitted: "attempt.submitted", | ||
| 32 | + expired: "attempt.expired", | ||
| 33 | + no_attempts_left: "attempt.noAttemptsLeft", | ||
| 34 | + }; | ||
| 35 | + | ||
| 36 | + for (const status of Object.keys(expected) as ExamDisplayStatus[]) { | ||
| 37 | + expect(examStatusMessageKey(status)).toBe(expected[status]); | ||
| 38 | + } | ||
| 39 | +}); | ||
src/lib/exam-status.tsmodified+14 -1
| @@ -1,4 +1,5 @@ | |||
| 1 | import type { AttemptStatus, Exam } from "@/api/client"; | 1 | import type { AttemptStatus, Exam } from "@/api/client"; |
| 2 | +import type { MessageKey } from "@/i18n/messages"; | ||
| 2 | 3 | ||
| 3 | export type ExamAttemptSummary = { | 4 | export type ExamAttemptSummary = { |
| 4 | status: AttemptStatus | "not_started"; | 5 | status: AttemptStatus | "not_started"; |
| @@ -13,10 +14,11 @@ export function isSittableExam(exam: Exam): boolean { | |||
| 13 | } | 14 | } |
| 14 | 15 | ||
| 15 | export function examDisplayStatus(exam: Exam, now: number, attempt?: ExamAttemptSummary | null): ExamDisplayStatus { | 16 | export function examDisplayStatus(exam: Exam, now: number, attempt?: ExamAttemptSummary | null): ExamDisplayStatus { |
| 17 | + if (attempt?.status === "submitted" || attempt?.status === "expired") return attempt.status; | ||
| 18 | + | ||
| 16 | const noAttemptsLeft = attempt && attempt.status !== "in_progress" && attempt.max_attempts > 0 && attempt.attempts_used >= attempt.max_attempts; | 19 | const noAttemptsLeft = attempt && attempt.status !== "in_progress" && attempt.max_attempts > 0 && attempt.attempts_used >= attempt.max_attempts; |
| 17 | 20 | ||
| 18 | if (noAttemptsLeft) { | 21 | if (noAttemptsLeft) { |
| 19 | - if (attempt.status === "submitted" || attempt.status === "expired") return attempt.status; | ||
| 20 | return "no_attempts_left"; | 22 | return "no_attempts_left"; |
| 21 | } | 23 | } |
| 22 | 24 | ||
| @@ -33,3 +35,14 @@ export function examStatusTone(status: ExamDisplayStatus): string { | |||
| 33 | if (status === "expired" || status === "no_attempts_left") return "finished"; | 35 | if (status === "expired" || status === "no_attempts_left") return "finished"; |
| 34 | return status; | 36 | return status; |
| 35 | } | 37 | } |
| 38 | + | ||
| 39 | +export function examStatusMessageKey(status: ExamDisplayStatus): MessageKey { | ||
| 40 | + if (status === "submitted") return "attempt.submitted"; | ||
| 41 | + if (status === "expired") return "attempt.expired"; | ||
| 42 | + if (status === "no_attempts_left") return "attempt.noAttemptsLeft"; | ||
| 43 | + if (status === "draft") return "exams.draft"; | ||
| 44 | + if (status === "unscheduled") return "exams.unscheduled"; | ||
| 45 | + if (status === "finished") return "exams.finished"; | ||
| 46 | + if (status === "upcoming") return "exams.upcoming"; | ||
| 47 | + return "exams.active"; | ||
| 48 | +} | ||
src/pages/exam-detail-page.tsxmodified+6 -11
| @@ -36,7 +36,7 @@ import { TableRowActions } from "@/components/ui/table-row-actions"; | |||
| 36 | import { hasMinRole } from "@/lib/roles"; | 36 | import { hasMinRole } from "@/lib/roles"; |
| 37 | import { createNow } from "@/lib/create-now"; | 37 | import { createNow } from "@/lib/create-now"; |
| 38 | import { examKindLabel } from "@/lib/exam-labels"; | 38 | import { examKindLabel } from "@/lib/exam-labels"; |
| 39 | -import { examDisplayStatus, examStatusTone, isSittableExam, type ExamAttemptSummary, type ExamDisplayStatus } from "@/lib/exam-status"; | 39 | +import { examDisplayStatus, examStatusMessageKey, examStatusTone, isSittableExam, type ExamAttemptSummary, type ExamDisplayStatus } from "@/lib/exam-status"; |
| 40 | import { examDurationMs, formatDateTime, formatDurationMinutes } from "@/lib/format"; | 40 | import { examDurationMs, formatDateTime, formatDurationMinutes } from "@/lib/format"; |
| 41 | import { personId, personLabel, personLabelWithId } from "@/lib/person"; | 41 | import { personId, personLabel, personLabelWithId } from "@/lib/person"; |
| 42 | import { cn } from "@/lib/cn"; | 42 | import { cn } from "@/lib/cn"; |
| @@ -187,18 +187,13 @@ function ExamDetailContent() { | |||
| 187 | const attempt = ownAttempt(); | 187 | const attempt = ownAttempt(); |
| 188 | return attempt ? { status: attempt.status, attempts_used: attempt.attempts_used, max_attempts: attempt.max_attempts } : null; | 188 | return attempt ? { status: attempt.status, attempts_used: attempt.attempts_used, max_attempts: attempt.max_attempts } : null; |
| 189 | }; | 189 | }; |
| 190 | - const noAttemptsLeft = () => detailStatus() === "no_attempts_left"; | 190 | + const noAttemptsLeft = () => { |
| 191 | + const attempt = ownAttemptSummary(); | ||
| 192 | + return detailStatus() === "no_attempts_left" || !!attempt && attempt.status !== "in_progress" && attempt.max_attempts > 0 && attempt.attempts_used >= attempt.max_attempts; | ||
| 193 | + }; | ||
| 191 | const detailStatus = (): ExamDisplayStatus => exam() ? examDisplayStatus(exam()!, now(), ownAttemptSummary()) : "unscheduled"; | 194 | const detailStatus = (): ExamDisplayStatus => exam() ? examDisplayStatus(exam()!, now(), ownAttemptSummary()) : "unscheduled"; |
| 192 | const detailStatusLabel = () => { | 195 | const detailStatusLabel = () => { |
| 193 | - const status = detailStatus(); | 196 | + return t(examStatusMessageKey(detailStatus())); |
| 194 | - if (status === "submitted") return t("attempt.submitted"); | ||
| 195 | - if (status === "expired") return t("attempt.expired"); | ||
| 196 | - if (status === "no_attempts_left") return t("attempt.noAttemptsLeft"); | ||
| 197 | - if (status === "draft") return t("exams.draft"); | ||
| 198 | - if (status === "unscheduled") return t("exams.unscheduled"); | ||
| 199 | - if (status === "finished") return t("exams.finished"); | ||
| 200 | - if (status === "upcoming") return t("exams.upcoming"); | ||
| 201 | - return t("exams.active"); | ||
| 202 | }; | 197 | }; |
| 203 | 198 | ||
| 204 | const canManage = () => { | 199 | const canManage = () => { |
src/pages/exams-page.tsxmodified+2 -9
| @@ -23,7 +23,7 @@ import { SidePanel } from "@/components/ui/side-panel"; | |||
| 23 | import { TableRowActions } from "@/components/ui/table-row-actions"; | 23 | import { TableRowActions } from "@/components/ui/table-row-actions"; |
| 24 | import { createNow } from "@/lib/create-now"; | 24 | import { createNow } from "@/lib/create-now"; |
| 25 | import { examKindLabel } from "@/lib/exam-labels"; | 25 | import { examKindLabel } from "@/lib/exam-labels"; |
| 26 | -import { examDisplayStatus, examStatusTone, type ExamDisplayStatus } from "@/lib/exam-status"; | 26 | +import { examDisplayStatus, examStatusMessageKey, examStatusTone, type ExamDisplayStatus } from "@/lib/exam-status"; |
| 27 | import { createFlash } from "@/lib/flash"; | 27 | import { createFlash } from "@/lib/flash"; |
| 28 | import { formatDateTime } from "@/lib/format"; | 28 | import { formatDateTime } from "@/lib/format"; |
| 29 | import { hasMinRole } from "@/lib/roles"; | 29 | import { hasMinRole } from "@/lib/roles"; |
| @@ -135,14 +135,7 @@ function ExamsContent() { | |||
| 135 | }; | 135 | }; |
| 136 | 136 | ||
| 137 | const statusLabel = (status: ExamDisplayStatus) => { | 137 | const statusLabel = (status: ExamDisplayStatus) => { |
| 138 | - if (status === "submitted") return t("attempt.submitted"); | 138 | + return t(examStatusMessageKey(status)); |
| 139 | - if (status === "expired") return t("attempt.expired"); | ||
| 140 | - if (status === "no_attempts_left") return t("attempt.noAttemptsLeft"); | ||
| 141 | - if (status === "draft") return t("exams.draft"); | ||
| 142 | - if (status === "unscheduled") return t("exams.unscheduled"); | ||
| 143 | - if (status === "finished") return t("exams.finished"); | ||
| 144 | - if (status === "upcoming") return t("exams.upcoming"); | ||
| 145 | - return t("exams.active"); | ||
| 146 | }; | 139 | }; |
| 147 | const rows = (): ExamRow[] => filterExams(list() ?? []).map((exam) => ({ ...exam, displayStatus: examStatus(exam) })); | 140 | const rows = (): ExamRow[] => filterExams(list() ?? []).map((exam) => ({ ...exam, displayStatus: examStatus(exam) })); |
| 148 | const columns = createMemo<ColumnDef<ExamRow>[]>(() => [ | 141 | const columns = createMemo<ColumnDef<ExamRow>[]>(() => [ |