🐛 improve exam question editing UX
Browse files at this commitbc76d2dca5fcbeeb524ee61c9fbf343641cf8b98Parent: 8117750- fix
- 🐛 improve exam question editing UX
3 changed files +110 -53
- src/components/exams/exam-questions-panel.tsx +97 -40
- src/components/exams/question-form.tsx +11 -11
- src/i18n/messages.ts +2 -2
src/components/exams/exam-questions-panel.tsxmodified+97 -40
| @@ -9,10 +9,12 @@ import { Badge } from "@/components/ui/badge"; | ||
| 9 | 9 | import { Button } from "@/components/ui/button"; |
| 10 | 10 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; |
| 11 | 11 | import { QuestionForm, type QuestionValues } from "@/components/exams/question-form"; |
| 12 | -import { IconPlus, IconTrash } from "@/components/ui/icons"; | |
| 12 | +import { IconChevronLeft, IconChevronRight, IconPlus, IconTrash } from "@/components/ui/icons"; | |
| 13 | 13 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 14 | 14 | import { useT } from "@/stores/preferences-context"; |
| 15 | 15 | |
| 16 | +const QUESTION_PAGE_SIZE = 5; | |
| 17 | + | |
| 16 | 18 | export function ExamQuestionsPanel(props: { examId: string; readOnly?: boolean }) { |
| 17 | 19 | const t = useT(); |
| 18 | 20 | const [questions, { refetch }] = createResource(() => props.examId, async (examId) => { |
| @@ -27,8 +29,16 @@ export function ExamQuestionsPanel(props: { examId: string; readOnly?: boolean } | ||
| 27 | 29 | const [editing, setEditing] = createSignal<ExamQuestion | null>(null); |
| 28 | 30 | const [removeQuestion, setRemoveQuestion] = createSignal<ExamQuestion | null>(null); |
| 29 | 31 | const [error, setError] = createSignal(""); |
| 32 | + const [page, setPage] = createSignal(0); | |
| 30 | 33 | |
| 31 | 34 | const formInitial = createMemo(() => editing() ?? undefined); |
| 35 | + const questionList = createMemo(() => questions() ?? []); | |
| 36 | + const totalPages = createMemo(() => Math.max(1, Math.ceil(questionList().length / QUESTION_PAGE_SIZE))); | |
| 37 | + const safePage = createMemo(() => Math.min(page(), totalPages() - 1)); | |
| 38 | + const pageItems = createMemo(() => { | |
| 39 | + const start = safePage() * QUESTION_PAGE_SIZE; | |
| 40 | + return questionList().slice(start, start + QUESTION_PAGE_SIZE); | |
| 41 | + }); | |
| 32 | 42 | |
| 33 | 43 | createEffect(() => { |
| 34 | 44 | if (!props.readOnly) return; |
| @@ -37,11 +47,16 @@ export function ExamQuestionsPanel(props: { examId: string; readOnly?: boolean } | ||
| 37 | 47 | setRemoveQuestion(null); |
| 38 | 48 | }); |
| 39 | 49 | |
| 50 | + createEffect(() => { | |
| 51 | + if (page() >= totalPages()) setPage(totalPages() - 1); | |
| 52 | + }); | |
| 53 | + | |
| 40 | 54 | const submit = async (values: QuestionValues) => { |
| 41 | 55 | if (props.readOnly) return; |
| 42 | 56 | setError(""); |
| 43 | 57 | try { |
| 44 | 58 | const q = editing(); |
| 59 | + const isNewQuestion = !q; | |
| 45 | 60 | if (q) { |
| 46 | 61 | await patchExamQuestionById(props.examId, q.id, values); |
| 47 | 62 | } else { |
| @@ -50,6 +65,7 @@ export function ExamQuestionsPanel(props: { examId: string; readOnly?: boolean } | ||
| 50 | 65 | setEditing(null); |
| 51 | 66 | setShowForm(false); |
| 52 | 67 | await refetch(); |
| 68 | + if (isNewQuestion) setPage(Math.max(0, Math.ceil(questionList().length / QUESTION_PAGE_SIZE) - 1)); | |
| 53 | 69 | } catch (err) { |
| 54 | 70 | setError(formatApiError(err)); |
| 55 | 71 | } |
| @@ -90,50 +106,91 @@ export function ExamQuestionsPanel(props: { examId: string; readOnly?: boolean } | ||
| 90 | 106 | |
| 91 | 107 | <Suspense fallback={<PageSpinner />}> |
| 92 | 108 | <Show |
| 93 | - when={(questions() ?? []).length > 0} | |
| 109 | + when={questionList().length > 0} | |
| 94 | 110 | fallback={<p class="rounded-sm bg-muted/40 px-3 py-4 text-sm text-muted-foreground">{t("questions.empty")}</p>} |
| 95 | 111 | > |
| 96 | - <ol class="space-y-3"> | |
| 97 | - <For each={questions() ?? []}> | |
| 98 | - {(q, index) => ( | |
| 99 | - <li class="rounded-md border p-4"> | |
| 100 | - <div class="flex flex-wrap items-start justify-between gap-3"> | |
| 101 | - <div class="min-w-0 space-y-2"> | |
| 102 | - <div class="flex flex-wrap items-center gap-2"> | |
| 103 | - <span class="text-xs font-semibold text-muted-foreground">#{index() + 1}</span> | |
| 104 | - <Badge variant="outline" class="capitalize"> | |
| 105 | - {q.kind === "choice" ? t("questions.kind.choice") : t("questions.kind.text")} | |
| 106 | - </Badge> | |
| 107 | - <Badge variant="outline">{q.points} {t("questions.points")}</Badge> | |
| 112 | + <div class="space-y-3"> | |
| 113 | + <ol class="space-y-3"> | |
| 114 | + <For each={pageItems()}> | |
| 115 | + {(q, index) => ( | |
| 116 | + <li class="rounded-md border p-4"> | |
| 117 | + <div class="flex flex-wrap items-start justify-between gap-3"> | |
| 118 | + <div class="min-w-0 space-y-2"> | |
| 119 | + <div class="flex flex-wrap items-center gap-2"> | |
| 120 | + <span class="text-xs font-semibold text-muted-foreground">#{safePage() * QUESTION_PAGE_SIZE + index() + 1}</span> | |
| 121 | + <Badge variant="outline" class="capitalize"> | |
| 122 | + {q.kind === "choice" ? t("questions.kind.choice") : t("questions.kind.text")} | |
| 123 | + </Badge> | |
| 124 | + <Badge variant="outline">{q.points} {t("questions.points")}</Badge> | |
| 125 | + </div> | |
| 126 | + <p class="whitespace-pre-wrap text-sm font-medium">{q.text}</p> | |
| 127 | + <Show when={q.kind === "choice" && q.choices}> | |
| 128 | + <ol class="grid gap-2 text-sm text-muted-foreground"> | |
| 129 | + <For each={q.choices ?? []}> | |
| 130 | + {(choice, choiceIndex) => ( | |
| 131 | + <li | |
| 132 | + class={ | |
| 133 | + choiceIndex() === q.correct | |
| 134 | + ? "flex items-start gap-3 rounded-sm bg-primary/10 px-3 py-2 font-medium text-foreground" | |
| 135 | + : "flex items-start gap-3 rounded-sm bg-muted/30 px-3 py-2" | |
| 136 | + } | |
| 137 | + > | |
| 138 | + <span class="mt-0.5 inline-flex h-5 min-w-5 items-center justify-center rounded-[3px] border bg-background text-[11px] font-semibold text-foreground"> | |
| 139 | + {String.fromCharCode(65 + choiceIndex())} | |
| 140 | + </span> | |
| 141 | + <span class="min-w-0 whitespace-pre-wrap">{choice}</span> | |
| 142 | + </li> | |
| 143 | + )} | |
| 144 | + </For> | |
| 145 | + </ol> | |
| 146 | + </Show> | |
| 108 | 147 | </div> |
| 109 | - <p class="whitespace-pre-wrap text-sm font-medium">{q.text}</p> | |
| 110 | - <Show when={q.kind === "choice" && q.choices}> | |
| 111 | - <ol class="grid gap-1 text-sm text-muted-foreground"> | |
| 112 | - <For each={q.choices ?? []}> | |
| 113 | - {(choice, choiceIndex) => ( | |
| 114 | - <li class={choiceIndex() === q.correct ? "font-medium text-foreground" : undefined}> | |
| 115 | - {choiceIndex()}. {choice} | |
| 116 | - </li> | |
| 117 | - )} | |
| 118 | - </For> | |
| 119 | - </ol> | |
| 148 | + <Show when={!props.readOnly}> | |
| 149 | + <div class="flex gap-2"> | |
| 150 | + <Button type="button" variant="outline" size="sm" onClick={() => setEditing(q)}> | |
| 151 | + {t("common.edit")} | |
| 152 | + </Button> | |
| 153 | + <Button type="button" variant="ghost" size="sm" class="text-destructive" onClick={() => setRemoveQuestion(q)}> | |
| 154 | + <IconTrash class="h-4 w-4" /> | |
| 155 | + </Button> | |
| 156 | + </div> | |
| 120 | 157 | </Show> |
| 121 | 158 | </div> |
| 122 | - <Show when={!props.readOnly}> | |
| 123 | - <div class="flex gap-2"> | |
| 124 | - <Button type="button" variant="outline" size="sm" onClick={() => setEditing(q)}> | |
| 125 | - {t("common.edit")} | |
| 126 | - </Button> | |
| 127 | - <Button type="button" variant="ghost" size="sm" class="text-destructive" onClick={() => setRemoveQuestion(q)}> | |
| 128 | - <IconTrash class="h-4 w-4" /> | |
| 129 | - </Button> | |
| 130 | - </div> | |
| 131 | - </Show> | |
| 132 | - </div> | |
| 133 | - </li> | |
| 134 | - )} | |
| 135 | - </For> | |
| 136 | - </ol> | |
| 159 | + </li> | |
| 160 | + )} | |
| 161 | + </For> | |
| 162 | + </ol> | |
| 163 | + | |
| 164 | + <Show when={questionList().length > QUESTION_PAGE_SIZE}> | |
| 165 | + <div class="flex items-center justify-between gap-2 border-t border-border/60 pt-3"> | |
| 166 | + <Button | |
| 167 | + type="button" | |
| 168 | + variant="outline" | |
| 169 | + size="sm" | |
| 170 | + class="h-8 gap-1" | |
| 171 | + disabled={safePage() <= 0} | |
| 172 | + onClick={() => setPage((p) => Math.max(0, p - 1))} | |
| 173 | + > | |
| 174 | + <IconChevronLeft class="h-3.5 w-3.5" /> | |
| 175 | + {t("common.prev")} | |
| 176 | + </Button> | |
| 177 | + <span class="text-xs tabular-nums text-muted-foreground"> | |
| 178 | + {t("common.pageOf", { page: safePage() + 1, total: totalPages() })} | |
| 179 | + </span> | |
| 180 | + <Button | |
| 181 | + type="button" | |
| 182 | + variant="outline" | |
| 183 | + size="sm" | |
| 184 | + class="h-8 gap-1" | |
| 185 | + disabled={safePage() >= totalPages() - 1} | |
| 186 | + onClick={() => setPage((p) => Math.min(totalPages() - 1, p + 1))} | |
| 187 | + > | |
| 188 | + {t("common.next")} | |
| 189 | + <IconChevronRight class="h-3.5 w-3.5" /> | |
| 190 | + </Button> | |
| 191 | + </div> | |
| 192 | + </Show> | |
| 193 | + </div> | |
| 137 | 194 | </Show> |
| 138 | 195 | </Suspense> |
| 139 | 196 |
src/components/exams/question-form.tsxmodified+11 -11
| @@ -1,4 +1,4 @@ | ||
| 1 | -import { For, Show, createEffect, createSignal } from "solid-js"; | |
| 1 | +import { For, Index, Show, createEffect, createSignal } from "solid-js"; | |
| 2 | 2 | import { formatApiError } from "@/api/client"; |
| 3 | 3 | import type { ExamQuestion, QuestionKind } from "@/api/types"; |
| 4 | 4 | import { QUESTION_KINDS } from "@/api/types"; |
| @@ -161,23 +161,23 @@ export function QuestionForm(props: { | ||
| 161 | 161 | </Button> |
| 162 | 162 | </div> |
| 163 | 163 | <div class="grid gap-3 xl:grid-cols-2"> |
| 164 | - <For each={choices()}> | |
| 164 | + <Index each={choices()}> | |
| 165 | 165 | {(choice, index) => ( |
| 166 | 166 | <div class="min-h-28 rounded-md border bg-background p-3"> |
| 167 | 167 | <div class="mb-2 flex items-center justify-between gap-2"> |
| 168 | 168 | <button |
| 169 | 169 | type="button" |
| 170 | 170 | class={ |
| 171 | - correct() === index() | |
| 171 | + correct() === index | |
| 172 | 172 | ? "inline-flex h-8 items-center gap-2 rounded-sm bg-primary px-2 text-xs font-medium text-primary-foreground" |
| 173 | 173 | : "inline-flex h-8 items-center gap-2 rounded-sm border px-2 text-xs font-medium text-muted-foreground hover:bg-accent" |
| 174 | 174 | } |
| 175 | - onClick={() => setCorrect(index())} | |
| 175 | + onClick={() => setCorrect(index)} | |
| 176 | 176 | > |
| 177 | 177 | <span class="inline-flex h-5 w-5 items-center justify-center rounded-[3px] border bg-background text-foreground"> |
| 178 | - {String.fromCharCode(65 + index())} | |
| 178 | + {String.fromCharCode(65 + index)} | |
| 179 | 179 | </span> |
| 180 | - {correct() === index() ? <IconCheck class="h-3.5 w-3.5" /> : t("questions.correct")} | |
| 180 | + {correct() === index ? <IconCheck class="h-3.5 w-3.5" /> : t("questions.correct")} | |
| 181 | 181 | </button> |
| 182 | 182 | <Button |
| 183 | 183 | type="button" |
| @@ -185,21 +185,21 @@ export function QuestionForm(props: { | ||
| 185 | 185 | size="sm" |
| 186 | 186 | disabled={choices().length <= 2} |
| 187 | 187 | class="h-8 px-2 text-destructive" |
| 188 | - onClick={() => removeChoice(index())} | |
| 188 | + onClick={() => removeChoice(index)} | |
| 189 | 189 | > |
| 190 | 190 | <IconTrash class="h-3.5 w-3.5" /> |
| 191 | 191 | </Button> |
| 192 | 192 | </div> |
| 193 | 193 | <Input |
| 194 | 194 | class="h-10" |
| 195 | - value={choice} | |
| 195 | + value={choice()} | |
| 196 | 196 | maxlength={500} |
| 197 | - placeholder={t("questions.choicePlaceholder", { index: String.fromCharCode(65 + index()) })} | |
| 198 | - onInput={(e) => setChoice(index(), e.currentTarget.value)} | |
| 197 | + placeholder={t("questions.choicePlaceholder", { index: String.fromCharCode(65 + index) })} | |
| 198 | + onInput={(e) => setChoice(index, e.currentTarget.value)} | |
| 199 | 199 | /> |
| 200 | 200 | </div> |
| 201 | 201 | )} |
| 202 | - </For> | |
| 202 | + </Index> | |
| 203 | 203 | </div> |
| 204 | 204 | <p class="text-xs text-muted-foreground">{t("questions.choicesHint")}</p> |
| 205 | 205 | </div> |
src/i18n/messages.tsmodified+2 -2
| @@ -519,7 +519,7 @@ const en: Dict = { | ||
| 519 | 519 | "questions.addChoice": "Add choice", |
| 520 | 520 | "questions.choicePlaceholder": "Choice {index}", |
| 521 | 521 | "questions.correctAnswer": "Correct answer", |
| 522 | - "questions.correct": "Correct choice index", | |
| 522 | + "questions.correct": "Mark correct", | |
| 523 | 523 | "questions.correctHint": "Zero-based: first choice is 0.", |
| 524 | 524 | "questions.kind.choice": "Choice", |
| 525 | 525 | "questions.kind.text": "Text", |
| @@ -871,7 +871,7 @@ const tr: Dict = { | ||
| 871 | 871 | "questions.addChoice": "Seçenek ekle", |
| 872 | 872 | "questions.choicePlaceholder": "Seçenek {index}", |
| 873 | 873 | "questions.correctAnswer": "Doğru cevap", |
| 874 | - "questions.correct": "Doğru seçenek indeksi", | |
| 874 | + "questions.correct": "Doğru seç", | |
| 875 | 875 | "questions.correctHint": "Sıfırdan başlar: ilk seçenek 0.", |
| 876 | 876 | "questions.kind.choice": "Seçmeli", |
| 877 | 877 | "questions.kind.text": "Metin", |