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