diff --git a/AGENTS.md b/AGENTS.md index d84ef12..2987952 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -36,7 +36,12 @@ instructions. Always written and read in English. `note-card.test.tsx`. - Components in the same domain live in the same folder: `components/notes/`, `components/events/`, `components/exams/`, `components/users/`, - `components/layout/`, `components/ui/`. + `components/attendance/`, `components/courses/`, `components/marks/`, + `components/sessions/`, `components/layout/`, `components/ui/`. +- Cross-cutting app state lives in `src/stores/` (`auth-context.tsx`, + `preferences-context.tsx`) — context providers only, never domain components. + School-policy lists (exam kinds, attendance statuses, grade bands) come from + `getSettings()` in a `createResource`, never hardcoded in components. - **API layer:** Domain-based folders under `src/api/` (e.g., `src/api/notes/`, `src/api/exams/`). One file per request under its domain folder, camelCase and verb-first, mirroring the endpoint exactly (`src/api/notes/getNoteById.ts`, diff --git a/src/api/__tests__/exams/exams.test.ts b/src/api/__tests__/exams/exams.test.ts index 17e2c4f..ac71cb4 100644 --- a/src/api/__tests__/exams/exams.test.ts +++ b/src/api/__tests__/exams/exams.test.ts @@ -3,7 +3,11 @@ import { getExams } from "../../exams"; import { getExamById } from "../../exams"; import { patchExamById } from "../../exams"; import { deleteExamById } from "../../exams"; -import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; +import { getExamQuestionImageBlob } from "../../exams"; +import { postExamAttemptAnswerImage } from "../../exams"; +import { deleteExamAttemptAnswerImage } from "../../exams"; +import { getExamAnswerImageBlob } from "../../exams"; +import { lastFetchCall, mockFetch204, mockFetchBlob, mockFetchSuccess } from "../helpers/mock-fetch"; describe("exams API - core", () => { afterEach(() => { @@ -57,4 +61,55 @@ describe("exams API - core", () => { expect(url).toBe("/api/exams/ex1"); expect(init?.method).toBe("DELETE"); }); + + it("getExamQuestionImageBlob fetches the question image blob", async () => { + const blob = new Blob([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], { type: "image/png" }); + mockFetchBlob(blob); + + const result = await getExamQuestionImageBlob("ex1", "q1"); + expect(result).toBeInstanceOf(Blob); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/exams/ex1/questions/q1/image"); + expect(init?.method ?? "GET").toBe("GET"); + expect(init?.credentials).toBe("same-origin"); + }); + + it("postExamAttemptAnswerImage calls /exams/:id/attempt/answers/:qid/image with FormData", async () => { + const mockMeta = { content_type: "image/png", size: 100 }; + mockFetchSuccess(mockMeta); + + const file = new File(["test"], "answer.png", { type: "image/png" }); + const result = await postExamAttemptAnswerImage("ex1", "q1", file); + expect(result).toEqual(mockMeta); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/exams/ex1/attempt/answers/q1/image"); + expect(init?.method).toBe("POST"); + expect(init?.body).toBeInstanceOf(FormData); + expect((init?.body as FormData).get("file")).toBe(file); + }); + + it("deleteExamAttemptAnswerImage calls /exams/:id/attempt/answers/:qid/image", async () => { + mockFetch204(); + + await deleteExamAttemptAnswerImage("ex1", "q1"); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/exams/ex1/attempt/answers/q1/image"); + expect(init?.method).toBe("DELETE"); + }); + + it("getExamAnswerImageBlob fetches the student's own answer image blob", async () => { + const blob = new Blob([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], { type: "image/png" }); + mockFetchBlob(blob); + + const result = await getExamAnswerImageBlob("ex1", "q1"); + expect(result).toBeInstanceOf(Blob); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/exams/ex1/attempt/answers/q1/image"); + expect(init?.method ?? "GET").toBe("GET"); + expect(init?.credentials).toBe("same-origin"); + }); }); diff --git a/src/api/client/types.ts b/src/api/client/types.ts index 646b851..91d7d49 100644 --- a/src/api/client/types.ts +++ b/src/api/client/types.ts @@ -196,6 +196,7 @@ export type AttemptAnswer = { selected?: number | null; text?: string | null; updated_at?: number; + answer_image?: ImageMeta | null; }; export type AttemptQuestionResponse = { @@ -303,6 +304,7 @@ export type StudentAnswer = { text: string | null; updated_at: number; is_correct: boolean | null; + answer_image?: ImageMeta | null; }; export type StudentAnswerSheet = { diff --git a/src/api/exams/deleteExamAttemptAnswerImage.ts b/src/api/exams/deleteExamAttemptAnswerImage.ts new file mode 100644 index 0000000..dfe9979 --- /dev/null +++ b/src/api/exams/deleteExamAttemptAnswerImage.ts @@ -0,0 +1,5 @@ +import { client } from "../client"; + +export function deleteExamAttemptAnswerImage(examId: string, questionId: string): Promise { + return client(`/exams/${examId}/attempt/answers/${questionId}/image`, { method: "DELETE" }); +} diff --git a/src/api/exams/getExamAnswerImageBlob.ts b/src/api/exams/getExamAnswerImageBlob.ts new file mode 100644 index 0000000..4cbbf22 --- /dev/null +++ b/src/api/exams/getExamAnswerImageBlob.ts @@ -0,0 +1,5 @@ +import { blobClient } from "../client"; + +export async function getExamAnswerImageBlob(examId: string, questionId: string, signal?: AbortSignal): Promise { + return blobClient(`/exams/${examId}/attempt/answers/${questionId}/image`, signal); +} diff --git a/src/api/exams/getExamQuestionImageBlob.ts b/src/api/exams/getExamQuestionImageBlob.ts new file mode 100644 index 0000000..e7d3945 --- /dev/null +++ b/src/api/exams/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/exams/index.ts b/src/api/exams/index.ts index 2b42ac6..80a2511 100644 --- a/src/api/exams/index.ts +++ b/src/api/exams/index.ts @@ -7,6 +7,7 @@ export * from "./postExamQuestion"; export * from "./patchExamQuestionById"; export * from "./deleteExamQuestionById"; export * from "./postExamQuestionImage"; +export * from "./getExamQuestionImageBlob"; export * from "./deleteExamQuestionImage"; export * from "./postExamChoiceImage"; export * from "./deleteExamChoiceImage"; @@ -23,3 +24,6 @@ export * from "./postExamAttempt"; export * from "./postExamAttemptFinish"; export * from "./getExamAttemptQuestions"; export * from "./postExamAttemptAnswer"; +export * from "./postExamAttemptAnswerImage"; +export * from "./deleteExamAttemptAnswerImage"; +export * from "./getExamAnswerImageBlob"; diff --git a/src/api/exams/postExamAttemptAnswerImage.ts b/src/api/exams/postExamAttemptAnswerImage.ts new file mode 100644 index 0000000..5d5a6a4 --- /dev/null +++ b/src/api/exams/postExamAttemptAnswerImage.ts @@ -0,0 +1,8 @@ +import { formClient } from "../client"; +import type { ImageMeta } from "../client"; + +export function postExamAttemptAnswerImage(examId: string, questionId: string, file: File, signal?: AbortSignal): Promise { + const body = new FormData(); + body.append("file", file); + return formClient(`/exams/${examId}/attempt/answers/${questionId}/image`, body, signal); +} diff --git a/src/components/exams/answer-sheet-view.tsx b/src/components/exams/answer-sheet-view.tsx index 2860c36..b1783db 100644 --- a/src/components/exams/answer-sheet-view.tsx +++ b/src/components/exams/answer-sheet-view.tsx @@ -81,6 +81,13 @@ export function AnswerSheetView(props: { examId: string; userId: string }) {

{t("exams.textAnswer")}

{row.answer?.text || "—"}

+ + {t("exams.drawAnswer")} +
} > diff --git a/src/components/exams/exam-room-ws.tsx b/src/components/exams/exam-room-ws.tsx index 5d833ed..452bc2a 100644 --- a/src/components/exams/exam-room-ws.tsx +++ b/src/components/exams/exam-room-ws.tsx @@ -1,15 +1,20 @@ -import { For, Match, Show, Suspense, Switch, createEffect, createMemo, createSignal, onCleanup } from "solid-js"; +import { For, Match, Show, Suspense, Switch, createEffect, createMemo, createSignal, lazy, onCleanup } from "solid-js"; import { getExamAttempt } from "@/api/exams"; import { getExamAttemptQuestions } from "@/api/exams"; import { postExamAttempt } from "@/api/exams"; import { postExamAttemptAnswer } from "@/api/exams"; import { postExamAttemptFinish } from "@/api/exams"; +import { postExamAttemptAnswerImage } from "@/api/exams"; +import { deleteExamAttemptAnswerImage } from "@/api/exams"; +import { getExamAnswerImageBlob } from "@/api/exams"; import { formatApiError, formatApiErrorMessage } from "@/api/client"; import type { AttemptQuestion, Exam, ExamAttempt } from "@/api/client"; +import type { DrawScene } from "@/lib/draw-stroke"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { ConfirmDialog } from "@/components/ui/confirm-dialog"; -import { IconAlert, IconChevronLeft, IconChevronRight } from "@/components/ui/icons"; +import { FormDialog } from "@/components/ui/form-dialog"; +import { IconAlert, IconChevronLeft, IconChevronRight, IconEdit, IconTrash } from "@/components/ui/icons"; import { PageSpinner } from "@/components/ui/page-spinner"; import { Textarea } from "@/components/ui/textarea"; import { cn } from "@/lib/cn"; @@ -17,6 +22,9 @@ import { createNow } from "@/lib/create-now"; import { formatDateTime } from "@/lib/format"; import { usePreferences, useT } from "@/stores/preferences-context"; +// Lazy so the drawing pad rides its own chunk, off the exam room's initial load. +const DrawCanvas = lazy(() => import("@/components/ui/draw-canvas").then((m) => ({ default: m.DrawCanvas }))); + type WsState = "connecting" | "connected" | "disconnected"; type WsMessage = | { type: "state"; status: string; deadline: number | null; remaining_ms: number | null; now: number; answered: number; question_count: number } @@ -207,6 +215,41 @@ export function ExamRoomWS(props: { exam: Exam }) { } }; + // A drawing is binary, so it skips the JSON WS/REST autosave: upload out-of-band, + // then refetch so the answer's answer_image and updated_at badge catch up. + const refreshAnswers = async () => { + const next = await getExamAttempt(props.exam.id); + setAttempt(next); + const qs = await getExamAttemptQuestions(props.exam.id); + setQuestions(qs); + }; + + const saveAnswerImage = async (question: AttemptQuestion, file: File) => { + setError(""); + setPending(true); + try { + await postExamAttemptAnswerImage(props.exam.id, question.id, file); + await refreshAnswers(); + } catch (err) { + setError(formatApiError(err, locale())); + } finally { + setPending(false); + } + }; + + const removeAnswerImage = async (question: AttemptQuestion) => { + setError(""); + setPending(true); + try { + await deleteExamAttemptAnswerImage(props.exam.id, question.id); + await refreshAnswers(); + } catch (err) { + setError(formatApiError(err, locale())); + } finally { + setPending(false); + } + }; + const finish = async () => { setPending(true); setError(""); @@ -359,6 +402,8 @@ export function ExamRoomWS(props: { exam: Exam }) { disabled={!canWrite() || pending()} onSave={(value) => saveAnswer(question(), value)} onSaved={goNext} + onSaveImage={(file) => saveAnswerImage(question(), file)} + onRemoveImage={() => removeAnswerImage(question())} />