🎓 open the class the create envelope names
Browse files at this commita6c49741fac720c1b8b1a2fe7014861415aa3bfcParent: 896684b- fix
- 🎓 open the class the create envelope names
7 changed files +176 -13
- src/api/__tests__/classes/classes.test.ts +9 -4
- src/api/__tests__/contract/bank-questions.contract.test.ts +5 -2
- src/api/__tests__/contract/learning.contract.test.ts +5 -2
- src/api/classes/postClass.ts +6 -3
- src/api/client/types.ts +7 -0
- src/pages/classes-page.test.tsx +88 -0
- src/pages/classes-page.tsx +56 -2
src/api/__tests__/classes/classes.test.tsmodified+9 -4
| @@ -28,10 +28,15 @@ describe("classes API", () => { | |||
| 28 | vi.restoreAllMocks(); | 28 | vi.restoreAllMocks(); |
| 29 | }); | 29 | }); |
| 30 | 30 | ||
| 31 | - it("postClass POSTs /classes with the body", async () => { | 31 | + // POST /classes answers with an envelope, not the bare class: the created |
| 32 | - mockFetchSuccess({ id: "c1" }); | 32 | + // row plus the pairs its grade's blueprint could not attach. |
| 33 | - const body = { name: "9-A", grade: "9", term_id: "t1" }; | 33 | + it("postClass POSTs /classes and returns the create envelope", async () => { |
| 34 | - await postClass(body); | 34 | + const envelope = { class: { id: "c1" }, skipped: [], stocked_from: null }; |
| 35 | + mockFetchSuccess(envelope); | ||
| 36 | + const body = { name: "9-A", grade: "9", year: "y1" }; | ||
| 37 | + const created = await postClass(body); | ||
| 38 | + expect(created).toEqual(envelope); | ||
| 39 | + expect(created.class.id).toBe("c1"); | ||
| 35 | const [url, init] = lastFetchCall(); | 40 | const [url, init] = lastFetchCall(); |
| 36 | expect(url).toBe("/api/classes"); | 41 | expect(url).toBe("/api/classes"); |
| 37 | expect(init?.method).toBe("POST"); | 42 | expect(init?.method).toBe("POST"); |
src/api/__tests__/contract/bank-questions.contract.test.tsmodified+5 -2
| @@ -28,11 +28,14 @@ describe.skipIf(!isLive)(`bank-questions contract @ ${contractBaseUrl}`, () => { | |||
| 28 | body: { name: tag, year: yearId, starts_at: Date.now(), ends_at: Date.now() + 120 * 24 * 60 * 60 * 1000 }, | 28 | body: { name: tag, year: yearId, starts_at: Date.now(), ends_at: Date.now() + 120 * 24 * 60 * 60 * 1000 }, |
| 29 | }); | 29 | }); |
| 30 | termId = term.id; | 30 | termId = term.id; |
| 31 | - const klass = await json<{ id: string }>("/classes", { | 31 | + // POST /classes answers with a create envelope, not the bare şube: its |
| 32 | + // `class` is the row, and `skipped` is the pair report (empty here). | ||
| 33 | + const created = await json<{ class: { id: string }; skipped: unknown[] }>("/classes", { | ||
| 32 | method: "POST", | 34 | method: "POST", |
| 33 | body: { name: tag, grade: "9", year: yearId }, | 35 | body: { name: tag, grade: "9", year: yearId }, |
| 34 | }); | 36 | }); |
| 35 | - classId = klass.id; | 37 | + classId = created.class.id; |
| 38 | + expect(Array.isArray(created.skipped)).toBe(true); | ||
| 36 | 39 | ||
| 37 | const course = await json<Course>("/courses", { | 40 | const course = await json<Course>("/courses", { |
| 38 | method: "POST", | 41 | method: "POST", |
src/api/__tests__/contract/learning.contract.test.tsmodified+5 -2
| @@ -30,11 +30,14 @@ describe.skipIf(!isLive)(`learning contract @ ${contractBaseUrl}`, () => { | |||
| 30 | termId = term.id; | 30 | termId = term.id; |
| 31 | expect(term.year).toBe(yearId); | 31 | expect(term.year).toBe(yearId); |
| 32 | 32 | ||
| 33 | - const klass = await json<{ id: string }>("/classes", { | 33 | + // POST /classes answers with a create envelope, not the bare şube: its |
| 34 | + // `class` is the row, and `skipped` is the pair report (empty here). | ||
| 35 | + const created = await json<{ class: { id: string }; skipped: unknown[] }>("/classes", { | ||
| 34 | method: "POST", | 36 | method: "POST", |
| 35 | body: { name: tag, grade: "9", year: yearId }, | 37 | body: { name: tag, grade: "9", year: yearId }, |
| 36 | }); | 38 | }); |
| 37 | - classId = klass.id; | 39 | + classId = created.class.id; |
| 40 | + expect(Array.isArray(created.skipped)).toBe(true); | ||
| 38 | 41 | ||
| 39 | const course = await json<{ id: string; title: string; kind: string }>("/courses", { | 42 | const course = await json<{ id: string; title: string; kind: string }>("/courses", { |
| 40 | method: "POST", | 43 | method: "POST", |
src/api/classes/postClass.tsmodified+6 -3
| @@ -1,10 +1,13 @@ | |||
| 1 | import { client } from "../client"; | 1 | import { client } from "../client"; |
| 2 | -import type { ClassGroup } from "../client"; | 2 | +import type { CreateClassResponse } from "../client"; |
| 3 | 3 | ||
| 4 | // `year` is the academic year (`getAcademicYears`) — what binds the şube to a | 4 | // `year` is the academic year (`getAcademicYears`) — what binds the şube to a |
| 5 | // karne and to the rollover. A şube with no year takes no exam. | 5 | // karne and to the rollover. A şube with no year takes no exam. |
| 6 | export type CreateClassBody = { name: string; grade?: string; year?: string; teacher_id?: string }; | 6 | export type CreateClassBody = { name: string; grade?: string; year?: string; teacher_id?: string }; |
| 7 | 7 | ||
| 8 | -export function postClass(body: CreateClassBody): Promise<ClassGroup> { | 8 | +// The 201 is a create envelope, not the bare class: its `class` is what the |
| 9 | - return client<ClassGroup>("/classes", { method: "POST", body }); | 9 | +// route navigates to, and its `skipped` is the blueprint fallout the caller |
| 10 | +// has to report. | ||
| 11 | +export function postClass(body: CreateClassBody): Promise<CreateClassResponse> { | ||
| 12 | + return client<CreateClassResponse>("/classes", { method: "POST", body }); | ||
| 10 | } | 13 | } |
src/api/client/types.tsmodified+7 -0
| @@ -403,6 +403,13 @@ export type ClassGroup = { | |||
| 403 | teacher: PersonRef | null; | 403 | teacher: PersonRef | null; |
| 404 | }; | 404 | }; |
| 405 | 405 | ||
| 406 | +// What POST /classes answers with: the created class, the (class, course) | ||
| 407 | +// pairs its grade's blueprint could not attach, and the grade of the blueprint | ||
| 408 | +// that stocked it. `skipped` is empty when the template took every course, and | ||
| 409 | +// always empty when no template covered the grade; `stocked_from` is null in | ||
| 410 | +// that second case. | ||
| 411 | +export type CreateClassResponse = { class: ClassGroup; skipped: BlueprintSkip[]; stocked_from: string | null }; | ||
| 412 | + | ||
| 406 | export type ClassMember = { | 413 | export type ClassMember = { |
| 407 | id: string; | 414 | id: string; |
| 408 | class: string; | 415 | class: string; |
src/pages/classes-page.test.tsxadded+88 -0
| @@ -0,0 +1,88 @@ | |||
| 1 | +import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; | ||
| 2 | +import type { JSX } from "solid-js"; | ||
| 3 | +import { afterEach, expect, test, vi } from "vitest"; | ||
| 4 | +import ClassesPage from "@/pages/classes-page"; | ||
| 5 | +import { PreferencesProvider } from "@/stores/preferences-context"; | ||
| 6 | + | ||
| 7 | +const { getClasses, getClassMembers, postClass, getCourses, navigate } = vi.hoisted(() => ({ | ||
| 8 | + getClasses: vi.fn(), | ||
| 9 | + getClassMembers: vi.fn(), | ||
| 10 | + postClass: vi.fn(), | ||
| 11 | + getCourses: vi.fn(), | ||
| 12 | + navigate: vi.fn(), | ||
| 13 | +})); | ||
| 14 | + | ||
| 15 | +vi.mock("@tanstack/solid-router", () => ({ | ||
| 16 | + Link: (props: { to: string; children: JSX.Element; class?: string }) => <a href={props.to} class={props.class}>{props.children}</a>, | ||
| 17 | + Navigate: () => null, | ||
| 18 | + useNavigate: () => navigate, | ||
| 19 | +})); | ||
| 20 | +vi.mock("@/stores/auth-context", () => ({ | ||
| 21 | + useAuth: () => ({ | ||
| 22 | + user: () => ({ id: "u-1", username: "boss", role: "manager" }), | ||
| 23 | + loading: () => false, | ||
| 24 | + error: () => null, | ||
| 25 | + refresh: vi.fn(), | ||
| 26 | + }), | ||
| 27 | +})); | ||
| 28 | +vi.mock("@/api/classes", () => ({ getClasses, getClassMembers, postClass })); | ||
| 29 | +vi.mock("@/api/academic-years", () => ({ getAcademicYears: vi.fn(async () => ({ items: [] })) })); | ||
| 30 | +vi.mock("@/api/limits", () => ({ | ||
| 31 | + getLimits: vi.fn(async () => ({ course: { max_class_name_len: 60, max_class_grade_len: 10 } })), | ||
| 32 | +})); | ||
| 33 | +vi.mock("@/api/courses", () => ({ getCourses })); | ||
| 34 | +vi.mock("@/api/users", () => ({ getUserSearch: vi.fn(async () => ({ items: [] })) })); | ||
| 35 | + | ||
| 36 | +const createdClass = { id: "c-new", creator: null, name: "9-A", grade: "9", year: null, teacher: null }; | ||
| 37 | + | ||
| 38 | +afterEach(() => { | ||
| 39 | + cleanup(); | ||
| 40 | + vi.clearAllMocks(); | ||
| 41 | +}); | ||
| 42 | + | ||
| 43 | +async function openCreateForm() { | ||
| 44 | + getClasses.mockResolvedValue({ items: [] }); | ||
| 45 | + getClassMembers.mockResolvedValue({ items: [], total: 0 }); | ||
| 46 | + render(() => <PreferencesProvider><ClassesPage /></PreferencesProvider>); | ||
| 47 | + | ||
| 48 | + fireEvent.click(await screen.findByRole("button", { name: /New class/ })); | ||
| 49 | + fireEvent.input(await screen.findByLabelText(/^Name/), { target: { value: "9-A" } }); | ||
| 50 | + fireEvent.click(screen.getByRole("button", { name: "Create" })); | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +// The 201 is an envelope, not the class: opening the route with `undefined` | ||
| 54 | +// 404s the class it just made. | ||
| 55 | +test("creating a class opens the class the server made", async () => { | ||
| 56 | + postClass.mockResolvedValue({ class: createdClass, skipped: [], stocked_from: null }); | ||
| 57 | + | ||
| 58 | + await openCreateForm(); | ||
| 59 | + | ||
| 60 | + await waitFor(() => | ||
| 61 | + expect(navigate).toHaveBeenCalledWith({ to: "/management/classes/$id", params: { id: "c-new" } }), | ||
| 62 | + ); | ||
| 63 | + expect(postClass).toHaveBeenCalledWith({ name: "9-A", grade: undefined, year: undefined, teacher_id: undefined }); | ||
| 64 | +}); | ||
| 65 | + | ||
| 66 | +test("a create whose blueprint left pairs behind holds the page until the report closes", async () => { | ||
| 67 | + postClass.mockResolvedValue({ | ||
| 68 | + class: createdClass, | ||
| 69 | + skipped: [{ class: "c-new", class_name: "9-A", course: "k1", reason: "the class is already at its course ceiling" }], | ||
| 70 | + stocked_from: "9", | ||
| 71 | + }); | ||
| 72 | + getCourses.mockResolvedValue({ items: [{ id: "k1", title: "Algebra" }] }); | ||
| 73 | + | ||
| 74 | + await openCreateForm(); | ||
| 75 | + | ||
| 76 | + expect(await screen.findByText(/could not be attached/)).toBeTruthy(); | ||
| 77 | + expect(navigate).not.toHaveBeenCalled(); | ||
| 78 | + | ||
| 79 | + fireEvent.click(screen.getByRole("button", { name: "See details" })); | ||
| 80 | + expect(await screen.findByText("Not attached")).toBeTruthy(); | ||
| 81 | + expect(await screen.findByText("Algebra")).toBeTruthy(); | ||
| 82 | + | ||
| 83 | + fireEvent.click(screen.getByRole("button", { name: "Close" })); | ||
| 84 | + | ||
| 85 | + await waitFor(() => | ||
| 86 | + expect(navigate).toHaveBeenCalledWith({ to: "/management/classes/$id", params: { id: "c-new" } }), | ||
| 87 | + ); | ||
| 88 | +}); | ||
src/pages/classes-page.tsxmodified+56 -2
| @@ -3,8 +3,10 @@ import { createResource } from "@/lib/create-resource"; | |||
| 3 | import { useNavigate } from "@tanstack/solid-router"; | 3 | import { useNavigate } from "@tanstack/solid-router"; |
| 4 | import { getClasses, getClassMembers, postClass } from "@/api/classes"; | 4 | import { getClasses, getClassMembers, postClass } from "@/api/classes"; |
| 5 | import { getAcademicYears } from "@/api/academic-years"; | 5 | import { getAcademicYears } from "@/api/academic-years"; |
| 6 | +import { getCourses } from "@/api/courses"; | ||
| 6 | import { getLimits } from "@/api/limits"; | 7 | import { getLimits } from "@/api/limits"; |
| 7 | -import { formatApiError, type ClassGroup } from "@/api/client"; | 8 | +import { formatApiError, type BlueprintSkip, type ClassGroup } from "@/api/client"; |
| 9 | +import { BlueprintSkippedReport } from "@/components/classes/blueprint-skipped-report"; | ||
| 8 | import { RouteGuard } from "@/components/layout/route-guard"; | 10 | import { RouteGuard } from "@/components/layout/route-guard"; |
| 9 | import { DataTableSearch } from "@/components/ui/data-table-search"; | 11 | import { DataTableSearch } from "@/components/ui/data-table-search"; |
| 10 | import { DataSection } from "@/components/ui/data-section"; | 12 | import { DataSection } from "@/components/ui/data-section"; |
| @@ -56,11 +58,18 @@ function ClassesContent() { | |||
| 56 | const [teacherId, setTeacherId] = createSignal(""); | 58 | const [teacherId, setTeacherId] = createSignal(""); |
| 57 | const [error, setError] = createSignal(""); | 59 | const [error, setError] = createSignal(""); |
| 58 | const [pending, setPending] = createSignal(false); | 60 | const [pending, setPending] = createSignal(false); |
| 61 | + // A create whose grade's blueprint left pairs behind reports them here before | ||
| 62 | + // it opens the class: the response is the only place those rows ever appear. | ||
| 63 | + const [createdClass, setCreatedClass] = createSignal<ClassGroup | null>(null); | ||
| 64 | + const [skipped, setSkipped] = createSignal<BlueprintSkip[]>([]); | ||
| 65 | + const [skippedCourseTitles, setSkippedCourseTitles] = createSignal<Record<string, string>>({}); | ||
| 66 | + const [reportOpen, setReportOpen] = createSignal(false); | ||
| 59 | const [flash, setFlash] = createFlash(); | 67 | const [flash, setFlash] = createFlash(); |
| 60 | 68 | ||
| 61 | const [years] = createResource(async () => (await getAcademicYears({ limit: 100 })).items); | 69 | const [years] = createResource(async () => (await getAcademicYears({ limit: 100 })).items); |
| 62 | const [limits] = createResource(() => canManage() ? getLimits() : null); | 70 | const [limits] = createResource(() => canManage() ? getLimits() : null); |
| 63 | const [list, { refetch }] = createResource(async () => (await getClasses()).items); | 71 | const [list, { refetch }] = createResource(async () => (await getClasses()).items); |
| 72 | + const courseTitle = (id: string) => skippedCourseTitles()[id] ?? id; | ||
| 64 | const listData = () => list.latest ?? list() ?? []; | 73 | const listData = () => list.latest ?? list() ?? []; |
| 65 | const yearName = (id: string | null) => years.latest?.find((year) => year.id === id)?.name ?? (id || t("academicYears.unassigned")); | 74 | const yearName = (id: string | null) => years.latest?.find((year) => year.id === id)?.name ?? (id || t("academicYears.unassigned")); |
| 66 | 75 | ||
| @@ -125,7 +134,23 @@ function ClassesContent() { | |||
| 125 | // The list reloads on the next visit; the class was created. | 134 | // The list reloads on the next visit; the class was created. |
| 126 | } | 135 | } |
| 127 | setFlash(t("common.created")); | 136 | setFlash(t("common.created")); |
| 128 | - void navigate({ to: "/management/classes/$id", params: { id: created.id } }); | 137 | + if (created.skipped.length > 0) { |
| 138 | + // The class exists either way; its grade's template just could not | ||
| 139 | + // take every course. The report names each skipped course, so the one | ||
| 140 | + // catalog read happens before the panel can open; a failed read leaves | ||
| 141 | + // the ids in place rather than holding the report back. | ||
| 142 | + try { | ||
| 143 | + const courses = (await getCourses({ limit: 200 })).items; | ||
| 144 | + setSkippedCourseTitles(Object.fromEntries(courses.map((course) => [course.id, course.title]))); | ||
| 145 | + } catch { | ||
| 146 | + // The report still opens; the rows fall back to course ids. | ||
| 147 | + } | ||
| 148 | + setCreatedClass(created.class); | ||
| 149 | + setSkipped(created.skipped); | ||
| 150 | + setReportOpen(true); | ||
| 151 | + return; | ||
| 152 | + } | ||
| 153 | + void navigate({ to: "/management/classes/$id", params: { id: created.class.id } }); | ||
| 129 | } catch (err) { | 154 | } catch (err) { |
| 130 | setError(formatApiError(err)); | 155 | setError(formatApiError(err)); |
| 131 | } finally { | 156 | } finally { |
| @@ -133,6 +158,19 @@ function ClassesContent() { | |||
| 133 | } | 158 | } |
| 134 | }; | 159 | }; |
| 135 | 160 | ||
| 161 | + // Closing the report is what opens the class, however it closes (button, | ||
| 162 | + // Escape, backdrop): the create already happened, so there is nowhere else | ||
| 163 | + // to go back to. | ||
| 164 | + const closeSkippedReport = (open: boolean) => { | ||
| 165 | + setReportOpen(open); | ||
| 166 | + if (open) return; | ||
| 167 | + const created = createdClass(); | ||
| 168 | + setCreatedClass(null); | ||
| 169 | + setSkipped([]); | ||
| 170 | + setSkippedCourseTitles({}); | ||
| 171 | + if (created) void navigate({ to: "/management/classes/$id", params: { id: created.id } }); | ||
| 172 | + }; | ||
| 173 | + | ||
| 136 | return ( | 174 | return ( |
| 137 | <div class="space-y-5"> | 175 | <div class="space-y-5"> |
| 138 | <SidePanel open={canManage() && showForm()} onOpenChange={setShowForm} title={t("classGroups.newClass")} description={t("classGroups.subtitle")}> | 176 | <SidePanel open={canManage() && showForm()} onOpenChange={setShowForm} title={t("classGroups.newClass")} description={t("classGroups.subtitle")}> |
| @@ -150,6 +188,22 @@ function ClassesContent() { | |||
| 150 | 188 | ||
| 151 | <Show when={flash()}><Alert variant="success">{flash()}</Alert></Show> | 189 | <Show when={flash()}><Alert variant="success">{flash()}</Alert></Show> |
| 152 | 190 | ||
| 191 | + <Show when={skipped().length > 0}> | ||
| 192 | + <Alert class="flex flex-wrap items-center justify-between gap-3 border-amber-500/40 bg-amber-500/10 text-amber-900 dark:text-amber-200"> | ||
| 193 | + <span class="min-w-0 flex-1">{t("classBlueprints.skippedSummary", { count: skipped().length })}</span> | ||
| 194 | + <Button type="button" size="sm" variant="outline" class="shrink-0 rounded-lg" onClick={() => setReportOpen(true)}> | ||
| 195 | + {t("classBlueprints.skippedDetails")} | ||
| 196 | + </Button> | ||
| 197 | + </Alert> | ||
| 198 | + </Show> | ||
| 199 | + | ||
| 200 | + <BlueprintSkippedReport | ||
| 201 | + open={reportOpen()} | ||
| 202 | + onOpenChange={closeSkippedReport} | ||
| 203 | + skipped={skipped()} | ||
| 204 | + courseTitle={courseTitle} | ||
| 205 | + /> | ||
| 206 | + | ||
| 153 | <Tabs value={tab()} onChange={setTab}> | 207 | <Tabs value={tab()} onChange={setTab}> |
| 154 | {/* Every blueprint endpoint is manager+, so a teacher must not be shown | 208 | {/* Every blueprint endpoint is manager+, so a teacher must not be shown |
| 155 | a tab that would 403 the moment it opens. */} | 209 | a tab that would 403 the moment it opens. */} |