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