diff --git a/src/components/exams/exam-questions-panel.tsx b/src/components/exams/exam-questions-panel.tsx index 010bd0f..af63604 100644 --- a/src/components/exams/exam-questions-panel.tsx +++ b/src/components/exams/exam-questions-panel.tsx @@ -9,10 +9,12 @@ import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { ConfirmDialog } from "@/components/ui/confirm-dialog"; import { QuestionForm, type QuestionValues } from "@/components/exams/question-form"; -import { IconPlus, IconTrash } from "@/components/ui/icons"; +import { IconChevronLeft, IconChevronRight, IconPlus, IconTrash } from "@/components/ui/icons"; import { PageSpinner } from "@/components/ui/page-spinner"; import { useT } from "@/stores/preferences-context"; +const QUESTION_PAGE_SIZE = 5; + export function ExamQuestionsPanel(props: { examId: string; readOnly?: boolean }) { const t = useT(); const [questions, { refetch }] = createResource(() => props.examId, async (examId) => { @@ -27,8 +29,16 @@ export function ExamQuestionsPanel(props: { examId: string; readOnly?: boolean } const [editing, setEditing] = createSignal(null); const [removeQuestion, setRemoveQuestion] = createSignal(null); const [error, setError] = createSignal(""); + const [page, setPage] = createSignal(0); const formInitial = createMemo(() => editing() ?? undefined); + const questionList = createMemo(() => questions() ?? []); + const totalPages = createMemo(() => Math.max(1, Math.ceil(questionList().length / QUESTION_PAGE_SIZE))); + const safePage = createMemo(() => Math.min(page(), totalPages() - 1)); + const pageItems = createMemo(() => { + const start = safePage() * QUESTION_PAGE_SIZE; + return questionList().slice(start, start + QUESTION_PAGE_SIZE); + }); createEffect(() => { if (!props.readOnly) return; @@ -37,11 +47,16 @@ export function ExamQuestionsPanel(props: { examId: string; readOnly?: boolean } setRemoveQuestion(null); }); + createEffect(() => { + if (page() >= totalPages()) setPage(totalPages() - 1); + }); + const submit = async (values: QuestionValues) => { if (props.readOnly) return; setError(""); try { const q = editing(); + const isNewQuestion = !q; if (q) { await patchExamQuestionById(props.examId, q.id, values); } else { @@ -50,6 +65,7 @@ export function ExamQuestionsPanel(props: { examId: string; readOnly?: boolean } setEditing(null); setShowForm(false); await refetch(); + if (isNewQuestion) setPage(Math.max(0, Math.ceil(questionList().length / QUESTION_PAGE_SIZE) - 1)); } catch (err) { setError(formatApiError(err)); } @@ -90,50 +106,91 @@ export function ExamQuestionsPanel(props: { examId: string; readOnly?: boolean } }> 0} + when={questionList().length > 0} fallback={

{t("questions.empty")}

} > -
    - - {(q, index) => ( -
  1. -
    -
    -
    - #{index() + 1} - - {q.kind === "choice" ? t("questions.kind.choice") : t("questions.kind.text")} - - {q.points} {t("questions.points")} +
    +
      + + {(q, index) => ( +
    1. +
      +
      +
      + #{safePage() * QUESTION_PAGE_SIZE + index() + 1} + + {q.kind === "choice" ? t("questions.kind.choice") : t("questions.kind.text")} + + {q.points} {t("questions.points")} +
      +

      {q.text}

      + +
        + + {(choice, choiceIndex) => ( +
      1. + + {String.fromCharCode(65 + choiceIndex())} + + {choice} +
      2. + )} +
        +
      +
      -

      {q.text}

      - -
        - - {(choice, choiceIndex) => ( -
      1. - {choiceIndex()}. {choice} -
      2. - )} -
        -
      + +
      + + +
      - -
      - - -
      -
      -
    -
  2. - )} -
    -
+ + )} + + + + QUESTION_PAGE_SIZE}> +
+ + + {t("common.pageOf", { page: safePage() + 1, total: totalPages() })} + + +
+
+
diff --git a/src/components/exams/question-form.tsx b/src/components/exams/question-form.tsx index d01358d..f6f9d59 100644 --- a/src/components/exams/question-form.tsx +++ b/src/components/exams/question-form.tsx @@ -1,4 +1,4 @@ -import { For, Show, createEffect, createSignal } from "solid-js"; +import { For, Index, Show, createEffect, createSignal } from "solid-js"; import { formatApiError } from "@/api/client"; import type { ExamQuestion, QuestionKind } from "@/api/types"; import { QUESTION_KINDS } from "@/api/types"; @@ -161,23 +161,23 @@ export function QuestionForm(props: {
- + {(choice, index) => (
setChoice(index(), e.currentTarget.value)} + placeholder={t("questions.choicePlaceholder", { index: String.fromCharCode(65 + index) })} + onInput={(e) => setChoice(index, e.currentTarget.value)} />
)} -
+

{t("questions.choicesHint")}

diff --git a/src/i18n/messages.ts b/src/i18n/messages.ts index 13500ab..f8fd176 100644 --- a/src/i18n/messages.ts +++ b/src/i18n/messages.ts @@ -519,7 +519,7 @@ const en: Dict = { "questions.addChoice": "Add choice", "questions.choicePlaceholder": "Choice {index}", "questions.correctAnswer": "Correct answer", - "questions.correct": "Correct choice index", + "questions.correct": "Mark correct", "questions.correctHint": "Zero-based: first choice is 0.", "questions.kind.choice": "Choice", "questions.kind.text": "Text", @@ -871,7 +871,7 @@ const tr: Dict = { "questions.addChoice": "Seçenek ekle", "questions.choicePlaceholder": "Seçenek {index}", "questions.correctAnswer": "Doğru cevap", - "questions.correct": "Doğru seçenek indeksi", + "questions.correct": "Doğru seç", "questions.correctHint": "Sıfırdan başlar: ilk seçenek 0.", "questions.kind.choice": "Seçmeli", "questions.kind.text": "Metin",