hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { Show, createEffect, createSignal } from "solid-js"; |
| 2 | import { createResource } from "@/lib/create-resource"; |
| 3 | import { |
| 4 | deleteOfferingExamWeightByKind, |
| 5 | deleteOfferingSubjectById, |
| 6 | deleteOfferingWeeklySlotById, |
| 7 | getOfferingExamWeights, |
| 8 | getOfferingSubjects, |
| 9 | getOfferingWeeklyPlan, |
| 10 | patchOfferingById, |
| 11 | patchOfferingExamWeight, |
| 12 | postOffering, |
| 13 | postOfferingSubject, |
| 14 | postOfferingWeeklySlot, |
| 15 | type UpdateOfferingBody, |
| 16 | } from "@/api/offerings"; |
| 17 | import { formatApiError, type ExamWeightEntry, type Limits, type Offering, type Subject, type WeeklySlot } from "@/api/client"; |
| 18 | import { GradeLevelSelect } from "@/components/classes/grade-level-select"; |
| 19 | import { ExamWeightsEditor } from "@/components/instances/exam-weights-editor"; |
| 20 | import { SubjectSetEditor } from "@/components/instances/subject-set-editor"; |
| 21 | import { WeeklyPlanTable } from "@/components/instances/weekly-plan-table"; |
| 22 | import { InfoTip } from "@/components/ui/info-tip"; |
| 23 | import { WeeklySlotForm } from "@/components/instances/weekly-slot-form"; |
| 24 | import { Alert } from "@/components/ui/alert"; |
| 25 | import { ErrorAlert } from "@/components/ui/error-alert"; |
| 26 | import { Button } from "@/components/ui/button"; |
| 27 | import { IconPlus } from "@/components/ui/icons"; |
| 28 | import { Input } from "@/components/ui/input"; |
| 29 | import { Label } from "@/components/ui/label"; |
| 30 | import { Select } from "@/components/ui/select"; |
| 31 | import { SidePanel } from "@/components/ui/side-panel"; |
| 32 | import { Textarea } from "@/components/ui/textarea"; |
| 33 | import { gradeLevelLabel } from "@/lib/grade-level"; |
| 34 | import { useT } from "@/stores/preferences-context"; |
| 35 | |
| 36 | const SECTION = "space-y-3 border-t border-border-hairline pt-4"; |
| 37 | |
| 38 | /** |
| 39 | * One grade template of a catalog course: `offering` null creates it (grade |
| 40 | * plus defaults), set edits its defaults and its topic set, template week and |
| 41 | * exam weights. Blank fields inherit — sent as null, never as "". |
| 42 | */ |
| 43 | export function OfferingPanel(props: { |
| 44 | open: boolean; |
| 45 | onOpenChange: (open: boolean) => void; |
| 46 | courseId: string; |
| 47 | courseTitle: string; |
| 48 | offering: Offering | null; |
| 49 | canEdit: boolean; |
| 50 | courseSubjects: Subject[]; |
| 51 | /** The catalog subjects are still loading: the picker waits instead of saying all are taken. */ |
| 52 | courseSubjectsLoading?: boolean; |
| 53 | examKinds: string[]; |
| 54 | limits?: Limits; |
| 55 | onSaved: (offering: Offering) => void | Promise<void>; |
| 56 | }) { |
| 57 | const t = useT(); |
| 58 | const [gradeLevel, setGradeLevel] = createSignal<number | null>(null); |
| 59 | const [title, setTitle] = createSignal(""); |
| 60 | const [description, setDescription] = createSignal(""); |
| 61 | const [hours, setHours] = createSignal(""); |
| 62 | const [karne, setKarne] = createSignal<"" | "yes" | "no">(""); |
| 63 | const [error, setError] = createSignal(""); |
| 64 | const [pending, setPending] = createSignal(false); |
| 65 | const [addingSlot, setAddingSlot] = createSignal(false); |
| 66 | const [weightEditing, setWeightEditing] = createSignal<ExamWeightEntry | "new" | null>(null); |
| 67 | const [sectionError, setSectionError] = createSignal(""); |
| 68 | |
| 69 | const editingId = () => (props.open && props.offering ? props.offering.id : null); |
| 70 | const [subjects, { refetch: refetchSubjects }] = createResource(editingId, (id) => getOfferingSubjects(id)); |
| 71 | const [slots, { refetch: refetchSlots }] = createResource(editingId, (id) => getOfferingWeeklyPlan(id)); |
| 72 | const [weights, { refetch: refetchWeights }] = createResource(editingId, async (id) => (await getOfferingExamWeights(id)).weights); |
| 73 | // An errored resource throws on read; each section shows its own retry. |
| 74 | const subjectRows = () => (subjects.error ? [] : subjects.latest ?? []); |
| 75 | const slotRows = () => (slots.error ? [] : slots.latest ?? []); |
| 76 | const weightRows = () => (weights.error ? [] : weights.latest ?? []); |
| 77 | |
| 78 | createEffect(() => { |
| 79 | if (!props.open) return; |
| 80 | const o = props.offering; |
| 81 | setGradeLevel(o ? o.grade_level : null); |
| 82 | setTitle(o?.title ?? ""); |
| 83 | setDescription(o?.description ?? ""); |
| 84 | setHours(o?.default_ders_saati != null ? String(o.default_ders_saati) : ""); |
| 85 | setKarne(o?.default_counts_toward_karne == null ? "" : o.default_counts_toward_karne ? "yes" : "no"); |
| 86 | setError(""); |
| 87 | setSectionError(""); |
| 88 | }); |
| 89 | |
| 90 | const submit = async (event: SubmitEvent) => { |
| 91 | event.preventDefault(); |
| 92 | const level = gradeLevel(); |
| 93 | if (level === null) { |
| 94 | setError(t("form.fieldRequired")); |
| 95 | return; |
| 96 | } |
| 97 | const hoursText = hours().trim(); |
| 98 | const hoursValue = hoursText === "" ? null : Number(hoursText); |
| 99 | const minHours = props.limits?.course.min_ders_saati ?? 1; |
| 100 | const maxHours = props.limits?.course.max_ders_saati ?? 40; |
| 101 | if (hoursValue !== null && (!Number.isInteger(hoursValue) || hoursValue < minHours || hoursValue > maxHours)) { |
| 102 | setError(t("instances.dersSaatiInvalid")); |
| 103 | return; |
| 104 | } |
| 105 | const fields: UpdateOfferingBody = { |
| 106 | title: title().trim() || null, |
| 107 | description: description().trim() || null, |
| 108 | default_ders_saati: hoursValue, |
| 109 | default_counts_toward_karne: karne() === "" ? null : karne() === "yes", |
| 110 | }; |
| 111 | setError(""); |
| 112 | setPending(true); |
| 113 | try { |
| 114 | const saved = props.offering |
| 115 | ? await patchOfferingById(props.offering.id, fields) |
| 116 | : await postOffering({ course: props.courseId, grade_level: level, ...fields }); |
| 117 | await props.onSaved(saved); |
| 118 | } catch (err) { |
| 119 | setError(formatApiError(err)); |
| 120 | } finally { |
| 121 | setPending(false); |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | const guard = async (action: () => Promise<unknown>) => { |
| 126 | setSectionError(""); |
| 127 | try { |
| 128 | await action(); |
| 129 | } catch (err) { |
| 130 | setSectionError(formatApiError(err)); |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | return ( |
| 135 | <SidePanel |
| 136 | guardUnsaved |
| 137 | size="wide" |
| 138 | open={props.open} |
| 139 | onOpenChange={props.onOpenChange} |
| 140 | title={props.offering ? `${props.courseTitle} · ${gradeLevelLabel(props.offering.grade_level, t)}` : t("offerings.new")} |
| 141 | description={t("offerings.help")} |
| 142 | > |
| 143 | <div class="space-y-4"> |
| 144 | <form class="space-y-3" noValidate onSubmit={submit}> |
| 145 | <div class="space-y-1.5"> |
| 146 | <Label for="offering-grade">{t("classGroups.grade")}</Label> |
| 147 | <GradeLevelSelect |
| 148 | id="offering-grade" |
| 149 | value={gradeLevel()} |
| 150 | min={props.limits?.course.min_grade_level} |
| 151 | max={props.limits?.course.max_grade_level} |
| 152 | disabled={!!props.offering || !props.canEdit} |
| 153 | onChange={setGradeLevel} |
| 154 | /> |
| 155 | </div> |
| 156 | <div class="space-y-1.5"> |
| 157 | <Label for="offering-title">{t("form.title")}</Label> |
| 158 | <Input id="offering-title" maxlength={props.limits?.course.max_title_len} disabled={!props.canEdit} placeholder={t("offerings.titlePlaceholder", { title: props.courseTitle })} value={title()} onInput={(e) => setTitle(e.currentTarget.value)} /> |
| 159 | </div> |
| 160 | <div class="space-y-1.5"> |
| 161 | <Label for="offering-description">{t("form.description")}</Label> |
| 162 | <Textarea id="offering-description" rows={3} maxlength={props.limits?.course.max_description_len} disabled={!props.canEdit} placeholder={t("offerings.descriptionPlaceholder")} value={description()} onInput={(e) => setDescription(e.currentTarget.value)} /> |
| 163 | </div> |
| 164 | <div class="grid gap-3 sm:grid-cols-2"> |
| 165 | <div class="space-y-1.5"> |
| 166 | <Label for="offering-hours">{t("offerings.defaultDersSaati")}</Label> |
| 167 | <Input id="offering-hours" type="number" inputMode="numeric" min={props.limits?.course.min_ders_saati} max={props.limits?.course.max_ders_saati} disabled={!props.canEdit} placeholder="1" value={hours()} onInput={(e) => setHours(e.currentTarget.value)} /> |
| 168 | <p class="text-xs text-muted-foreground">{t("offerings.defaultDersSaatiHint")}</p> |
| 169 | </div> |
| 170 | <div class="space-y-1.5"> |
| 171 | <Label for="offering-karne">{t("offerings.defaultKarne")}</Label> |
| 172 | <Select id="offering-karne" value={karne()} disabled={!props.canEdit} onChange={(e) => setKarne(e.currentTarget.value as "" | "yes" | "no")}> |
| 173 | <option value="">{t("offerings.inheritDefault")}</option> |
| 174 | <option value="yes">{t("common.yes")}</option> |
| 175 | <option value="no">{t("common.no")}</option> |
| 176 | </Select> |
| 177 | </div> |
| 178 | </div> |
| 179 | <Show when={error()}><Alert variant="destructive">{error()}</Alert></Show> |
| 180 | <Show when={props.canEdit}> |
| 181 | <div class="flex gap-2"> |
| 182 | <Button type="submit" disabled={pending()}>{props.offering ? t("common.save") : t("common.create")}</Button> |
| 183 | <Button type="button" variant="outline" onClick={() => props.onOpenChange(false)}>{t("common.cancel")}</Button> |
| 184 | </div> |
| 185 | </Show> |
| 186 | </form> |
| 187 | |
| 188 | <Show when={props.offering}> |
| 189 | {(offering) => ( |
| 190 | <> |
| 191 | <Show when={sectionError()}><Alert variant="destructive">{sectionError()}</Alert></Show> |
| 192 | |
| 193 | <section class={SECTION}> |
| 194 | <div class="flex items-center gap-1"> |
| 195 | <h3 class="text-sm font-semibold">{t("instances.subjectsTitle")}</h3> |
| 196 | <InfoTip text={t("offerings.subjectsHelp")} label={t("common.infoAbout", { item: t("instances.subjectsTitle") })} /> |
| 197 | </div> |
| 198 | <Show when={subjects.error}> |
| 199 | <ErrorAlert message={formatApiError(subjects.error)} onRetry={() => void refetchSubjects()} /> |
| 200 | </Show> |
| 201 | <SubjectSetEditor |
| 202 | loading={(subjects.loading && !subjects.latest) || props.courseSubjectsLoading} |
| 203 | selected={subjectRows()} |
| 204 | available={props.courseSubjects} |
| 205 | canEdit={props.canEdit} |
| 206 | onAdd={async (subjectId) => { await postOfferingSubject(offering().id, subjectId); await refetchSubjects(); }} |
| 207 | onRemove={async (subject) => { await deleteOfferingSubjectById(offering().id, subject.id); await refetchSubjects(); }} |
| 208 | /> |
| 209 | </section> |
| 210 | |
| 211 | <section class={SECTION}> |
| 212 | <div class="flex items-center gap-1"> |
| 213 | <h3 class="text-sm font-semibold">{t("weeklyPlan.title")}</h3> |
| 214 | <InfoTip text={t("offerings.weeklyPlanHelp")} label={t("common.infoAbout", { item: t("weeklyPlan.title") })} /> |
| 215 | </div> |
| 216 | <Show when={slots.error}> |
| 217 | <ErrorAlert message={formatApiError(slots.error)} onRetry={() => void refetchSlots()} /> |
| 218 | </Show> |
| 219 | <WeeklyPlanTable |
| 220 | slots={slotRows()} |
| 221 | onRemove={props.canEdit ? (slot: WeeklySlot) => void guard(async () => { await deleteOfferingWeeklySlotById(offering().id, slot.id); await refetchSlots(); }) : undefined} |
| 222 | actions={ |
| 223 | <Show when={props.canEdit}> |
| 224 | <Button type="button" size="sm" onClick={() => setAddingSlot(true)}> |
| 225 | <IconPlus class="h-4 w-4" /> |
| 226 | {t("weeklyPlan.addSlot")} |
| 227 | </Button> |
| 228 | </Show> |
| 229 | } |
| 230 | /> |
| 231 | </section> |
| 232 | |
| 233 | <section class={SECTION}> |
| 234 | <div class="flex items-center gap-1"> |
| 235 | <h3 class="text-sm font-semibold">{t("instances.examWeightsTitle")}</h3> |
| 236 | <InfoTip text={t("offerings.weightsHelp")} label={t("common.infoAbout", { item: t("instances.examWeightsTitle") })} /> |
| 237 | </div> |
| 238 | <Show when={weights.error}> |
| 239 | <ErrorAlert message={formatApiError(weights.error)} onRetry={() => void refetchWeights()} /> |
| 240 | </Show> |
| 241 | <ExamWeightsEditor |
| 242 | weights={weightRows()} |
| 243 | kinds={props.examKinds} |
| 244 | canEdit={props.canEdit} |
| 245 | editing={weightEditing()} |
| 246 | onEditingChange={setWeightEditing} |
| 247 | onSet={async (entry) => { await patchOfferingExamWeight(offering().id, entry); await refetchWeights(); }} |
| 248 | onRemove={async (kind) => { await deleteOfferingExamWeightByKind(offering().id, kind); await refetchWeights(); }} |
| 249 | actions={ |
| 250 | <Show when={props.canEdit}> |
| 251 | <Button type="button" size="sm" onClick={() => setWeightEditing("new")}> |
| 252 | <IconPlus class="h-4 w-4" /> |
| 253 | {t("instances.addWeight")} |
| 254 | </Button> |
| 255 | </Show> |
| 256 | } |
| 257 | /> |
| 258 | </section> |
| 259 | |
| 260 | <WeeklySlotForm |
| 261 | open={addingSlot()} |
| 262 | onOpenChange={setAddingSlot} |
| 263 | minMinute={props.limits?.weekly_plan.min_slot_minute} |
| 264 | maxMinute={props.limits?.weekly_plan.max_slot_minute} |
| 265 | maxTopicLen={props.limits?.course.max_session_topic_len} |
| 266 | onSubmit={async (body) => { await postOfferingWeeklySlot(offering().id, body); await refetchSlots(); }} |
| 267 | /> |
| 268 | </> |
| 269 | )} |
| 270 | </Show> |
| 271 | </div> |
| 272 | </SidePanel> |
| 273 | ); |
| 274 | } |