🖼️ let question and solution images be replaced or removed
Browse files at this commitf923bbf2c8181c94b51aa60a53ce2f40fe81eb51Parent: 7f602db- feat
- 🖼️ let question and solution images be replaced or removed
6 changed files +164 -24
- src/api/__tests__/questions/questions.test.ts +23 -1
- src/api/__tests__/questions/solutions.test.ts +23 -1
- src/api/shared/questions.ts +14 -5
- src/api/shared/solutions.ts +14 -5
- src/pages/bank-question-detail-page.tsx +54 -6
- src/pages/question-detail-page.tsx +36 -6
src/api/__tests__/questions/questions.test.tsmodified+23 -1
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | -import { getQuestions, getQuestionById, postQuestion, postQuestionApprove, deleteQuestionById, getQuestionImageUrl, getQuestionImageBlob } from "../../shared"; | |
| 2 | +import { getQuestions, getQuestionById, postQuestion, postQuestionApprove, deleteQuestionById, getQuestionImageUrl, getQuestionImageBlob, postQuestionImage, deleteQuestionImage } from "../../shared"; | |
| 3 | 3 | import { lastFetchCall, mockFetch204, mockFetchBlob, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 4 | 4 | |
| 5 | 5 | describe("questions API", () => { |
| @@ -82,4 +82,26 @@ describe("questions API", () => { | ||
| 82 | 82 | const [url] = lastFetchCall(); |
| 83 | 83 | expect(url).toBe(getQuestionImageUrl("q1")); |
| 84 | 84 | }); |
| 85 | + | |
| 86 | + it("postQuestionImage uploads multipart to /questions/:id/image", async () => { | |
| 87 | + mockFetch204(); | |
| 88 | + | |
| 89 | + await postQuestionImage("q1", new File(["x"], "sketch.png", { type: "image/png" })); | |
| 90 | + | |
| 91 | + const [url, init] = lastFetchCall(); | |
| 92 | + expect(url).toBe("/api/questions/q1/image"); | |
| 93 | + expect(init?.method).toBe("POST"); | |
| 94 | + expect(init?.body).toBeInstanceOf(FormData); | |
| 95 | + expect((init?.body as FormData).get("file")).toBeInstanceOf(File); | |
| 96 | + }); | |
| 97 | + | |
| 98 | + it("deleteQuestionImage calls /questions/:id/image", async () => { | |
| 99 | + mockFetch204(); | |
| 100 | + | |
| 101 | + await deleteQuestionImage("q1"); | |
| 102 | + | |
| 103 | + const [url, init] = lastFetchCall(); | |
| 104 | + expect(url).toBe("/api/questions/q1/image"); | |
| 105 | + expect(init?.method).toBe("DELETE"); | |
| 106 | + }); | |
| 85 | 107 | }); |
src/api/__tests__/questions/solutions.test.tsmodified+23 -1
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | -import { getSolutions, postSolution, patchSolutionById, deleteSolutionById, getSolutionImageUrl, getSolutionImageBlob } from "../../shared"; | |
| 2 | +import { getSolutions, postSolution, patchSolutionById, deleteSolutionById, getSolutionImageUrl, getSolutionImageBlob, postSolutionImage, deleteSolutionImage } from "../../shared"; | |
| 3 | 3 | import { lastFetchCall, mockFetch204, mockFetchBlob, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 4 | 4 | |
| 5 | 5 | describe("solutions API", () => { |
| @@ -71,4 +71,26 @@ describe("solutions API", () => { | ||
| 71 | 71 | const [url] = lastFetchCall(); |
| 72 | 72 | expect(url).toBe(getSolutionImageUrl("q1", "s1")); |
| 73 | 73 | }); |
| 74 | + | |
| 75 | + it("postSolutionImage uploads multipart to /questions/:id/solutions/:sid/image", async () => { | |
| 76 | + mockFetch204(); | |
| 77 | + | |
| 78 | + await postSolutionImage("q1", "s1", new File(["x"], "work.png", { type: "image/png" })); | |
| 79 | + | |
| 80 | + const [url, init] = lastFetchCall(); | |
| 81 | + expect(url).toBe("/api/questions/q1/solutions/s1/image"); | |
| 82 | + expect(init?.method).toBe("POST"); | |
| 83 | + expect(init?.body).toBeInstanceOf(FormData); | |
| 84 | + expect((init?.body as FormData).get("file")).toBeInstanceOf(File); | |
| 85 | + }); | |
| 86 | + | |
| 87 | + it("deleteSolutionImage calls /questions/:id/solutions/:sid/image", async () => { | |
| 88 | + mockFetch204(); | |
| 89 | + | |
| 90 | + await deleteSolutionImage("q1", "s1"); | |
| 91 | + | |
| 92 | + const [url, init] = lastFetchCall(); | |
| 93 | + expect(url).toBe("/api/questions/q1/solutions/s1/image"); | |
| 94 | + expect(init?.method).toBe("DELETE"); | |
| 95 | + }); | |
| 74 | 96 | }); |
src/api/shared/questions.tsmodified+14 -5
| @@ -50,15 +50,24 @@ export async function postQuestion(body: AskQuestion, image?: File): Promise<Poo | ||
| 50 | 50 | body, |
| 51 | 51 | }); |
| 52 | 52 | |
| 53 | - if (image) { | |
| 54 | - const fd = new FormData(); | |
| 55 | - fd.append("file", image); | |
| 56 | - await formClient(`/questions/${res.id}/image`, fd); | |
| 57 | - } | |
| 53 | + if (image) await postQuestionImage(res.id, image); | |
| 58 | 54 | |
| 59 | 55 | return res; |
| 60 | 56 | } |
| 61 | 57 | |
| 58 | +/** Attaches or replaces the question's single illustration. */ | |
| 59 | +export async function postQuestionImage(id: string, image: File): Promise<void> { | |
| 60 | + const fd = new FormData(); | |
| 61 | + fd.append("file", image); | |
| 62 | + await formClient(`/questions/${id}/image`, fd); | |
| 63 | +} | |
| 64 | + | |
| 65 | +export async function deleteQuestionImage(id: string): Promise<void> { | |
| 66 | + return client(`/questions/${id}/image`, { | |
| 67 | + method: "DELETE", | |
| 68 | + }); | |
| 69 | +} | |
| 70 | + | |
| 62 | 71 | export async function postQuestionApprove(id: string): Promise<PoolQuestionResponse> { |
| 63 | 72 | return client(`/questions/${id}/approve`, { |
| 64 | 73 | method: "POST", |
src/api/shared/solutions.tsmodified+14 -5
| @@ -41,15 +41,24 @@ export async function postSolution( | ||
| 41 | 41 | body, |
| 42 | 42 | }); |
| 43 | 43 | |
| 44 | - if (image) { | |
| 45 | - const fd = new FormData(); | |
| 46 | - fd.append("file", image); | |
| 47 | - await formClient(`/questions/${id}/solutions/${res.id}/image`, fd); | |
| 48 | - } | |
| 44 | + if (image) await postSolutionImage(id, res.id, image); | |
| 49 | 45 | |
| 50 | 46 | return res; |
| 51 | 47 | } |
| 52 | 48 | |
| 49 | +/** Attaches or replaces the solution's single attachment. */ | |
| 50 | +export async function postSolutionImage(id: string, sid: string, image: File): Promise<void> { | |
| 51 | + const fd = new FormData(); | |
| 52 | + fd.append("file", image); | |
| 53 | + await formClient(`/questions/${id}/solutions/${sid}/image`, fd); | |
| 54 | +} | |
| 55 | + | |
| 56 | +export async function deleteSolutionImage(id: string, sid: string): Promise<void> { | |
| 57 | + return client(`/questions/${id}/solutions/${sid}/image`, { | |
| 58 | + method: "DELETE", | |
| 59 | + }); | |
| 60 | +} | |
| 61 | + | |
| 53 | 62 | export async function patchSolutionById( |
| 54 | 63 | id: string, |
| 55 | 64 | sid: string, |
src/pages/bank-question-detail-page.tsxmodified+54 -6
| @@ -1,6 +1,11 @@ | ||
| 1 | 1 | import { Link, useLocation, useNavigate, useParams } from "@tanstack/solid-router"; |
| 2 | 2 | import { For, Show, Suspense, createMemo, createResource, createSignal } from "solid-js"; |
| 3 | -import { deleteBankQuestionById, getBankQuestionById } from "@/api/bank-questions"; | |
| 3 | +import { | |
| 4 | + deleteBankChoiceImage, | |
| 5 | + deleteBankQuestionById, | |
| 6 | + deleteBankQuestionImage, | |
| 7 | + getBankQuestionById, | |
| 8 | +} from "@/api/bank-questions"; | |
| 4 | 9 | import { getCourses } from "@/api/courses"; |
| 5 | 10 | import { formatApiError } from "@/api/client"; |
| 6 | 11 | import { BankQuestionForm } from "@/components/exams/bank-question-form"; |
| @@ -55,6 +60,23 @@ function BankQuestionDetailContent() { | ||
| 55 | 60 | const [error, setError] = createSignal(""); |
| 56 | 61 | const canEdit = () => question()?.owner === auth.user()?.id || hasMinRole(auth.user()?.role, "admin"); |
| 57 | 62 | |
| 63 | + // The form can attach a picture but never drop one, so removal lives here | |
| 64 | + // beside the image it deletes — same owner/admin gate as every other edit. | |
| 65 | + const [imagePending, setImagePending] = createSignal(""); | |
| 66 | + const removeImage = async (key: string, remove: () => Promise<void>) => { | |
| 67 | + if (imagePending()) return; | |
| 68 | + setError(""); | |
| 69 | + setImagePending(key); | |
| 70 | + try { | |
| 71 | + await remove(); | |
| 72 | + await refetch(); | |
| 73 | + } catch (err) { | |
| 74 | + setError(formatApiError(err)); | |
| 75 | + } finally { | |
| 76 | + setImagePending(""); | |
| 77 | + } | |
| 78 | + }; | |
| 79 | + | |
| 58 | 80 | return ( |
| 59 | 81 | <Suspense fallback={<PageSpinner />}> |
| 60 | 82 | <Show |
| @@ -109,11 +131,25 @@ function BankQuestionDetailContent() { | ||
| 109 | 131 | </div> |
| 110 | 132 | |
| 111 | 133 | <Show when={current().image}> |
| 112 | - <img | |
| 113 | - src={`/api/bank-questions/${current().id}/image`} | |
| 114 | - alt={t("questions.image")} | |
| 115 | - class="max-h-80 rounded-lg border object-contain" | |
| 116 | - /> | |
| 134 | + <div class="space-y-2"> | |
| 135 | + <img | |
| 136 | + src={`/api/bank-questions/${current().id}/image`} | |
| 137 | + alt={t("questions.image")} | |
| 138 | + class="max-h-80 rounded-lg border object-contain" | |
| 139 | + /> | |
| 140 | + <Show when={canEdit()}> | |
| 141 | + <Button | |
| 142 | + variant="outline" | |
| 143 | + size="sm" | |
| 144 | + class="text-destructive hover:text-destructive" | |
| 145 | + disabled={imagePending() === "question"} | |
| 146 | + onClick={() => void removeImage("question", () => deleteBankQuestionImage(current().id))} | |
| 147 | + > | |
| 148 | + <IconTrash class="h-4 w-4" /> | |
| 149 | + {t("common.remove")} | |
| 150 | + </Button> | |
| 151 | + </Show> | |
| 152 | + </div> | |
| 117 | 153 | </Show> |
| 118 | 154 | |
| 119 | 155 | <Show when={current().choices?.length}> |
| @@ -130,6 +166,18 @@ function BankQuestionDetailContent() { | ||
| 130 | 166 | alt="" |
| 131 | 167 | class="h-12 w-12 rounded-md border object-cover" |
| 132 | 168 | /> |
| 169 | + <Show when={canEdit()}> | |
| 170 | + <Button | |
| 171 | + variant="ghost" | |
| 172 | + size="icon" | |
| 173 | + class="h-8 w-8 text-destructive hover:bg-destructive/10" | |
| 174 | + aria-label={t("common.remove")} | |
| 175 | + disabled={imagePending() === choice.id} | |
| 176 | + onClick={() => void removeImage(choice.id, () => deleteBankChoiceImage(current().id, choice.id))} | |
| 177 | + > | |
| 178 | + <IconTrash class="h-4 w-4" /> | |
| 179 | + </Button> | |
| 180 | + </Show> | |
| 133 | 181 | </Show> |
| 134 | 182 | </div> |
| 135 | 183 | )} |
src/pages/question-detail-page.tsxmodified+36 -6
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | import { For, Show, Suspense, createResource, createSignal, lazy } from "solid-js"; |
| 2 | 2 | import { useParams, useRouter } from "@tanstack/solid-router"; |
| 3 | 3 | import { getQuestionById, deleteQuestionById, postQuestionApprove, getQuestionImageUrl, getQuestionImageBlob } from "@/api/shared"; |
| 4 | -import { getSolutions, postSolution, patchSolutionById, deleteSolutionById, getSolutionImageUrl, getSolutionImageBlob } from "@/api/shared"; | |
| 4 | +import { getSolutions, postSolution, patchSolutionById, deleteSolutionById, getSolutionImageUrl, getSolutionImageBlob, postSolutionImage, deleteSolutionImage } from "@/api/shared"; | |
| 5 | 5 | import { getSettings } from "@/api/settings"; |
| 6 | 6 | import type { SolutionResponse } from "@/api/shared"; |
| 7 | 7 | import { formatApiError } from "@/api/client"; |
| @@ -254,7 +254,10 @@ function SolutionFormDialog(props: { questionId: string; initialData?: SolutionR | ||
| 254 | 254 | const [error, setError] = createSignal(""); |
| 255 | 255 | const [body, setBody] = createSignal(props.initialData?.body || ""); |
| 256 | 256 | const [file, setFile] = createSignal<File | null>(null); |
| 257 | + const [removeImage, setRemoveImage] = createSignal(false); | |
| 257 | 258 | const [drawing, setDrawing] = createSignal(false); |
| 259 | + /** The already-uploaded attachment, until it is dropped or superseded. */ | |
| 260 | + const keptImage = () => Boolean(props.initialData?.image) && !removeImage() && !file(); | |
| 258 | 261 | const [settings] = createResource(async () => { |
| 259 | 262 | try { |
| 260 | 263 | return await getSettings(); |
| @@ -283,7 +286,13 @@ function SolutionFormDialog(props: { questionId: string; initialData?: SolutionR | ||
| 283 | 286 | setError(""); |
| 284 | 287 | try { |
| 285 | 288 | if (props.initialData) { |
| 286 | - await patchSolutionById(props.questionId, props.initialData.id, { body: body().trim() }); | |
| 289 | + const solutionId = props.initialData.id; | |
| 290 | + await patchSolutionById(props.questionId, solutionId, { body: body().trim() }); | |
| 291 | + // A re-upload replaces the stored image, so only an untouched picker | |
| 292 | + // with the remove flag set needs the explicit DELETE. | |
| 293 | + const picked = file(); | |
| 294 | + if (picked) await postSolutionImage(props.questionId, solutionId, picked); | |
| 295 | + else if (removeImage()) await deleteSolutionImage(props.questionId, solutionId); | |
| 287 | 296 | } else { |
| 288 | 297 | await postSolution(props.questionId, { body: body().trim() }, file() || undefined); |
| 289 | 298 | } |
| @@ -301,9 +310,31 @@ function SolutionFormDialog(props: { questionId: string; initialData?: SolutionR | ||
| 301 | 310 | <Label for="s-body">{t("pool.body")}</Label> |
| 302 | 311 | <Textarea id="s-body" value={body()} onInput={(e) => setBody(e.currentTarget.value)} required rows={5} /> |
| 303 | 312 | </div> |
| 304 | - <Show when={!props.initialData}> | |
| 305 | - <div class="space-y-2"> | |
| 313 | + <div class="space-y-2"> | |
| 306 | 314 | <Label>{t("pool.image")}</Label> |
| 315 | + {/* Editing keeps the attachment editable too: the backend replaces | |
| 316 | + the image on re-upload and drops it on DELETE, so the form must | |
| 317 | + offer both rather than silently keeping whatever was there. */} | |
| 318 | + <Show when={keptImage()}> | |
| 319 | + <div class="flex items-center gap-3 rounded-lg border border-border p-2"> | |
| 320 | + <ReplayableImage | |
| 321 | + fetchBlob={() => getSolutionImageBlob(props.questionId, props.initialData!.id)} | |
| 322 | + src={getSolutionImageUrl(props.questionId, props.initialData!.id)} | |
| 323 | + alt="" | |
| 324 | + imgClass="max-h-20 w-auto rounded object-contain" | |
| 325 | + /> | |
| 326 | + <Button | |
| 327 | + type="button" | |
| 328 | + variant="ghost" | |
| 329 | + size="sm" | |
| 330 | + class="ml-auto text-destructive hover:bg-destructive/10" | |
| 331 | + onClick={() => setRemoveImage(true)} | |
| 332 | + > | |
| 333 | + <IconX class="mr-1 h-4 w-4" /> | |
| 334 | + {t("common.remove")} | |
| 335 | + </Button> | |
| 336 | + </div> | |
| 337 | + </Show> | |
| 307 | 338 | <div class="relative flex flex-col items-center justify-center rounded-lg border-2 border-dashed border-border p-6 transition-colors hover:bg-muted/50 focus-within:ring-2 focus-within:ring-primary focus-within:ring-offset-2"> |
| 308 | 339 | <Show |
| 309 | 340 | when={!file()} |
| @@ -362,8 +393,7 @@ function SolutionFormDialog(props: { questionId: string; initialData?: SolutionR | ||
| 362 | 393 | /> |
| 363 | 394 | </Suspense> |
| 364 | 395 | </Show> |
| 365 | - </div> | |
| 366 | - </Show> | |
| 396 | + </div> | |
| 367 | 397 | <div class="flex justify-end gap-3 pt-4"> |
| 368 | 398 | <Button type="button" variant="outline" onClick={props.onClose}> |
| 369 | 399 | {t("common.cancel")} |