forge
Explore
Log in

Don't you dare go Hollow

🐛 improve exam question editing UX

Browse files at this commit

bc76d2dca5fcbeeb524ee61c9fbf343641cf8b98Burak BoduroğluParent: 8117750
  • fix
    • 🐛 improve exam question editing UX
Show full message
fix: 🐛 improve exam question editing UX

- keep choice inputs stable while typing

- show saved choices with A/B/C/D labels instead of numeric indexes

- add spacing and correct-answer emphasis to choice rows

- paginate the question list to keep the panel compact

- jump to the last page after creating a new question
src/components/exams/exam-questions-panel.tsxmodified+97 -40
@@ -9,10 +9,12 @@ import { Badge } from "@/components/ui/badge";
99 import { Button } from "@/components/ui/button";
1010 import { ConfirmDialog } from "@/components/ui/confirm-dialog";
1111 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";
1313 import { PageSpinner } from "@/components/ui/page-spinner";
1414 import { useT } from "@/stores/preferences-context";
1515
16+const QUESTION_PAGE_SIZE = 5;
17+
1618 export function ExamQuestionsPanel(props: { examId: string; readOnly?: boolean }) {
1719 const t = useT();
1820 const [questions, { refetch }] = createResource(() => props.examId, async (examId) => {
@@ -27,8 +29,16 @@ export function ExamQuestionsPanel(props: { examId: string; readOnly?: boolean }
2729 const [editing, setEditing] = createSignal<ExamQuestion | null>(null);
2830 const [removeQuestion, setRemoveQuestion] = createSignal<ExamQuestion | null>(null);
2931 const [error, setError] = createSignal("");
32+ const [page, setPage] = createSignal(0);
3033
3134 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+ });
3242
3343 createEffect(() => {
3444 if (!props.readOnly) return;
@@ -37,11 +47,16 @@ export function ExamQuestionsPanel(props: { examId: string; readOnly?: boolean }
3747 setRemoveQuestion(null);
3848 });
3949
50+ createEffect(() => {
51+ if (page() >= totalPages()) setPage(totalPages() - 1);
52+ });
53+
4054 const submit = async (values: QuestionValues) => {
4155 if (props.readOnly) return;
4256 setError("");
4357 try {
4458 const q = editing();
59+ const isNewQuestion = !q;
4560 if (q) {
4661 await patchExamQuestionById(props.examId, q.id, values);
4762 } else {
@@ -50,6 +65,7 @@ export function ExamQuestionsPanel(props: { examId: string; readOnly?: boolean }
5065 setEditing(null);
5166 setShowForm(false);
5267 await refetch();
68+ if (isNewQuestion) setPage(Math.max(0, Math.ceil(questionList().length / QUESTION_PAGE_SIZE) - 1));
5369 } catch (err) {
5470 setError(formatApiError(err));
5571 }
@@ -90,50 +106,91 @@ export function ExamQuestionsPanel(props: { examId: string; readOnly?: boolean }
90106
91107 <Suspense fallback={<PageSpinner />}>
92108 <Show
93- when={(questions() ?? []).length > 0}
109+ when={questionList().length > 0}
94110 fallback={<p class="rounded-sm bg-muted/40 px-3 py-4 text-sm text-muted-foreground">{t("questions.empty")}</p>}
95111 >
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>
108147 </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>
120157 </Show>
121158 </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>
137194 </Show>
138195 </Suspense>
139196
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";
22 import { formatApiError } from "@/api/client";
33 import type { ExamQuestion, QuestionKind } from "@/api/types";
44 import { QUESTION_KINDS } from "@/api/types";
@@ -161,23 +161,23 @@ export function QuestionForm(props: {
161161 </Button>
162162 </div>
163163 <div class="grid gap-3 xl:grid-cols-2">
164- <For each={choices()}>
164+ <Index each={choices()}>
165165 {(choice, index) => (
166166 <div class="min-h-28 rounded-md border bg-background p-3">
167167 <div class="mb-2 flex items-center justify-between gap-2">
168168 <button
169169 type="button"
170170 class={
171- correct() === index()
171+ correct() === index
172172 ? "inline-flex h-8 items-center gap-2 rounded-sm bg-primary px-2 text-xs font-medium text-primary-foreground"
173173 : "inline-flex h-8 items-center gap-2 rounded-sm border px-2 text-xs font-medium text-muted-foreground hover:bg-accent"
174174 }
175- onClick={() => setCorrect(index())}
175+ onClick={() => setCorrect(index)}
176176 >
177177 <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)}
179179 </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")}
181181 </button>
182182 <Button
183183 type="button"
@@ -185,21 +185,21 @@ export function QuestionForm(props: {
185185 size="sm"
186186 disabled={choices().length <= 2}
187187 class="h-8 px-2 text-destructive"
188- onClick={() => removeChoice(index())}
188+ onClick={() => removeChoice(index)}
189189 >
190190 <IconTrash class="h-3.5 w-3.5" />
191191 </Button>
192192 </div>
193193 <Input
194194 class="h-10"
195- value={choice}
195+ value={choice()}
196196 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)}
199199 />
200200 </div>
201201 )}
202- </For>
202+ </Index>
203203 </div>
204204 <p class="text-xs text-muted-foreground">{t("questions.choicesHint")}</p>
205205 </div>
src/i18n/messages.tsmodified+2 -2
@@ -519,7 +519,7 @@ const en: Dict = {
519519 "questions.addChoice": "Add choice",
520520 "questions.choicePlaceholder": "Choice {index}",
521521 "questions.correctAnswer": "Correct answer",
522- "questions.correct": "Correct choice index",
522+ "questions.correct": "Mark correct",
523523 "questions.correctHint": "Zero-based: first choice is 0.",
524524 "questions.kind.choice": "Choice",
525525 "questions.kind.text": "Text",
@@ -871,7 +871,7 @@ const tr: Dict = {
871871 "questions.addChoice": "Seçenek ekle",
872872 "questions.choicePlaceholder": "Seçenek {index}",
873873 "questions.correctAnswer": "Doğru cevap",
874- "questions.correct": "Doğru seçenek indeksi",
874+ "questions.correct": "Doğru seç",
875875 "questions.correctHint": "Sıfırdan başlar: ilk seçenek 0.",
876876 "questions.kind.choice": "Seçmeli",
877877 "questions.kind.text": "Metin",