🐛 exam status colors and polling, add submitted/expired filters
Browse files at this commitcae0fc497304cb574b93c39c06278eecaaa73798Parent: af20b6a- fix
- 🐛 exam status colors and polling, add submitted/expired filters
3 changed files +58 -18
- src/lib/schedule-status.ts +7 -2
- src/pages/exam-detail-page.tsx +4 -4
- src/pages/exams-page.tsx +47 -12
src/lib/schedule-status.tsmodified+7 -2
| @@ -6,10 +6,14 @@ export type ScheduleStatus = | |||
| 6 | | "soon" | 6 | | "soon" |
| 7 | | "finished" | 7 | | "finished" |
| 8 | | "past" | 8 | | "past" |
| 9 | - | "unscheduled"; | 9 | + | "unscheduled" |
| 10 | + | "submitted"; | ||
| 10 | 11 | ||
| 11 | export function scheduleStatusClass(status: ScheduleStatus | string): string { | 12 | export function scheduleStatusClass(status: ScheduleStatus | string): string { |
| 12 | if (status === "active") { | 13 | if (status === "active") { |
| 14 | + return "border-sky-500/30 bg-sky-500/10 text-sky-700 dark:text-sky-300"; | ||
| 15 | + } | ||
| 16 | + if (status === "submitted") { | ||
| 13 | return "border-emerald-500/30 bg-emerald-500/10 text-emerald-700 dark:text-emerald-300"; | 17 | return "border-emerald-500/30 bg-emerald-500/10 text-emerald-700 dark:text-emerald-300"; |
| 14 | } | 18 | } |
| 15 | if (status === "today" || status === "upcoming" || status === "soon") { | 19 | if (status === "today" || status === "upcoming" || status === "soon") { |
| @@ -22,7 +26,8 @@ export function scheduleStatusClass(status: ScheduleStatus | string): string { | |||
| 22 | } | 26 | } |
| 23 | 27 | ||
| 24 | export function scheduleStatusDotClass(status: ScheduleStatus | string): string { | 28 | export function scheduleStatusDotClass(status: ScheduleStatus | string): string { |
| 25 | - if (status === "active") return "bg-emerald-600"; | 29 | + if (status === "active") return "bg-sky-600"; |
| 30 | + if (status === "submitted") return "bg-emerald-600"; | ||
| 26 | if (status === "today" || status === "upcoming" || status === "soon") return "bg-amber-600"; | 31 | if (status === "today" || status === "upcoming" || status === "soon") return "bg-amber-600"; |
| 27 | if (status === "finished" || status === "past") return "bg-rose-600"; | 32 | if (status === "finished" || status === "past") return "bg-rose-600"; |
| 28 | return "bg-muted-foreground"; | 33 | return "bg-muted-foreground"; |
src/pages/exam-detail-page.tsxmodified+4 -4
| @@ -92,6 +92,8 @@ function ExamDetailContent() { | |||
| 92 | questions: false, | 92 | questions: false, |
| 93 | results: false, | 93 | results: false, |
| 94 | }); | 94 | }); |
| 95 | + const isSittable = () => exam()?.mode === "sync" || exam()?.mode === "async" || exam()?.mode === "open"; | ||
| 96 | + const isScheduled = () => isSittable(); | ||
| 95 | 97 | ||
| 96 | const [ownResult] = createResource( | 98 | const [ownResult] = createResource( |
| 97 | () => (isStudent() ? id() : null), | 99 | () => (isStudent() ? id() : null), |
| @@ -191,8 +193,6 @@ function ExamDetailContent() { | |||
| 191 | if (mode === "open") return t("exams.mode.open"); | 193 | if (mode === "open") return t("exams.mode.open"); |
| 192 | return t("exams.unscheduled"); | 194 | return t("exams.unscheduled"); |
| 193 | }; | 195 | }; |
| 194 | - const isSittable = () => exam()?.mode === "sync" || exam()?.mode === "async" || exam()?.mode === "open"; | ||
| 195 | - const isScheduled = () => isSittable(); | ||
| 196 | const isDraft = () => exam()?.draft === true; | 196 | const isDraft = () => exam()?.draft === true; |
| 197 | const gradeStudents = () => { | 197 | const gradeStudents = () => { |
| 198 | const graded = new Set((results() ?? []).map((row) => personId(row.user))); | 198 | const graded = new Set((results() ?? []).map((row) => personId(row.user))); |
| @@ -347,10 +347,10 @@ function ExamDetailContent() { | |||
| 347 | variant="outline" | 347 | variant="outline" |
| 348 | class={cn( | 348 | class={cn( |
| 349 | "mt-2 w-fit rounded-sm capitalize", | 349 | "mt-2 w-fit rounded-sm capitalize", |
| 350 | - scheduleStatusClass(detailStatus() === "submitted" || detailStatus() === "expired" ? "finished" : detailStatus()), | 350 | + scheduleStatusClass(detailStatus() === "expired" ? "finished" : detailStatus()), |
| 351 | )} | 351 | )} |
| 352 | > | 352 | > |
| 353 | - <span class={cn("mr-1.5 inline-block h-1.5 w-1.5 rounded-full", scheduleStatusDotClass(detailStatus() === "submitted" || detailStatus() === "expired" ? "finished" : detailStatus()))} /> | 353 | + <span class={cn("mr-1.5 inline-block h-1.5 w-1.5 rounded-full", scheduleStatusDotClass(detailStatus() === "expired" ? "finished" : detailStatus()))} /> |
| 354 | {detailStatusLabel()} | 354 | {detailStatusLabel()} |
| 355 | </Badge> | 355 | </Badge> |
| 356 | </div> | 356 | </div> |
src/pages/exams-page.tsxmodified+47 -12
| @@ -1,13 +1,14 @@ | |||
| 1 | -import { For, Show, Suspense, createEffect, createMemo, createResource, createSignal } from "solid-js"; | 1 | +import { For, Show, Suspense, createEffect, createMemo, createResource, createSignal, onCleanup } from "solid-js"; |
| 2 | import type { ColumnDef } from "@tanstack/solid-table"; | 2 | import type { ColumnDef } from "@tanstack/solid-table"; |
| 3 | import { useNavigate } from "@tanstack/solid-router"; | 3 | import { useNavigate } from "@tanstack/solid-router"; |
| 4 | import { getCourses } from "@/api/getCourses"; | 4 | import { getCourses } from "@/api/getCourses"; |
| 5 | +import { getExamAttempt } from "@/api/getExamAttempt"; | ||
| 5 | import { getExams } from "@/api/getExams"; | 6 | import { getExams } from "@/api/getExams"; |
| 6 | import { getMyCourses } from "@/api/getMyCourses"; | 7 | import { getMyCourses } from "@/api/getMyCourses"; |
| 7 | import { patchExamById } from "@/api/patchExamById"; | 8 | import { patchExamById } from "@/api/patchExamById"; |
| 8 | import { postCourseExam } from "@/api/postCourseExam"; | 9 | import { postCourseExam } from "@/api/postCourseExam"; |
| 9 | import { formatApiError } from "@/api/client"; | 10 | import { formatApiError } from "@/api/client"; |
| 10 | -import type { Course, Exam } from "@/api/types"; | 11 | +import type { AttemptStatus, Course, Exam } from "@/api/types"; |
| 11 | import { ExamForm, type ExamFormValues } from "@/components/exams/exam-form"; | 12 | import { ExamForm, type ExamFormValues } from "@/components/exams/exam-form"; |
| 12 | import { RouteGuard } from "@/components/layout/route-guard"; | 13 | import { RouteGuard } from "@/components/layout/route-guard"; |
| 13 | import { PageHeader } from "@/components/layout/page-header"; | 14 | import { PageHeader } from "@/components/layout/page-header"; |
| @@ -31,7 +32,8 @@ import { usePreferences, useT } from "@/stores/preferences-context"; | |||
| 31 | 32 | ||
| 32 | const EXAM_PAGE_SIZE = 12; | 33 | const EXAM_PAGE_SIZE = 12; |
| 33 | 34 | ||
| 34 | -type ExamStatus = "draft" | "unscheduled" | "upcoming" | "active" | "finished"; | 35 | +type ExamStatus = "draft" | "unscheduled" | "upcoming" | "active" | "finished" | "submitted" | "expired"; |
| 36 | +type ExamRow = Exam & { displayStatus: ExamStatus }; | ||
| 35 | 37 | ||
| 36 | export default function ExamsPage() { | 38 | export default function ExamsPage() { |
| 37 | return ( | 39 | return ( |
| @@ -53,6 +55,7 @@ function ExamsContent() { | |||
| 53 | const [createOpen, setCreateOpen] = createSignal(false); | 55 | const [createOpen, setCreateOpen] = createSignal(false); |
| 54 | const [selectedCourseId, setSelectedCourseId] = createSignal(""); | 56 | const [selectedCourseId, setSelectedCourseId] = createSignal(""); |
| 55 | const [editingExam, setEditingExam] = createSignal<Exam | null>(null); | 57 | const [editingExam, setEditingExam] = createSignal<Exam | null>(null); |
| 58 | + const [attemptStatuses, setAttemptStatuses] = createSignal<Record<string, AttemptStatus | "not_started">>({}); | ||
| 56 | const [flash, setFlash] = createFlash(); | 59 | const [flash, setFlash] = createFlash(); |
| 57 | 60 | ||
| 58 | const [courses] = createResource( | 61 | const [courses] = createResource( |
| @@ -75,9 +78,12 @@ function ExamsContent() { | |||
| 75 | const courseById = createMemo(() => new Map(visibleCourses().map((course) => [course.id, course]))); | 78 | const courseById = createMemo(() => new Map(visibleCourses().map((course) => [course.id, course]))); |
| 76 | const courseTitle = (courseId: string) => courseById().get(courseId)?.title ?? courseId; | 79 | const courseTitle = (courseId: string) => courseById().get(courseId)?.title ?? courseId; |
| 77 | 80 | ||
| 81 | + const isSittable = (exam: Exam) => exam.mode === "sync" || exam.mode === "async" || exam.mode === "open"; | ||
| 78 | const examStatus = (exam: Exam): ExamStatus => { | 82 | const examStatus = (exam: Exam): ExamStatus => { |
| 83 | + const own = attemptStatuses()[exam.id]; | ||
| 84 | + if (own === "submitted" || own === "expired") return own; | ||
| 79 | if (exam.draft) return "draft"; | 85 | if (exam.draft) return "draft"; |
| 80 | - if (exam.mode !== "sync" && exam.mode !== "async" && exam.mode !== "open") return "unscheduled"; | 86 | + if (!isSittable(exam)) return "unscheduled"; |
| 81 | if (exam.mode === "open") return "active"; | 87 | if (exam.mode === "open") return "active"; |
| 82 | const current = now(); | 88 | const current = now(); |
| 83 | if (exam.ends_at != null && exam.ends_at < current) return "finished"; | 89 | if (exam.ends_at != null && exam.ends_at < current) return "finished"; |
| @@ -94,8 +100,8 @@ function ExamsContent() { | |||
| 94 | return true; | 100 | return true; |
| 95 | }); | 101 | }); |
| 96 | }; | 102 | }; |
| 97 | - const searchExam = (exam: Exam, query: string) => | 103 | + const searchExam = (exam: ExamRow, query: string) => |
| 98 | - [exam.title, exam.description, courseTitle(exam.course), examKindLabel(String(exam.kind), t), statusLabel(examStatus(exam))] | 104 | + [exam.title, exam.description, courseTitle(exam.course), examKindLabel(String(exam.kind), t), statusLabel(exam.displayStatus)] |
| 99 | .join(" ") | 105 | .join(" ") |
| 100 | .toLocaleLowerCase(locale()) | 106 | .toLocaleLowerCase(locale()) |
| 101 | .includes(query.toLocaleLowerCase(locale())); | 107 | .includes(query.toLocaleLowerCase(locale())); |
| @@ -115,15 +121,42 @@ function ExamsContent() { | |||
| 115 | }); | 121 | }); |
| 116 | 122 | ||
| 117 | const statusLabel = (status: ExamStatus) => { | 123 | const statusLabel = (status: ExamStatus) => { |
| 124 | + if (status === "submitted") return t("attempt.submitted"); | ||
| 125 | + if (status === "expired") return t("attempt.expired"); | ||
| 118 | if (status === "draft") return t("exams.draft"); | 126 | if (status === "draft") return t("exams.draft"); |
| 119 | if (status === "unscheduled") return t("exams.unscheduled"); | 127 | if (status === "unscheduled") return t("exams.unscheduled"); |
| 120 | if (status === "finished") return t("exams.finished"); | 128 | if (status === "finished") return t("exams.finished"); |
| 121 | if (status === "upcoming") return t("exams.upcoming"); | 129 | if (status === "upcoming") return t("exams.upcoming"); |
| 122 | return t("exams.active"); | 130 | return t("exams.active"); |
| 123 | }; | 131 | }; |
| 132 | + const statusTone = (status: ExamStatus) => status === "expired" ? "finished" : status; | ||
| 124 | 133 | ||
| 125 | - const rows = () => filterExams(list() ?? []); | 134 | + const rows = (): ExamRow[] => filterExams(list() ?? []).map((exam) => ({ ...exam, displayStatus: examStatus(exam) })); |
| 126 | - const columns = createMemo<ColumnDef<Exam>[]>(() => [ | 135 | + createEffect(() => { |
| 136 | + if (!isStudent()) return; | ||
| 137 | + const exams = list(); | ||
| 138 | + if (!exams) return; | ||
| 139 | + const allowed = new Set(visibleCourses().map((course) => course.id)); | ||
| 140 | + const refresh = () => { | ||
| 141 | + for (const exam of exams) { | ||
| 142 | + if (!allowed.has(exam.course) || !isSittable(exam)) continue; | ||
| 143 | + const current = attemptStatuses()[exam.id]; | ||
| 144 | + if (current === "submitted" || current === "expired") continue; | ||
| 145 | + if (current != null && current !== "in_progress") continue; | ||
| 146 | + void getExamAttempt(exam.id) | ||
| 147 | + .then((attempt) => { | ||
| 148 | + setAttemptStatuses((prev) => ({ ...prev, [exam.id]: attempt.status })); | ||
| 149 | + }) | ||
| 150 | + .catch(() => { | ||
| 151 | + setAttemptStatuses((prev) => ({ ...prev, [exam.id]: "not_started" })); | ||
| 152 | + }); | ||
| 153 | + } | ||
| 154 | + }; | ||
| 155 | + refresh(); | ||
| 156 | + const interval = window.setInterval(refresh, 5000); | ||
| 157 | + onCleanup(() => window.clearInterval(interval)); | ||
| 158 | + }); | ||
| 159 | + const columns = createMemo<ColumnDef<ExamRow>[]>(() => [ | ||
| 127 | { | 160 | { |
| 128 | accessorKey: "title", | 161 | accessorKey: "title", |
| 129 | header: t("exams.title"), | 162 | header: t("exams.title"), |
| @@ -149,14 +182,14 @@ function ExamsContent() { | |||
| 149 | }, | 182 | }, |
| 150 | { | 183 | { |
| 151 | id: "status", | 184 | id: "status", |
| 152 | - accessorFn: (exam) => statusLabel(examStatus(exam)), | 185 | + accessorFn: (exam) => statusLabel(exam.displayStatus), |
| 153 | header: t("attempt.status"), | 186 | header: t("attempt.status"), |
| 154 | meta: { headerClass: "text-center", cellClass: "text-center" }, | 187 | meta: { headerClass: "text-center", cellClass: "text-center" }, |
| 155 | cell: (cell) => { | 188 | cell: (cell) => { |
| 156 | - const status = examStatus(cell.row.original); | 189 | + const status = cell.row.original.displayStatus; |
| 157 | return ( | 190 | return ( |
| 158 | - <Badge variant="outline" class={cn("w-28 justify-center rounded-sm", scheduleStatusClass(status))}> | 191 | + <Badge variant="outline" class={cn("w-28 justify-center rounded-sm", scheduleStatusClass(statusTone(status)))}> |
| 159 | - <span class={cn("mr-1.5 h-1.5 w-1.5 rounded-full", scheduleStatusDotClass(status))} /> | 192 | + <span class={cn("mr-1.5 h-1.5 w-1.5 rounded-full", scheduleStatusDotClass(statusTone(status)))} /> |
| 160 | {statusLabel(status)} | 193 | {statusLabel(status)} |
| 161 | </Badge> | 194 | </Badge> |
| 162 | ); | 195 | ); |
| @@ -251,6 +284,8 @@ function ExamsContent() { | |||
| 251 | <> | 284 | <> |
| 252 | <Select class="h-9 w-full rounded-sm sm:w-40" value={statusFilter()} onChange={(event) => setStatusFilter(event.currentTarget.value as ExamStatus | "all")}> | 285 | <Select class="h-9 w-full rounded-sm sm:w-40" value={statusFilter()} onChange={(event) => setStatusFilter(event.currentTarget.value as ExamStatus | "all")}> |
| 253 | <option value="all">{t("common.all")}</option> | 286 | <option value="all">{t("common.all")}</option> |
| 287 | + <option value="submitted">{t("attempt.submitted")}</option> | ||
| 288 | + <option value="expired">{t("attempt.expired")}</option> | ||
| 254 | <option value="draft">{t("exams.draft")}</option> | 289 | <option value="draft">{t("exams.draft")}</option> |
| 255 | <option value="upcoming">{t("exams.upcoming")}</option> | 290 | <option value="upcoming">{t("exams.upcoming")}</option> |
| 256 | <option value="active">{t("exams.active")}</option> | 291 | <option value="active">{t("exams.active")}</option> |