hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { For, Show, createEffect, createSignal } from "solid-js"; |
| 2 | import { patchClassById } from "@/api/classes"; |
| 3 | import { formatApiError, type AcademicYear, type ClassGroup } from "@/api/client"; |
| 4 | import { Alert } from "@/components/ui/alert"; |
| 5 | import { Button } from "@/components/ui/button"; |
| 6 | import { GradeLevelSelect } from "@/components/classes/grade-level-select"; |
| 7 | import { Input } from "@/components/ui/input"; |
| 8 | import { Label } from "@/components/ui/label"; |
| 9 | import { Select } from "@/components/ui/select"; |
| 10 | import { SidePanel } from "@/components/ui/side-panel"; |
| 11 | import { UserSearchSelect } from "@/components/users/user-search-select"; |
| 12 | import { useT } from "@/stores/preferences-context"; |
| 13 | |
| 14 | /** |
| 15 | * A şube's edit form (name, grade, academic year, homeroom teacher). One |
| 16 | * component for the class detail page header and the classes list's row menu, |
| 17 | * so both send the same PATCH. The panel closes itself on success and hands |
| 18 | * the saved row back; the caller refetches and shows its own flash. |
| 19 | */ |
| 20 | export function ClassEditPanel(props: { |
| 21 | cls: ClassGroup | null; |
| 22 | open: boolean; |
| 23 | onOpenChange: (open: boolean) => void; |
| 24 | onSaved: (cls: ClassGroup) => void | Promise<void>; |
| 25 | years: AcademicYear[]; |
| 26 | maxNameLen?: number; |
| 27 | minGradeLevel?: number; |
| 28 | maxGradeLevel?: number; |
| 29 | }) { |
| 30 | const t = useT(); |
| 31 | const [name, setName] = createSignal(""); |
| 32 | const [gradeLevel, setGradeLevel] = createSignal<number | null>(null); |
| 33 | const [yearId, setYearId] = createSignal(""); |
| 34 | const [teacherId, setTeacherId] = createSignal(""); |
| 35 | const [error, setError] = createSignal(""); |
| 36 | const [nameError, setNameError] = createSignal(""); |
| 37 | const [pending, setPending] = createSignal(false); |
| 38 | |
| 39 | // Seed the form each time it opens, from the row it opens for. |
| 40 | createEffect(() => { |
| 41 | const c = props.cls; |
| 42 | if (!props.open || !c) return; |
| 43 | setName(c.name); |
| 44 | setGradeLevel(c.grade_level); |
| 45 | setYearId(c.year ?? ""); |
| 46 | setTeacherId(c.teacher?.id ?? ""); |
| 47 | setError(""); |
| 48 | setNameError(""); |
| 49 | }); |
| 50 | |
| 51 | const submit = async (event: SubmitEvent) => { |
| 52 | event.preventDefault(); |
| 53 | const c = props.cls; |
| 54 | if (!c || pending()) return; |
| 55 | setError(""); |
| 56 | // Same rule as the create form: a blank name is caught under the field, |
| 57 | // not sent for the backend to reject. |
| 58 | if (!name().trim()) { |
| 59 | setNameError(t("form.fieldRequired")); |
| 60 | document.getElementById("edit-class-name")?.focus(); |
| 61 | return; |
| 62 | } |
| 63 | setPending(true); |
| 64 | try { |
| 65 | const saved = await patchClassById(c.id, { name: name().trim(), ...(gradeLevel() !== null ? { grade_level: gradeLevel()! } : {}), year: yearId() || null, teacher_id: teacherId() || null }); |
| 66 | props.onOpenChange(false); |
| 67 | await props.onSaved(saved); |
| 68 | } catch (err) { |
| 69 | setError(formatApiError(err)); |
| 70 | } finally { |
| 71 | setPending(false); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | return ( |
| 76 | <SidePanel guardUnsaved open={props.open && !!props.cls} onOpenChange={props.onOpenChange} title={t("common.edit")} description={props.cls?.name}> |
| 77 | <form class="space-y-4" noValidate onSubmit={submit}> |
| 78 | <div class="space-y-3"> |
| 79 | <div class="space-y-1.5"><Label for="edit-class-name">{t("classGroups.className")}<span class="ml-0.5 text-destructive-text">*</span></Label><Input id="edit-class-name" required aria-required="true" maxlength={props.maxNameLen} value={name()} error={nameError()} onInput={(e) => { setName(e.currentTarget.value); setNameError(""); }} /></div> |
| 80 | <div class="space-y-1.5"><Label for="edit-class-grade">{t("classGroups.grade")}</Label><GradeLevelSelect id="edit-class-grade" value={gradeLevel()} min={props.minGradeLevel} max={props.maxGradeLevel} onChange={setGradeLevel} /></div> |
| 81 | <div class="space-y-1.5"><Label for="edit-class-year">{t("academicYears.year")}</Label><Select id="edit-class-year" value={yearId()} onChange={(e) => setYearId(e.currentTarget.value)}><option value="">{t("academicYears.unassigned")}</option><For each={props.years}>{(year) => <option value={year.id}>{year.name}</option>}</For></Select></div> |
| 82 | <UserSearchSelect id="edit-class-teacher" label={t("classGroups.homeroomTeacher")} value={teacherId()} initialUser={props.cls?.teacher} onChange={setTeacherId} placeholder={t("classGroups.selectTeacher")} role="teacher" /> |
| 83 | </div> |
| 84 | <Show when={error()}><Alert variant="destructive">{error()}</Alert></Show> |
| 85 | <div class="flex gap-2 border-t border-border-hairline pt-4"><Button type="submit" disabled={pending()}>{t("common.save")}</Button><Button type="button" variant="outline" onClick={() => props.onOpenChange(false)}>{t("common.cancel")}</Button></div> |
| 86 | </form> |
| 87 | </SidePanel> |
| 88 | ); |
| 89 | } |