diff --git a/src/api/deleteExamChoiceImage.ts b/src/api/deleteExamChoiceImage.ts new file mode 100644 index 0000000..80504e1 --- /dev/null +++ b/src/api/deleteExamChoiceImage.ts @@ -0,0 +1,5 @@ +import { client } from "./client"; + +export function deleteExamChoiceImage(examId: string, questionId: string, index: number): Promise { + return client(`/exams/${examId}/questions/${questionId}/choices/${index}/image`, { method: "DELETE" }); +} diff --git a/src/api/deleteExamQuestionImage.ts b/src/api/deleteExamQuestionImage.ts new file mode 100644 index 0000000..42f73af --- /dev/null +++ b/src/api/deleteExamQuestionImage.ts @@ -0,0 +1,5 @@ +import { client } from "./client"; + +export function deleteExamQuestionImage(examId: string, questionId: string): Promise { + return client(`/exams/${examId}/questions/${questionId}/image`, { method: "DELETE" }); +} diff --git a/src/api/getExamChoiceImageBlob.ts b/src/api/getExamChoiceImageBlob.ts new file mode 100644 index 0000000..e3405ef --- /dev/null +++ b/src/api/getExamChoiceImageBlob.ts @@ -0,0 +1,5 @@ +import { blobClient } from "./client"; + +export function getExamChoiceImageBlob(examId: string, questionId: string, index: number, signal?: AbortSignal): Promise { + return blobClient(`/exams/${examId}/questions/${questionId}/choices/${index}/image`, signal); +} diff --git a/src/api/getExamQuestionImageBlob.ts b/src/api/getExamQuestionImageBlob.ts new file mode 100644 index 0000000..66af25c --- /dev/null +++ b/src/api/getExamQuestionImageBlob.ts @@ -0,0 +1,5 @@ +import { blobClient } from "./client"; + +export function getExamQuestionImageBlob(examId: string, questionId: string, signal?: AbortSignal): Promise { + return blobClient(`/exams/${examId}/questions/${questionId}/image`, signal); +} diff --git a/src/api/postExamChoiceImage.ts b/src/api/postExamChoiceImage.ts new file mode 100644 index 0000000..ee889d4 --- /dev/null +++ b/src/api/postExamChoiceImage.ts @@ -0,0 +1,8 @@ +import { formClient } from "./client"; +import type { ImageMeta } from "./types"; + +export function postExamChoiceImage(examId: string, questionId: string, index: number, file: File, signal?: AbortSignal): Promise { + const body = new FormData(); + body.append("file", file); + return formClient(`/exams/${examId}/questions/${questionId}/choices/${index}/image`, body, signal); +} diff --git a/src/api/postExamQuestionImage.ts b/src/api/postExamQuestionImage.ts new file mode 100644 index 0000000..b9bcb9b --- /dev/null +++ b/src/api/postExamQuestionImage.ts @@ -0,0 +1,8 @@ +import { formClient } from "./client"; +import type { ImageMeta } from "./types"; + +export function postExamQuestionImage(examId: string, questionId: string, file: File, signal?: AbortSignal): Promise { + const body = new FormData(); + body.append("file", file); + return formClient(`/exams/${examId}/questions/${questionId}/image`, body, signal); +} diff --git a/src/components/exams/exam-questions-panel.tsx b/src/components/exams/exam-questions-panel.tsx index f222840..1fffa2e 100644 --- a/src/components/exams/exam-questions-panel.tsx +++ b/src/components/exams/exam-questions-panel.tsx @@ -1,8 +1,12 @@ import { For, Show, Suspense, createEffect, createMemo, createResource, createSignal } from "solid-js"; +import { deleteExamChoiceImage } from "@/api/deleteExamChoiceImage"; +import { deleteExamQuestionImage } from "@/api/deleteExamQuestionImage"; import { deleteExamQuestionById } from "@/api/deleteExamQuestionById"; import { getCourseSubjects } from "@/api/getCourseSubjects"; import { getExamQuestions } from "@/api/getExamQuestions"; import { patchExamQuestionById } from "@/api/patchExamQuestionById"; +import { postExamChoiceImage } from "@/api/postExamChoiceImage"; +import { postExamQuestionImage } from "@/api/postExamQuestionImage"; import { postExamQuestion } from "@/api/postExamQuestion"; import { ApiError, formatApiError } from "@/api/client"; import type { ExamQuestion } from "@/api/types"; @@ -34,6 +38,7 @@ export function ExamQuestionsPanel(props: { examId: string; courseId: string; re const [editing, setEditing] = createSignal(null); const [removeQuestion, setRemoveQuestion] = createSignal(null); const [error, setError] = createSignal(""); + const [imagePending, setImagePending] = createSignal(""); const [flash, setFlash] = createFlash(); const [page, setPage] = createSignal(0); @@ -64,11 +69,17 @@ export function ExamQuestionsPanel(props: { examId: string; courseId: string; re try { const q = editing(); const isNewQuestion = !q; + const { image, choice_images, ...body } = values; + const saved = q + ? await patchExamQuestionById(props.examId, q.id, body) + : await postExamQuestion(props.examId, body); + if (image) await postExamQuestionImage(props.examId, saved.id, image); + for (const [index, file] of (choice_images ?? []).entries()) { + if (file) await postExamChoiceImage(props.examId, saved.id, index, file); + } if (q) { - await patchExamQuestionById(props.examId, q.id, values); setFlash(t("common.saved")); } else { - await postExamQuestion(props.examId, values); setFlash(t("common.created")); } setEditing(null); @@ -80,6 +91,36 @@ export function ExamQuestionsPanel(props: { examId: string; courseId: string; re } }; + const removeQuestionImage = async (question: ExamQuestion) => { + if (props.readOnly) return; + setError(""); + setImagePending(`${question.id}:question`); + try { + await deleteExamQuestionImage(props.examId, question.id); + await refetch(); + setFlash(t("common.deleted")); + } catch (err) { + setError(formatApiError(err)); + } finally { + setImagePending(""); + } + }; + + const removeChoiceImage = async (question: ExamQuestion, index: number) => { + if (props.readOnly) return; + setError(""); + setImagePending(`${question.id}:choice:${index}`); + try { + await deleteExamChoiceImage(props.examId, question.id, index); + await refetch(); + setFlash(t("common.deleted")); + } catch (err) { + setError(formatApiError(err)); + } finally { + setImagePending(""); + } + }; + return (
@@ -156,6 +197,11 @@ export function ExamQuestionsPanel(props: { examId: string; courseId: string; re class="max-h-64 rounded-md border object-contain" /> + + +

{q.text}

    @@ -171,14 +217,21 @@ export function ExamQuestionsPanel(props: { examId: string; courseId: string; re {String.fromCharCode(65 + choiceIndex())} - {choice} - - {t("questions.choiceImage")} - + + {choice} + + {t("questions.choiceImage")} + + + + + )} diff --git a/src/components/exams/question-form.tsx b/src/components/exams/question-form.tsx index d6fbd6a..c8b76fc 100644 --- a/src/components/exams/question-form.tsx +++ b/src/components/exams/question-form.tsx @@ -3,11 +3,12 @@ import { formatApiError } from "@/api/client"; import type { ExamQuestion, QuestionKind, Subject } from "@/api/types"; import { QUESTION_KINDS } from "@/api/types"; import { Button } from "@/components/ui/button"; -import { IconCheck, IconPlus, IconTrash } from "@/components/ui/icons"; +import { IconCheck, IconFileImage, IconPlus, IconTrash } from "@/components/ui/icons"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select } from "@/components/ui/select"; import { Textarea } from "@/components/ui/textarea"; +import { cn } from "@/lib/cn"; import { useT } from "@/stores/preferences-context"; export type QuestionValues = { @@ -17,6 +18,8 @@ export type QuestionValues = { points: number; choices: string[] | null; correct: number | null; + image: File | null; + choice_images: (File | null)[] | null; }; export function QuestionForm(props: { @@ -33,6 +36,8 @@ export function QuestionForm(props: { const [points, setPoints] = createSignal(String(props.initial?.points ?? 1)); const [choices, setChoices] = createSignal(props.initial?.choices ?? ["", "", "", ""]); const [correct, setCorrect] = createSignal(props.initial?.correct ?? 0); + const [image, setImage] = createSignal(null); + const [choiceImages, setChoiceImages] = createSignal<(File | null)[]>(choices().map(() => null)); const [error, setError] = createSignal(""); const [pending, setPending] = createSignal(false); @@ -44,6 +49,8 @@ export function QuestionForm(props: { setPoints(String(initial?.points ?? 1)); setChoices(initial?.choices ?? ["", "", "", ""]); setCorrect(initial?.correct ?? 0); + setImage(null); + setChoiceImages((initial?.choices ?? ["", "", "", ""]).map(() => null)); setError(""); }); @@ -62,12 +69,18 @@ export function QuestionForm(props: { setChoices((current) => current.map((choice, i) => (i === index ? value : choice))); }; + const setChoiceImage = (index: number, file: File | null) => { + setChoiceImages((current) => choices().map((_, i) => (i === index ? file : current[i] ?? null))); + }; + const addChoice = () => { setChoices((current) => (current.length >= 10 ? current : [...current, ""])); + setChoiceImages((current) => (current.length >= 10 ? current : [...current, null])); }; const removeChoice = (index: number) => { setChoices((current) => current.filter((_, i) => i !== index)); + setChoiceImages((current) => current.filter((_, i) => i !== index)); setCorrect((current) => Math.max(0, Math.min(current, choices().length - 2))); }; @@ -80,15 +93,18 @@ export function QuestionForm(props: { const p = Number(points()); if (!Number.isInteger(p) || p < 1 || p > 100) return t("questions.pointsRange"); if (kind() === "text") { - return { subject_id: subject, text: body, kind: "text", points: p, choices: null, correct: null }; + return { subject_id: subject, text: body, kind: "text", points: p, choices: null, correct: null, image: image(), choice_images: null }; } - const cleanChoices = choices().map((choice) => choice.trim()).filter(Boolean); + const indexedChoices = choices() + .map((choice, index) => ({ choice: choice.trim(), image: choiceImages()[index] ?? null })) + .filter((item) => item.choice); + const cleanChoices = indexedChoices.map((item) => item.choice); if (cleanChoices.length < 2 || cleanChoices.length > 10 || cleanChoices.some((choice) => choice.length > 500)) { return t("questions.choicesRange"); } const c = correct(); if (!Number.isInteger(c) || c < 0 || c >= cleanChoices.length) return t("questions.correctRange"); - return { subject_id: subject, text: body, kind: "choice", points: p, choices: cleanChoices, correct: c }; + return { subject_id: subject, text: body, kind: "choice", points: p, choices: cleanChoices, correct: c, image: image(), choice_images: indexedChoices.map((item) => item.image) }; }; const submit = async (e: SubmitEvent) => { @@ -128,19 +144,56 @@ export function QuestionForm(props: { resizeTextArea(); }} /> +
    + + + {t("questions.image")} + + setImage(event.currentTarget.files?.[0] ?? null)} + /> + +
-
- -
+
+ +
- setSubjectId(e.currentTarget.value)}> {(subject) => }
-
- setKind(e.currentTarget.value as QuestionKind)}> {(k) => } @@ -154,6 +207,7 @@ export function QuestionForm(props: { min={1} max={100} value={points()} + class="h-10" required onInput={(e) => setPoints(e.currentTarget.value)} /> @@ -210,11 +264,31 @@ export function QuestionForm(props: { placeholder={t("questions.choicePlaceholder", { index: String.fromCharCode(65 + index) })} onInput={(e) => setChoice(index, e.currentTarget.value)} /> + + {t("questions.choiceImage")} + +
+ + setChoiceImage(index, event.currentTarget.files?.[0] ?? null)} + /> + +
)}
-

{t("questions.choicesHint")}

{error() &&

{error()}

}