import { Show, createEffect, createSignal } from "solid-js"; import { deleteInstanceExamWeightByKind, deleteInstanceExamWeights, deleteInstanceSubjectById, deleteInstanceSubjects, patchInstanceById, patchInstanceExamWeight, postInstanceReset, postInstanceSubject, } from "@/api/instances"; import { formatApiError, type ExamWeightEntry, type Instance, type InstanceOverrideField, type Subject } from "@/api/client"; import { ExamWeightsEditor } from "@/components/instances/exam-weights-editor"; import { OverrideBadge } from "@/components/instances/override-badge"; import { SubjectSetEditor } from "@/components/instances/subject-set-editor"; import { Alert } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { ConfirmDialog } from "@/components/ui/confirm-dialog"; import { DataSection } from "@/components/ui/data-section"; import { IconEdit, IconPlus, IconRotateCcw } from "@/components/ui/icons"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { SidePanel } from "@/components/ui/side-panel"; import { Textarea } from "@/components/ui/textarea"; import { useT } from "@/stores/preferences-context"; type ResetTarget = { fields: InstanceOverrideField[]; label: string; run?: () => Promise }; /** * What a section overrides of its grade template: title/description, hours * and the karne flag, the topic set and the exam weights. Every value is the * resolved one; a badge tells an own override from an inherited value, and * "reset" hands a field back to the template. */ export function InstanceSettingsPanel(props: { instance: Instance; canManage: boolean; /** The catalog course's subjects — what a section's own set may hold. */ courseSubjects: Subject[]; /** settings.exam_kinds names. */ examKinds: string[]; maxTitleLen?: number; maxDescriptionLen?: number; onChanged: () => unknown; }) { const t = useT(); const [error, setError] = createSignal(""); const [editingContent, setEditingContent] = createSignal(false); const [title, setTitle] = createSignal(""); const [description, setDescription] = createSignal(""); const [contentError, setContentError] = createSignal(""); const [pending, setPending] = createSignal(false); const [reset, setReset] = createSignal(null); const [weightEditing, setWeightEditing] = createSignal(null); createEffect(() => { if (!editingContent()) return; setTitle(props.instance.title); setDescription(props.instance.description); setContentError(""); }); const act = async (action: () => Promise) => { setError(""); try { await action(); await props.onChanged(); } catch (err) { setError(formatApiError(err)); throw err; } }; // For editors that show their own error: write, then reload the instance. const mutate = async (action: () => Promise) => { await action(); await props.onChanged(); }; const saveContent = async (event: SubmitEvent) => { event.preventDefault(); const nextTitle = title().trim(); if (!nextTitle) { setContentError(t("form.fieldRequired")); return; } // PATCH only what changed: sending an unchanged inherited value would // silently freeze it as the section's own override. const body: { title?: string; description?: string } = {}; if (nextTitle !== props.instance.title) body.title = nextTitle; if (description().trim() !== props.instance.description) body.description = description().trim(); if (!body.title && body.description === undefined) { setEditingContent(false); return; } setPending(true); try { await patchInstanceById(props.instance.id, body); await props.onChanged(); setEditingContent(false); } catch (err) { setContentError(formatApiError(err)); } finally { setPending(false); } }; const resetButton = (target: ResetTarget) => ( ); const row = (label: string, value: string, own: boolean, field: InstanceOverrideField) => (

{label}

{value || "—"}

{resetButton({ fields: [field], label })}
); return (

{t("instances.settingsHelp")}

{error()} } >
{row(t("form.title"), props.instance.title, props.instance.title_overridden, "title")} {row(t("form.description"), props.instance.description, props.instance.description_overridden, "description")}
{row(t("instances.dersSaati"), String(props.instance.ders_saati), props.instance.ders_saati_overridden, "ders_saati")} {row(t("instances.countsTowardKarne"), props.instance.counts_toward_karne ? t("common.yes") : t("common.no"), props.instance.counts_toward_karne_overridden, "counts_toward_karne")}
{resetButton({ fields: ["subjects"], label: t("instances.subjectsTitle"), run: () => deleteInstanceSubjects(props.instance.id) })} } > mutate(() => postInstanceSubject(props.instance.id, subjectId))} onRemove={(subject) => mutate(() => deleteInstanceSubjectById(props.instance.id, subject.id))} /> mutate(() => patchInstanceExamWeight(props.instance.id, entry))} onRemove={props.instance.exam_weights_inherited ? undefined : (kind) => mutate(() => deleteInstanceExamWeightByKind(props.instance.id, kind))} actions={ {resetButton({ fields: ["exam_weights"], label: t("instances.examWeightsTitle"), run: () => deleteInstanceExamWeights(props.instance.id) })} } />
setTitle(e.currentTarget.value)} />