hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { For, Show, createEffect, createMemo, createSignal } from "solid-js"; |
| 2 | import { |
| 3 | getClassBlueprintByGrade, |
| 4 | patchClassBlueprintByGrade, |
| 5 | postClassBlueprint, |
| 6 | } from "@/api/classes"; |
| 7 | import { ApiError, formatApiError, type BlueprintResult, type ClassBlueprint, type Course } from "@/api/client"; |
| 8 | import { Alert } from "@/components/ui/alert"; |
| 9 | import { Button } from "@/components/ui/button"; |
| 10 | import { GradeLevelSelect } from "@/components/classes/grade-level-select"; |
| 11 | import { Input } from "@/components/ui/input"; |
| 12 | import { Label } from "@/components/ui/label"; |
| 13 | import { SidePanel } from "@/components/ui/side-panel"; |
| 14 | import { courseKindLabel } from "@/lib/course-kind"; |
| 15 | import { matchesSearch } from "@/lib/search-text"; |
| 16 | import { useT } from "@/stores/preferences-context"; |
| 17 | |
| 18 | export function BlueprintPanel(props: { |
| 19 | open: boolean; |
| 20 | onOpenChange: (open: boolean) => void; |
| 21 | /** null creates a new blueprint; set edits that grade's course set. */ |
| 22 | blueprint: ClassBlueprint | null; |
| 23 | courses: Course[]; |
| 24 | maxCourses: number; |
| 25 | minGradeLevel?: number; |
| 26 | maxGradeLevel?: number; |
| 27 | onSaved: (result: BlueprintResult) => void; |
| 28 | }) { |
| 29 | const t = useT(); |
| 30 | const [gradeLevel, setGradeLevel] = createSignal<number | null>(null); |
| 31 | const [selected, setSelected] = createSignal<string[]>([]); |
| 32 | const [filter, setFilter] = createSignal(""); |
| 33 | const [error, setError] = createSignal(""); |
| 34 | const [warning, setWarning] = createSignal(""); |
| 35 | const [pending, setPending] = createSignal(false); |
| 36 | |
| 37 | const editing = () => props.blueprint !== null; |
| 38 | |
| 39 | // The panel instance is reused between "edit grade 9" and "edit grade 10", |
| 40 | // and props are not read at construction time in Solid — seeding these at |
| 41 | // the top of the component would freeze the first blueprint's course set. |
| 42 | createEffect(() => { |
| 43 | if (!props.open) return; |
| 44 | const bp = props.blueprint; |
| 45 | setGradeLevel(bp ? bp.grade_level : null); |
| 46 | setSelected(bp?.courses ? [...bp.courses] : []); |
| 47 | setFilter(""); |
| 48 | setError(""); |
| 49 | setWarning(""); |
| 50 | }); |
| 51 | |
| 52 | const visibleCourses = createMemo(() => { |
| 53 | const q = filter().trim(); |
| 54 | if (!q) return props.courses; |
| 55 | return props.courses.filter((c) => matchesSearch(q, c.title)); |
| 56 | }); |
| 57 | |
| 58 | const isSelected = (id: string) => selected().includes(id); |
| 59 | const atCeiling = () => selected().length >= props.maxCourses; |
| 60 | |
| 61 | const toggle = (id: string) => { |
| 62 | setSelected((prev) => (prev.includes(id) ? prev.filter((c) => c !== id) : [...prev, id])); |
| 63 | }; |
| 64 | |
| 65 | // A losing race has no version token to retry with, so re-read the blueprint, |
| 66 | // reseed the checkboxes from the server's list, and let the user resubmit. |
| 67 | const reseedFromServer = async (g: number) => { |
| 68 | try { |
| 69 | const fresh = await getClassBlueprintByGrade(g); |
| 70 | setSelected([...fresh.courses]); |
| 71 | setWarning(t("classBlueprints.staleReload")); |
| 72 | setError(""); |
| 73 | } catch (err) { |
| 74 | setError(formatApiError(err)); |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | const submit = async (e: Event) => { |
| 79 | e.preventDefault(); |
| 80 | const g = gradeLevel(); |
| 81 | if (g === null) return; |
| 82 | setError(""); |
| 83 | setWarning(""); |
| 84 | setPending(true); |
| 85 | try { |
| 86 | const result = editing() |
| 87 | ? await patchClassBlueprintByGrade(g, { course_ids: selected() }) |
| 88 | : await postClassBlueprint({ grade_level: g, course_ids: selected() }); |
| 89 | props.onSaved(result); |
| 90 | } catch (err) { |
| 91 | if (err instanceof ApiError && err.status === 409) { |
| 92 | if (editing()) { |
| 93 | await reseedFromServer(g); |
| 94 | } else { |
| 95 | setError(t("classBlueprints.conflictExists")); |
| 96 | } |
| 97 | } else { |
| 98 | setError(formatApiError(err)); |
| 99 | } |
| 100 | } finally { |
| 101 | setPending(false); |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | return ( |
| 106 | <SidePanel guardUnsaved |
| 107 | open={props.open} |
| 108 | onOpenChange={props.onOpenChange} |
| 109 | title={editing() ? t("classBlueprints.edit") : t("classBlueprints.new")} |
| 110 | description={t("classBlueprints.selectCourses")} |
| 111 | size="wide" |
| 112 | > |
| 113 | <form onSubmit={submit} class="space-y-4"> |
| 114 | <Show when={warning()}> |
| 115 | <Alert class="border-amber-500/40 bg-amber-500/10 text-amber-900 dark:text-amber-200">{warning()}</Alert> |
| 116 | </Show> |
| 117 | |
| 118 | <div class="space-y-1.5"> |
| 119 | <Label for="bp-grade"> |
| 120 | {t("classBlueprints.grade")} |
| 121 | <Show when={!editing()}><span class="ml-0.5 text-destructive-text">*</span></Show> |
| 122 | </Label> |
| 123 | <GradeLevelSelect |
| 124 | id="bp-grade" |
| 125 | value={gradeLevel()} |
| 126 | min={props.minGradeLevel} |
| 127 | max={props.maxGradeLevel} |
| 128 | disabled={editing()} |
| 129 | onChange={setGradeLevel} |
| 130 | /> |
| 131 | <p class="text-xs text-muted-foreground">{t("classBlueprints.gradeHint")}</p> |
| 132 | </div> |
| 133 | |
| 134 | <div class="space-y-2"> |
| 135 | <div class="flex flex-wrap items-baseline justify-between gap-2"> |
| 136 | <Label for="bp-filter">{t("classBlueprints.courses")}</Label> |
| 137 | <span class="text-xs tabular-nums text-muted-foreground"> |
| 138 | {t("classBlueprints.selected", { count: selected().length, max: props.maxCourses })} |
| 139 | </span> |
| 140 | </div> |
| 141 | <p class="text-xs text-muted-foreground">{t("classBlueprints.wholeSetHint")}</p> |
| 142 | <Input |
| 143 | id="bp-filter" |
| 144 | class="h-8 rounded-lg text-[13px]" |
| 145 | placeholder={t("classBlueprints.filterCourses")} |
| 146 | value={filter()} |
| 147 | onInput={(e) => setFilter(e.currentTarget.value)} |
| 148 | /> |
| 149 | <div class="max-h-72 space-y-1 overflow-y-auto rounded-lg border border-border/70 p-2"> |
| 150 | <Show |
| 151 | when={visibleCourses().length > 0} |
| 152 | fallback={<p class="p-2 text-sm text-muted-foreground">{t("classBlueprints.noCourseMatch")}</p>} |
| 153 | > |
| 154 | <For each={visibleCourses()}> |
| 155 | {(course) => ( |
| 156 | <label class="flex cursor-pointer items-center gap-2 rounded-md px-2 py-1.5 text-sm hover:bg-muted/60"> |
| 157 | <input |
| 158 | type="checkbox" |
| 159 | class="h-4 w-4 shrink-0 accent-primary" |
| 160 | checked={isSelected(course.id)} |
| 161 | disabled={!isSelected(course.id) && atCeiling()} |
| 162 | onChange={() => toggle(course.id)} |
| 163 | /> |
| 164 | <span class="min-w-0 flex-1 truncate">{course.title}</span> |
| 165 | <span class="shrink-0 text-xs text-muted-foreground">{courseKindLabel(course.kind, t)}</span> |
| 166 | </label> |
| 167 | )} |
| 168 | </For> |
| 169 | </Show> |
| 170 | </div> |
| 171 | </div> |
| 172 | |
| 173 | <Show when={error()}> |
| 174 | <Alert variant="destructive">{error()}</Alert> |
| 175 | </Show> |
| 176 | |
| 177 | {/* Save + Cancel on a hairline, like the class create and edit panels. */} |
| 178 | <div class="flex gap-2 border-t border-border-hairline pt-4"> |
| 179 | <Button type="submit" disabled={pending() || gradeLevel() === null}> |
| 180 | {t("common.save")} |
| 181 | </Button> |
| 182 | <Button type="button" variant="outline" onClick={() => props.onOpenChange(false)}> |
| 183 | {t("common.cancel")} |
| 184 | </Button> |
| 185 | </div> |
| 186 | </form> |
| 187 | </SidePanel> |
| 188 | ); |
| 189 | } |