import { For, Show, createEffect, createMemo, createSignal } from "solid-js"; import { getClassBlueprintByGrade, patchClassBlueprintByGrade, postClassBlueprint, } from "@/api/classes"; import { ApiError, formatApiError, type BlueprintResult, type ClassBlueprint, type Course } from "@/api/client"; import { Alert } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { GradeLevelSelect } from "@/components/classes/grade-level-select"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { SidePanel } from "@/components/ui/side-panel"; import { courseKindLabel } from "@/lib/course-kind"; import { matchesSearch } from "@/lib/search-text"; import { useT } from "@/stores/preferences-context"; export function BlueprintPanel(props: { open: boolean; onOpenChange: (open: boolean) => void; /** null creates a new blueprint; set edits that grade's course set. */ blueprint: ClassBlueprint | null; courses: Course[]; maxCourses: number; minGradeLevel?: number; maxGradeLevel?: number; onSaved: (result: BlueprintResult) => void; }) { const t = useT(); const [gradeLevel, setGradeLevel] = createSignal(null); const [selected, setSelected] = createSignal([]); const [filter, setFilter] = createSignal(""); const [error, setError] = createSignal(""); const [warning, setWarning] = createSignal(""); const [pending, setPending] = createSignal(false); const editing = () => props.blueprint !== null; // The panel instance is reused between "edit grade 9" and "edit grade 10", // and props are not read at construction time in Solid — seeding these at // the top of the component would freeze the first blueprint's course set. createEffect(() => { if (!props.open) return; const bp = props.blueprint; setGradeLevel(bp ? bp.grade_level : null); setSelected(bp?.courses ? [...bp.courses] : []); setFilter(""); setError(""); setWarning(""); }); const visibleCourses = createMemo(() => { const q = filter().trim(); if (!q) return props.courses; return props.courses.filter((c) => matchesSearch(q, c.title)); }); const isSelected = (id: string) => selected().includes(id); const atCeiling = () => selected().length >= props.maxCourses; const toggle = (id: string) => { setSelected((prev) => (prev.includes(id) ? prev.filter((c) => c !== id) : [...prev, id])); }; // A losing race has no version token to retry with, so re-read the blueprint, // reseed the checkboxes from the server's list, and let the user resubmit. const reseedFromServer = async (g: number) => { try { const fresh = await getClassBlueprintByGrade(g); setSelected([...fresh.courses]); setWarning(t("classBlueprints.staleReload")); setError(""); } catch (err) { setError(formatApiError(err)); } }; const submit = async (e: Event) => { e.preventDefault(); const g = gradeLevel(); if (g === null) return; setError(""); setWarning(""); setPending(true); try { const result = editing() ? await patchClassBlueprintByGrade(g, { course_ids: selected() }) : await postClassBlueprint({ grade_level: g, course_ids: selected() }); props.onSaved(result); } catch (err) { if (err instanceof ApiError && err.status === 409) { if (editing()) { await reseedFromServer(g); } else { setError(t("classBlueprints.conflictExists")); } } else { setError(formatApiError(err)); } } finally { setPending(false); } }; return (
{warning()}

{t("classBlueprints.gradeHint")}

{t("classBlueprints.selected", { count: selected().length, max: props.maxCourses })}

{t("classBlueprints.wholeSetHint")}

setFilter(e.currentTarget.value)} />
0} fallback={

{t("classBlueprints.noCourseMatch")}

} > {(course) => ( )}
{error()} {/* Save + Cancel on a hairline, like the class create and edit panels. */}
); }