forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

school-edit-panel.tsx85 lines, 3.3 KiBHistoryRawDownload
1import { Show, createEffect, createSignal } from "solid-js";
2import { patchSchool } from "@/api/schools";
3import { formatApiError, type School, type SchoolStatus } from "@/api/client";
4import { Alert } from "@/components/ui/alert";
5import { Button } from "@/components/ui/button";
6import { Input } from "@/components/ui/input";
7import { Label } from "@/components/ui/label";
8import { Select } from "@/components/ui/select";
9import { SidePanel } from "@/components/ui/side-panel";
10import { useT } from "@/stores/preferences-context";
11
12/**
13 * A tenant school's edit form (name, status). One component for the builder
14 * school detail header and the builder schools list's row menu, so both send
15 * the same PATCH. The panel closes itself on success; the caller refetches
16 * and shows its own flash in `onSaved`.
17 */
18export function SchoolEditPanel(props: {
19 school: School | null;
20 open: boolean;
21 onOpenChange: (open: boolean) => void;
22 onSaved: () => void | Promise<void>;
23}) {
24 const t = useT();
25 const [name, setName] = createSignal("");
26 const [status, setStatus] = createSignal<SchoolStatus>("active");
27 const [error, setError] = createSignal("");
28 const [pending, setPending] = createSignal(false);
29
30 // Seed the form each time it opens, from the school it opens for.
31 createEffect(() => {
32 const current = props.school;
33 if (!props.open || !current) return;
34 setName(current.name);
35 setStatus(current.status);
36 setError("");
37 });
38
39 const submit = async (event: SubmitEvent) => {
40 event.preventDefault();
41 const current = props.school;
42 if (!current || pending()) return;
43 setError("");
44 setPending(true);
45 try {
46 await patchSchool(current.id, { name: name().trim(), status: status() });
47 props.onOpenChange(false);
48 await props.onSaved();
49 } catch (err) {
50 setError(formatApiError(err));
51 } finally {
52 setPending(false);
53 }
54 };
55
56 return (
57 <SidePanel guardUnsaved open={props.open && !!props.school} onOpenChange={props.onOpenChange} title={t("builder.editSchool")} description={t("builder.editSchoolHint")}>
58 <form class="space-y-4" onSubmit={submit}>
59 <Show when={error()}>
60 <Alert variant="destructive">{error()}</Alert>
61 </Show>
62 <div class="space-y-1.5">
63 <Label for="school-edit-name">{t("builder.schoolName")}</Label>
64 <Input id="school-edit-name" class="rounded-lg" required maxlength={120} value={name()} onInput={(e) => setName(e.currentTarget.value)} />
65 </div>
66 <div class="space-y-1.5">
67 <Label for="school-edit-status">{t("builder.status")}</Label>
68 <Select id="school-edit-status" value={status()} onChange={(e) => setStatus(e.currentTarget.value as SchoolStatus)}>
69 <option value="active">{t("builder.statusActive")}</option>
70 <option value="suspended">{t("builder.statusSuspended")}</option>
71 </Select>
72 <p class="text-xs text-text-subtle">{t("builder.suspendHint")}</p>
73 </div>
74 <div class="flex flex-wrap items-center gap-2">
75 <Button type="button" variant="outline" class="h-10 rounded-lg" onClick={() => props.onOpenChange(false)}>
76 {t("common.cancel")}
77 </Button>
78 <Button type="submit" class="h-10 rounded-lg" disabled={pending()}>
79 {t("common.save")}
80 </Button>
81 </div>
82 </form>
83 </SidePanel>
84 );
85}