import { Show, createEffect, createSignal } from "solid-js"; import { createResource } from "@/lib/create-resource"; import { deleteOfferingExamWeightByKind, deleteOfferingSubjectById, deleteOfferingWeeklySlotById, getOfferingExamWeights, getOfferingSubjects, getOfferingWeeklyPlan, patchOfferingById, patchOfferingExamWeight, postOffering, postOfferingSubject, postOfferingWeeklySlot, type UpdateOfferingBody, } from "@/api/offerings"; import { formatApiError, type ExamWeightEntry, type Limits, type Offering, type Subject, type WeeklySlot } from "@/api/client"; import { GradeLevelSelect } from "@/components/classes/grade-level-select"; import { ExamWeightsEditor } from "@/components/instances/exam-weights-editor"; import { SubjectSetEditor } from "@/components/instances/subject-set-editor"; import { WeeklyPlanTable } from "@/components/instances/weekly-plan-table"; import { InfoTip } from "@/components/ui/info-tip"; import { WeeklySlotForm } from "@/components/instances/weekly-slot-form"; import { Alert } from "@/components/ui/alert"; import { ErrorAlert } from "@/components/ui/error-alert"; import { Button } from "@/components/ui/button"; import { IconPlus } from "@/components/ui/icons"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select } from "@/components/ui/select"; import { SidePanel } from "@/components/ui/side-panel"; import { Textarea } from "@/components/ui/textarea"; import { gradeLevelLabel } from "@/lib/grade-level"; import { useT } from "@/stores/preferences-context"; const SECTION = "space-y-3 border-t border-border-hairline pt-4"; /** * One grade template of a catalog course: `offering` null creates it (grade * plus defaults), set edits its defaults and its topic set, template week and * exam weights. Blank fields inherit — sent as null, never as "". */ export function OfferingPanel(props: { open: boolean; onOpenChange: (open: boolean) => void; courseId: string; courseTitle: string; offering: Offering | null; canEdit: boolean; courseSubjects: Subject[]; /** The catalog subjects are still loading: the picker waits instead of saying all are taken. */ courseSubjectsLoading?: boolean; examKinds: string[]; limits?: Limits; onSaved: (offering: Offering) => void | Promise; }) { const t = useT(); const [gradeLevel, setGradeLevel] = createSignal(null); const [title, setTitle] = createSignal(""); const [description, setDescription] = createSignal(""); const [hours, setHours] = createSignal(""); const [karne, setKarne] = createSignal<"" | "yes" | "no">(""); const [error, setError] = createSignal(""); const [pending, setPending] = createSignal(false); const [addingSlot, setAddingSlot] = createSignal(false); const [weightEditing, setWeightEditing] = createSignal(null); const [sectionError, setSectionError] = createSignal(""); const editingId = () => (props.open && props.offering ? props.offering.id : null); const [subjects, { refetch: refetchSubjects }] = createResource(editingId, (id) => getOfferingSubjects(id)); const [slots, { refetch: refetchSlots }] = createResource(editingId, (id) => getOfferingWeeklyPlan(id)); const [weights, { refetch: refetchWeights }] = createResource(editingId, async (id) => (await getOfferingExamWeights(id)).weights); // An errored resource throws on read; each section shows its own retry. const subjectRows = () => (subjects.error ? [] : subjects.latest ?? []); const slotRows = () => (slots.error ? [] : slots.latest ?? []); const weightRows = () => (weights.error ? [] : weights.latest ?? []); createEffect(() => { if (!props.open) return; const o = props.offering; setGradeLevel(o ? o.grade_level : null); setTitle(o?.title ?? ""); setDescription(o?.description ?? ""); setHours(o?.default_ders_saati != null ? String(o.default_ders_saati) : ""); setKarne(o?.default_counts_toward_karne == null ? "" : o.default_counts_toward_karne ? "yes" : "no"); setError(""); setSectionError(""); }); const submit = async (event: SubmitEvent) => { event.preventDefault(); const level = gradeLevel(); if (level === null) { setError(t("form.fieldRequired")); return; } const hoursText = hours().trim(); const hoursValue = hoursText === "" ? null : Number(hoursText); const minHours = props.limits?.course.min_ders_saati ?? 1; const maxHours = props.limits?.course.max_ders_saati ?? 40; if (hoursValue !== null && (!Number.isInteger(hoursValue) || hoursValue < minHours || hoursValue > maxHours)) { setError(t("instances.dersSaatiInvalid")); return; } const fields: UpdateOfferingBody = { title: title().trim() || null, description: description().trim() || null, default_ders_saati: hoursValue, default_counts_toward_karne: karne() === "" ? null : karne() === "yes", }; setError(""); setPending(true); try { const saved = props.offering ? await patchOfferingById(props.offering.id, fields) : await postOffering({ course: props.courseId, grade_level: level, ...fields }); await props.onSaved(saved); } catch (err) { setError(formatApiError(err)); } finally { setPending(false); } }; const guard = async (action: () => Promise) => { setSectionError(""); try { await action(); } catch (err) { setSectionError(formatApiError(err)); } }; return (
setTitle(e.currentTarget.value)} />