From 4d568179bd4b711b16828bce37657efd4639b479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Sat, 19 Sep 2026 21:14:56 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20share=20exams=20with=20sibl?= =?UTF-8?q?ing=20sections,=20stop=20empty-id=20subject=20reads=20-=20exam?= =?UTF-8?q?=20audience=20endpoints=20return=20the=20audience=20array=20the?= =?UTF-8?q?=20API=20sends=20=20=20(POST/DELETE=20answered=20as=20a=20singl?= =?UTF-8?q?e=20row=20/=20void=20before),=20tests=20updated=20-=20new=20"Pa?= =?UTF-8?q?yla=C5=9F=C4=B1lan=20=C5=9Fubeler"=20tab=20on=20exam=20detail:?= =?UTF-8?q?=20list=20the=20audience,=20share=20with=20=20=20a=20section=20?= =?UTF-8?q?teaching=20the=20same=20course=20in=20the=20same=20academic=20y?= =?UTF-8?q?ear,=20withdraw=20one=20-=20ExamQuestionsPanel=20takes=20a=20nu?= =?UTF-8?q?llable=20courseId=20and=20skips=20the=20subjects=20read=20=20?= =?UTF-8?q?=20until=20it=20resolves;=20Solid=20fetched=20`/courses//subjec?= =?UTF-8?q?ts`=20on=20the=20empty=20string=20-=20a=20failed=20subjects=20r?= =?UTF-8?q?ead=20no=20longer=20throws=20into=20the=20page?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/__tests__/exams/exams.test.ts | 30 ++- .../exams/deleteExamAudienceByInstanceId.ts | 6 +- src/api/exams/getExamAudience.ts | 15 +- src/api/exams/postExamAudience.ts | 7 +- src/components/exams/exam-audience-panel.tsx | 239 ++++++++++++++++++ src/components/exams/exam-questions-panel.tsx | 17 +- src/pages/exam-detail-page.tsx | 15 +- src/pages/exams-page.tsx | 4 +- 8 files changed, 302 insertions(+), 31 deletions(-) create mode 100644 src/components/exams/exam-audience-panel.tsx diff --git a/src/api/__tests__/exams/exams.test.ts b/src/api/__tests__/exams/exams.test.ts index 9a88f80..2414de5 100644 --- a/src/api/__tests__/exams/exams.test.ts +++ b/src/api/__tests__/exams/exams.test.ts @@ -146,34 +146,42 @@ describe("exams API - core", () => { expect(init?.method ?? "GET").toBe("GET"); expect(init?.credentials).toBe("same-origin"); }); - it("getExamAudience calls /exams/:id/audience", async () => { - mockFetchSuccess({ items: [], total: 0 }); + it("getExamAudience reads the whole audience array", async () => { + mockFetchSuccess([{ instance: "i1", class: "c1", course: "co1" }]); - await getExamAudience("x1", { limit: 10 }); + const audience = await getExamAudience("x1"); - const [url] = lastFetchCall(); - expect(url).toBe("/api/exams/x1/audience?limit=10"); + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/exams/x1/audience"); + expect(init?.method ?? "GET").toBe("GET"); + expect(audience).toEqual([{ instance: "i1", class: "c1", course: "co1" }]); }); - it("postExamAudience announces the exam to another instance", async () => { - mockFetchSuccess({ instance: "i2", class: "c2", course: "co1" }); + it("postExamAudience announces the exam and returns the audience after the write", async () => { + const after = [ + { instance: "i1", class: "c1", course: "co1" }, + { instance: "i2", class: "c2", course: "co1" }, + ]; + mockFetchSuccess(after); - await postExamAudience("x1", "i2"); + const audience = await postExamAudience("x1", "i2"); const [url, init] = lastFetchCall(); expect(url).toBe("/api/exams/x1/audience"); expect(init?.method).toBe("POST"); expect(init?.body).toBe(JSON.stringify({ instance: "i2" })); + expect(audience).toEqual(after); }); - it("deleteExamAudienceByInstanceId calls /exams/:id/audience/:instance", async () => { - mockFetch204(); + it("deleteExamAudienceByInstanceId withdraws one instance and returns what is left", async () => { + mockFetchSuccess([{ instance: "i1", class: "c1", course: "co1" }]); - await deleteExamAudienceByInstanceId("x1", "i2"); + const audience = await deleteExamAudienceByInstanceId("x1", "i2"); const [url, init] = lastFetchCall(); expect(url).toBe("/api/exams/x1/audience/i2"); expect(init?.method).toBe("DELETE"); + expect(audience).toHaveLength(1); }); }); diff --git a/src/api/exams/deleteExamAudienceByInstanceId.ts b/src/api/exams/deleteExamAudienceByInstanceId.ts index 6c9105f..4e54d6c 100644 --- a/src/api/exams/deleteExamAudienceByInstanceId.ts +++ b/src/api/exams/deleteExamAudienceByInstanceId.ts @@ -1,5 +1,7 @@ import { client } from "../client"; +import type { ExamAudience } from "../client"; -export function deleteExamAudienceByInstanceId(examId: string, instanceId: string): Promise { - return client(`/exams/${examId}/audience/${instanceId}`, { method: "DELETE" }); +/** Withdraw the exam from one instance. Answers the audience left behind; the owner is not withdrawable (400). */ +export function deleteExamAudienceByInstanceId(examId: string, instanceId: string): Promise { + return client(`/exams/${examId}/audience/${instanceId}`, { method: "DELETE" }); } diff --git a/src/api/exams/getExamAudience.ts b/src/api/exams/getExamAudience.ts index 130ad1a..87fc7d7 100644 --- a/src/api/exams/getExamAudience.ts +++ b/src/api/exams/getExamAudience.ts @@ -1,13 +1,10 @@ import { client } from "../client"; -import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; import type { ExamAudience } from "../client"; -/** The instances this exam is announced to beyond the one that owns it. */ -export async function getExamAudience( - examId: string, - params?: PageParams, - signal?: AbortSignal, -): Promise> { - const data = await client(`/exams/${examId}/audience${pageQuery(params)}`, { signal }); - return normalizePage(data); +/** + * Every instance the exam is announced to, its owner first, then in + * announcement order. A plain array — the door takes no paging. + */ +export function getExamAudience(examId: string, signal?: AbortSignal): Promise { + return client(`/exams/${examId}/audience`, { signal }); } diff --git a/src/api/exams/postExamAudience.ts b/src/api/exams/postExamAudience.ts index dda4737..dfa3165 100644 --- a/src/api/exams/postExamAudience.ts +++ b/src/api/exams/postExamAudience.ts @@ -4,10 +4,11 @@ import type { ExamAudience } from "../client"; /** * Announce the exam to another instance. It must teach the exam's own catalog * course, sit under the same academic year, and be a different instance — the - * owner is refused with a 400. + * owner is refused with a 400. Answers the whole audience after the write; + * re-announcing a standing pair is a no-op. */ -export function postExamAudience(examId: string, instanceId: string): Promise { - return client(`/exams/${examId}/audience`, { +export function postExamAudience(examId: string, instanceId: string): Promise { + return client(`/exams/${examId}/audience`, { method: "POST", body: { instance: instanceId }, }); diff --git a/src/components/exams/exam-audience-panel.tsx b/src/components/exams/exam-audience-panel.tsx new file mode 100644 index 0000000..bc37f25 --- /dev/null +++ b/src/components/exams/exam-audience-panel.tsx @@ -0,0 +1,239 @@ +import { Show, Suspense, createMemo, createSignal } from "solid-js"; +import type { ColumnDef } from "@tanstack/solid-table"; +import { createResource } from "@/lib/create-resource"; +import { getClasses, getClassInstances } from "@/api/classes"; +import { deleteExamAudienceByInstanceId, getExamAudience, postExamAudience } from "@/api/exams"; +import { formatApiError, type ClassGroup, type ExamAudience } from "@/api/client"; +import { Alert } from "@/components/ui/alert"; +import { Badge } from "@/components/ui/badge"; +import { Button } from "@/components/ui/button"; +import { ConfirmDialog } from "@/components/ui/confirm-dialog"; +import { DataTable, DataTableSkeleton } from "@/components/ui/data-table"; +import { IconPlus, IconTrash } from "@/components/ui/icons"; +import { Label } from "@/components/ui/label"; +import { SearchableSelect } from "@/components/ui/searchable-select"; +import { SidePanel } from "@/components/ui/side-panel"; +import { TableRowActions } from "@/components/ui/table-row-actions"; +import { createFlash } from "@/lib/flash"; +import { useT } from "@/stores/preferences-context"; + +type AudienceRow = ExamAudience & { owner: boolean; label: string }; + +/** + * The sections a shared exam is announced to beyond its owner. Announcing puts + * the exam — and its marks — into the sibling section's lists and report card; + * the backend only accepts a section that teaches the same catalog course in + * the same academic year, so the picker offers exactly those. + */ +export function ExamAudiencePanel(props: { + examId: string; + /** The exam's own instance (`class_course`). */ + ownerInstanceId: string; + /** The catalog course the owner instance teaches; null while it resolves. */ + courseId: string | null; + /** Announce/withdraw rights; the list itself is readable either way. */ + canManage: boolean; +}) { + const t = useT(); + const [flash, setFlash] = createFlash(); + const [error, setError] = createSignal(""); + const [addOpen, setAddOpen] = createSignal(false); + const [selected, setSelected] = createSignal(""); + const [saving, setSaving] = createSignal(false); + const [removing, setRemoving] = createSignal(null); + + const [audience, { mutate }] = createResource(() => props.examId, (examId) => getExamAudience(examId)); + // Section names label both the table and the picker. Teacher+ reads the + // school's class list; one read covers every row. + const [classes] = createResource(async () => { + try { + return (await getClasses({ limit: 200 })).items; + } catch { + return [] as ClassGroup[]; + } + }); + const classById = createMemo(() => new Map((classes() ?? []).map((klass) => [klass.id, klass]))); + const className = (classId: string) => classById().get(classId)?.name ?? "—"; + + const rows = createMemo(() => + (audience() ?? []).map((row) => ({ + ...row, + owner: row.instance === props.ownerInstanceId, + label: className(row.class), + })), + ); + + // Candidates load only when the picker opens: one read per same-year şube. + const [candidates] = createResource( + () => (addOpen() && props.courseId && classes() ? props.courseId : null), + async (courseId) => { + const owner = (audience() ?? []).find((row) => row.instance === props.ownerInstanceId); + const ownerYear = owner ? classById().get(owner.class)?.year ?? null : null; + const siblings = (classes() ?? []).filter((klass) => klass.year === ownerYear); + const perClass = await Promise.all( + siblings.map(async (klass) => { + try { + return (await getClassInstances(klass.id, { limit: 200 })).items; + } catch { + return []; + } + }), + ); + return perClass + .flat() + .filter((instance) => instance.course === courseId) + .map((instance) => ({ value: instance.id, label: className(instance.class) })) + .sort((a, b) => a.label.localeCompare(b.label, "tr")); + }, + ); + const options = () => { + const taken = new Set((audience() ?? []).map((row) => row.instance)); + return (candidates() ?? []).filter((option) => !taken.has(option.value)); + }; + + const add = async () => { + const instanceId = selected(); + if (!instanceId || saving()) return; + setSaving(true); + setError(""); + try { + mutate(await postExamAudience(props.examId, instanceId)); + setSelected(""); + setAddOpen(false); + setFlash(t("exams.audience.added")); + } catch (err) { + setError(formatApiError(err)); + } finally { + setSaving(false); + } + }; + + const remove = async () => { + const row = removing(); + if (!row) return; + setError(""); + try { + mutate(await deleteExamAudienceByInstanceId(props.examId, row.instance)); + setFlash(t("exams.audience.removed")); + } catch (err) { + setError(formatApiError(err)); + } finally { + setRemoving(null); + } + }; + + const columns = createMemo[]>(() => [ + { + accessorKey: "label", + header: t("exams.audience.section"), + meta: { cellClass: "font-medium" }, + }, + { + id: "status", + header: t("exams.audience.status"), + cell: (cell) => ( + + {cell.row.original.owner ? t("exams.audience.owner") : t("exams.audience.shared")} + + ), + }, + ...(props.canManage + ? [ + { + id: "actions", + header: t("common.actions"), + meta: { + headerClass: "w-[110px] min-w-[110px] max-w-[110px] h-[45px] text-center whitespace-nowrap", + cellClass: "w-[110px] min-w-[110px] max-w-[110px] h-[45px] text-center whitespace-nowrap", + }, + cell: (cell) => ( + + , + destructive: true, + onSelect: () => setRemoving(cell.row.original), + }, + ]} + /> + + ), + } satisfies ColumnDef, + ] + : []), + ]); + + return ( +
+ + {flash()} + + + {error()} + + }> + {formatApiError(audience.error)}}> + + + + } + /> + + + + { + setAddOpen(open); + if (!open) setSelected(""); + }} + title={t("exams.audience.add")} + description={t("exams.audience.addHelp")} + > +
+ } + > + 0} fallback={

{t("exams.audience.noCandidates")}

}> +
+ + +
+
+ +
+
+
+
+
+ + !open && setRemoving(null)} + title={t("exams.audience.remove")} + summary={t("exams.audience.removeConfirm", { section: removing()?.label ?? "" })} + variant="destructive" + onConfirm={remove} + /> +
+ ); +} diff --git a/src/components/exams/exam-questions-panel.tsx b/src/components/exams/exam-questions-panel.tsx index a15f22f..51d7946 100644 --- a/src/components/exams/exam-questions-panel.tsx +++ b/src/components/exams/exam-questions-panel.tsx @@ -39,7 +39,8 @@ const QUESTION_PAGE_SIZE = 1; export function ExamQuestionsPanel(props: { examId: string; - courseId: string; + /** The exam's catalog course; null until the instance behind the exam resolves. */ + courseId: string | null; readOnly?: boolean; embedded?: boolean; createOpen?: boolean; @@ -50,7 +51,19 @@ export function ExamQuestionsPanel(props: { const isTeacherPlus = () => hasMinRole(auth.user()?.role, "teacher"); const [bankOpen, setBankOpen] = createSignal(false); const [banking, setBanking] = createSignal(""); - const [subjects] = createResource(() => props.courseId, async (courseId) => (await getCourseSubjects(courseId)).items); + // Solid only skips a fetch on null/undefined/false — an empty string would + // still ask for `/courses//subjects`. A subject list that cannot load only + // costs the name lookup (ids stay readable), so it must not throw into the page. + const [subjects] = createResource( + () => props.courseId || null, + async (courseId) => { + try { + return (await getCourseSubjects(courseId)).items; + } catch { + return []; + } + }, + ); const [questions, { refetch }] = createResource(() => props.examId, async (examId) => { try { return (await getExamQuestions(examId)).items; diff --git a/src/pages/exam-detail-page.tsx b/src/pages/exam-detail-page.tsx index 2ab65ba..3d9e5d3 100644 --- a/src/pages/exam-detail-page.tsx +++ b/src/pages/exam-detail-page.tsx @@ -17,6 +17,7 @@ import { postExamResult } from "@/api/exams"; import { ApiError, formatApiError } from "@/api/client"; import type { ExamResult } from "@/api/client"; import { ExamForm } from "@/components/exams/exam-form"; +import { ExamAudiencePanel } from "@/components/exams/exam-audience-panel"; import { ExamQuestionsPanel } from "@/components/exams/exam-questions-panel"; import { AnswerSheetView } from "@/components/exams/answer-sheet-view"; import { ExamResultBadge } from "@/components/exams/exam-result-badge"; @@ -30,7 +31,7 @@ import { Button } from "@/components/ui/button"; import { ConfirmDialog } from "@/components/ui/confirm-dialog"; import { DataTable, DataTableSkeleton } from "@/components/ui/data-table"; import { EmptyState } from "@/components/ui/empty-state"; -import { IconCalendarDays, IconChart, IconChevronDown, IconClipboardCheck, IconClock, IconEdit, IconExam, IconEye, IconRefresh, IconTrash } from "@/components/ui/icons"; +import { IconCalendarDays, IconChart, IconChevronDown, IconClipboardCheck, IconClock, IconEdit, IconExam, IconEye, IconRefresh, IconSchool, IconTrash } from "@/components/ui/icons"; import { Input } from "@/components/ui/input"; import { PaginationControls } from "@/components/ui/pagination-controls"; import { PageSpinner } from "@/components/ui/page-spinner"; @@ -663,6 +664,7 @@ function ExamDetailContent() { {t("exams.statistics")} {t("questions.title")} {t("exams.results")} + {t("exams.audience.tab")} @@ -693,7 +695,7 @@ function ExamDetailContent() { - + @@ -740,6 +742,15 @@ function ExamDetailContent() { + + + +
@@ -526,7 +526,7 @@ function ExamsContent() {