Make the attempt picker a dropdown
Browse files at this commit2b5e1a3f21963bf54e6abe3b25aea847a9364c2cParent: f7005de- style
- make the attempt picker a dropdown
2 changed files +78 -40
src/components/exams/answer-sheet-view.tsxmodified+66 -40
| @@ -1,14 +1,15 @@ | |||
| 1 | -import { For, Show, Suspense, createResource, createSignal } from "solid-js"; | 1 | +import { For, Show, Suspense, createMemo, createResource, createSignal } from "solid-js"; |
| 2 | import { ApiError } from "@/api/client"; | 2 | import { ApiError } from "@/api/client"; |
| 3 | import { getExamQuestions } from "@/api/exams"; | 3 | import { getExamQuestions } from "@/api/exams"; |
| 4 | import { getStudentAnswers, getStudentAnswerImage } from "@/api/exams"; | 4 | import { getStudentAnswers, getStudentAnswerImage } from "@/api/exams"; |
| 5 | -import { getStudentAttempts, getStudentAttemptAnswers, getStudentAttemptAnswerImage, getStudentMarksHistory } from "@/api/exams"; | 5 | +import { getStudentAttempts, getStudentAttemptAnswers, getStudentAttemptAnswerImage } from "@/api/exams"; |
| 6 | import type { StudentAnswerSheet } from "@/api/client"; | 6 | import type { StudentAnswerSheet } from "@/api/client"; |
| 7 | import { Badge } from "@/components/ui/badge"; | 7 | import { Badge } from "@/components/ui/badge"; |
| 8 | import { Button } from "@/components/ui/button"; | 8 | import { Button } from "@/components/ui/button"; |
| 9 | import { IconCheck, IconDownload, IconX } from "@/components/ui/icons"; | 9 | import { IconCheck, IconDownload, IconX } from "@/components/ui/icons"; |
| 10 | import { PageSpinner } from "@/components/ui/page-spinner"; | 10 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 11 | import { ReplayableImage } from "@/components/ui/replayable-image"; | 11 | import { ReplayableImage } from "@/components/ui/replayable-image"; |
| 12 | +import { DropdownSelect } from "@/components/ui/select"; | ||
| 12 | import { cn } from "@/lib/cn"; | 13 | import { cn } from "@/lib/cn"; |
| 13 | import { joinAnswerSheet } from "@/lib/answer-sheet"; | 14 | import { joinAnswerSheet } from "@/lib/answer-sheet"; |
| 14 | import { personLabelWithId } from "@/lib/person"; | 15 | import { personLabelWithId } from "@/lib/person"; |
| @@ -33,18 +34,6 @@ export function AnswerSheetView(props: { examId: string; userId: string }) { | |||
| 33 | } | 34 | } |
| 34 | }, | 35 | }, |
| 35 | ); | 36 | ); |
| 36 | - const [marks] = createResource( | ||
| 37 | - () => [props.examId, props.userId] as const, | ||
| 38 | - async ([examId, userId]) => { | ||
| 39 | - try { | ||
| 40 | - return await getStudentMarksHistory(examId, userId); | ||
| 41 | - } catch (err) { | ||
| 42 | - if (err instanceof ApiError && err.status === 404) return []; | ||
| 43 | - throw err; | ||
| 44 | - } | ||
| 45 | - }, | ||
| 46 | - ); | ||
| 47 | - | ||
| 48 | const latestSeq = () => { | 37 | const latestSeq = () => { |
| 49 | const a = attempts(); | 38 | const a = attempts(); |
| 50 | return a && a.length ? a[a.length - 1] : null; | 39 | return a && a.length ? a[a.length - 1] : null; |
| @@ -55,7 +44,14 @@ export function AnswerSheetView(props: { examId: string; userId: string }) { | |||
| 55 | const s = activeSeq(); | 44 | const s = activeSeq(); |
| 56 | return s == null || s === latestSeq(); | 45 | return s == null || s === latestSeq(); |
| 57 | }; | 46 | }; |
| 58 | - const markForSeq = (seq: number) => marks()?.[seq - 1]?.mark ?? null; | 47 | + // Dropdown options, newest sitting first (a student may have many). Just the |
| 48 | + // sitting label; the latest is tagged as the current one. Per-sitting stats | ||
| 49 | + // live in the sheet header for the selected sitting, not in the dropdown. | ||
| 50 | + const attemptOptions = () => | ||
| 51 | + [...(attempts() ?? [])].reverse().map((seq) => ({ | ||
| 52 | + value: seq, | ||
| 53 | + label: `${t("exams.attemptN", { n: seq })}${seq === latestSeq() ? ` (${t("exams.currentAttempt")})` : ""}`, | ||
| 54 | + })); | ||
| 59 | 55 | ||
| 60 | const answerImageUrl = (questionId: string) => { | 56 | const answerImageUrl = (questionId: string) => { |
| 61 | const seq = activeSeq(); | 57 | const seq = activeSeq(); |
| @@ -97,32 +93,45 @@ export function AnswerSheetView(props: { examId: string; userId: string }) { | |||
| 97 | }, | 93 | }, |
| 98 | ); | 94 | ); |
| 99 | 95 | ||
| 96 | + // Per-sitting answer breakdown for the viewed attempt's header. `is_correct` | ||
| 97 | + // is only set for auto-gradable (choice) questions: right = got it, wrong = | ||
| 98 | + // missed it, empty = no answer given; answered text questions fall in none | ||
| 99 | + // (a human grades those). `pending` surfaces those so the counts stay honest. | ||
| 100 | + const sheetStats = createMemo(() => { | ||
| 101 | + const rows = data()?.rows ?? []; | ||
| 102 | + const answered = (a: (typeof rows)[number]["answer"]) => | ||
| 103 | + !!a && (a.selected != null || !!a.text || !!a.answer_image); | ||
| 104 | + let right = 0; | ||
| 105 | + let wrong = 0; | ||
| 106 | + let empty = 0; | ||
| 107 | + let pending = 0; | ||
| 108 | + for (const r of rows) { | ||
| 109 | + if (!answered(r.answer)) empty++; | ||
| 110 | + else if (r.answer?.is_correct === true) right++; | ||
| 111 | + else if (r.answer?.is_correct === false) wrong++; | ||
| 112 | + else pending++; | ||
| 113 | + } | ||
| 114 | + return { right, wrong, empty, pending, total: rows.length }; | ||
| 115 | + }); | ||
| 116 | + | ||
| 100 | return ( | 117 | return ( |
| 101 | - <Suspense fallback={<PageSpinner />}> | 118 | + // The picker stays OUTSIDE the Suspense: switching attempts re-fetches |
| 119 | + // `data`, and if the dropdown lived inside the suspending subtree it would | ||
| 120 | + // unmount and its portalled menu would flash unpositioned (top-left) on | ||
| 121 | + // every select. Only the answer sheet suspends. | ||
| 122 | + <div class="space-y-4"> | ||
| 102 | <Show when={(attempts() ?? []).length > 1}> | 123 | <Show when={(attempts() ?? []).length > 1}> |
| 103 | - <div class="flex flex-wrap items-center gap-2"> | 124 | + <div class="flex flex-col gap-1.5"> |
| 104 | <span class="text-xs font-medium text-muted-foreground">{t("exams.previousAttempts")}</span> | 125 | <span class="text-xs font-medium text-muted-foreground">{t("exams.previousAttempts")}</span> |
| 105 | - <For each={attempts()}> | 126 | + <DropdownSelect |
| 106 | - {(seq) => ( | 127 | + options={attemptOptions()} |
| 107 | - <Button | 128 | + value={activeSeq() ?? 0} |
| 108 | - type="button" | 129 | + onChange={(v) => setSelectedSeq(v)} |
| 109 | - size="sm" | 130 | + triggerClass="w-full sm:w-auto" |
| 110 | - variant={activeSeq() === seq ? "secondary" : "outline"} | 131 | + /> |
| 111 | - class="h-8 rounded-lg" | ||
| 112 | - onClick={() => setSelectedSeq(seq)} | ||
| 113 | - > | ||
| 114 | - {t("exams.attemptN", { n: seq })} | ||
| 115 | - <Show when={markForSeq(seq) != null}> | ||
| 116 | - <Badge variant="outline" class="ml-1.5 tabular-nums text-[10px]">{markForSeq(seq)}</Badge> | ||
| 117 | - </Show> | ||
| 118 | - <Show when={seq === latestSeq()}> | ||
| 119 | - <span class="ml-1 text-[10px] text-muted-foreground">({t("exams.currentAttempt")})</span> | ||
| 120 | - </Show> | ||
| 121 | - </Button> | ||
| 122 | - )} | ||
| 123 | - </For> | ||
| 124 | </div> | 132 | </div> |
| 125 | </Show> | 133 | </Show> |
| 134 | + <Suspense fallback={<PageSpinner />}> | ||
| 126 | <Show when={data()}> | 135 | <Show when={data()}> |
| 127 | {(d) => ( | 136 | {(d) => ( |
| 128 | <div class="space-y-4"> | 137 | <div class="space-y-4"> |
| @@ -132,9 +141,25 @@ export function AnswerSheetView(props: { examId: string; userId: string }) { | |||
| 132 | <div class="flex flex-wrap items-center justify-between gap-2 rounded-md border bg-muted/30 px-4 py-3"> | 141 | <div class="flex flex-wrap items-center justify-between gap-2 rounded-md border bg-muted/30 px-4 py-3"> |
| 133 | <p class="text-sm font-medium">{personLabelWithId(d().sheet.user)}</p> | 142 | <p class="text-sm font-medium">{personLabelWithId(d().sheet.user)}</p> |
| 134 | <Show when={d().sheet.answers.length > 0} fallback={<Badge variant="outline">{t("exams.notStarted")}</Badge>}> | 143 | <Show when={d().sheet.answers.length > 0} fallback={<Badge variant="outline">{t("exams.notStarted")}</Badge>}> |
| 135 | - <Badge variant="secondary"> | 144 | + <div class="flex flex-wrap items-center gap-1.5"> |
| 136 | - {t("exams.autoScore")}: {d().sheet.auto_score.earned}/{d().sheet.auto_score.possible} | 145 | + <Badge variant="outline" class="border-success/50 bg-success/10 text-success"> |
| 137 | - </Badge> | 146 | + {t("exams.answersRight")}: {sheetStats().right} |
| 147 | + </Badge> | ||
| 148 | + <Badge variant="outline" class="border-destructive/50 bg-destructive/10 text-destructive"> | ||
| 149 | + {t("exams.answersWrong")}: {sheetStats().wrong} | ||
| 150 | + </Badge> | ||
| 151 | + <Badge variant="outline" class="text-muted-foreground"> | ||
| 152 | + {t("exams.answersEmpty")}: {sheetStats().empty} | ||
| 153 | + </Badge> | ||
| 154 | + <Show when={sheetStats().pending > 0}> | ||
| 155 | + <Badge variant="outline" class="text-muted-foreground"> | ||
| 156 | + {t("exams.answersPending")}: {sheetStats().pending} | ||
| 157 | + </Badge> | ||
| 158 | + </Show> | ||
| 159 | + <Badge variant="secondary"> | ||
| 160 | + {t("exams.autoScore")}: {d().sheet.auto_score.earned}/{d().sheet.auto_score.possible} | ||
| 161 | + </Badge> | ||
| 162 | + </div> | ||
| 138 | </Show> | 163 | </Show> |
| 139 | </div> | 164 | </div> |
| 140 | 165 | ||
| @@ -225,6 +250,7 @@ export function AnswerSheetView(props: { examId: string; userId: string }) { | |||
| 225 | </div> | 250 | </div> |
| 226 | )} | 251 | )} |
| 227 | </Show> | 252 | </Show> |
| 228 | - </Suspense> | 253 | + </Suspense> |
| 254 | + </div> | ||
| 229 | ); | 255 | ); |
| 230 | } | 256 | } |
src/i18n/messages.tsmodified+12 -0
| @@ -659,6 +659,10 @@ export type MessageKey = | |||
| 659 | | "exams.lastActivity" | 659 | | "exams.lastActivity" |
| 660 | | "exams.answerSheet" | 660 | | "exams.answerSheet" |
| 661 | | "exams.autoScore" | 661 | | "exams.autoScore" |
| 662 | + | "exams.answersRight" | ||
| 663 | + | "exams.answersWrong" | ||
| 664 | + | "exams.answersEmpty" | ||
| 665 | + | "exams.answersPending" | ||
| 662 | | "exams.earned" | 666 | | "exams.earned" |
| 663 | | "exams.possible" | 667 | | "exams.possible" |
| 664 | | "exams.isCorrect" | 668 | | "exams.isCorrect" |
| @@ -1489,6 +1493,10 @@ const en: Dict = { | |||
| 1489 | "exams.lastActivity": "Last activity", | 1493 | "exams.lastActivity": "Last activity", |
| 1490 | "exams.answerSheet": "Answer Sheet", | 1494 | "exams.answerSheet": "Answer Sheet", |
| 1491 | "exams.autoScore": "Auto-score", | 1495 | "exams.autoScore": "Auto-score", |
| 1496 | + "exams.answersRight": "Correct", | ||
| 1497 | + "exams.answersWrong": "Wrong", | ||
| 1498 | + "exams.answersEmpty": "Empty", | ||
| 1499 | + "exams.answersPending": "Ungraded", | ||
| 1492 | "exams.earned": "Earned", | 1500 | "exams.earned": "Earned", |
| 1493 | "exams.possible": "Possible", | 1501 | "exams.possible": "Possible", |
| 1494 | "exams.isCorrect": "Correct", | 1502 | "exams.isCorrect": "Correct", |
| @@ -2313,6 +2321,10 @@ const tr: Dict = { | |||
| 2313 | "exams.lastActivity": "Son hareket", | 2321 | "exams.lastActivity": "Son hareket", |
| 2314 | "exams.answerSheet": "Cevap Kâğıdı", | 2322 | "exams.answerSheet": "Cevap Kâğıdı", |
| 2315 | "exams.autoScore": "Otomatik puan", | 2323 | "exams.autoScore": "Otomatik puan", |
| 2324 | + "exams.answersRight": "Doğru", | ||
| 2325 | + "exams.answersWrong": "Yanlış", | ||
| 2326 | + "exams.answersEmpty": "Boş", | ||
| 2327 | + "exams.answersPending": "Değerlendirilmedi", | ||
| 2316 | "exams.earned": "Alınan", | 2328 | "exams.earned": "Alınan", |
| 2317 | "exams.possible": "Mümkün", | 2329 | "exams.possible": "Mümkün", |
| 2318 | "exams.isCorrect": "Doğru", | 2330 | "exams.isCorrect": "Doğru", |