hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { Show, createSignal } from "solid-js"; |
| 2 | import { createResource } from "@/lib/create-resource"; |
| 3 | import { getModulesCatalog } from "@/api/modules"; |
| 4 | import { getLimits } from "@/api/limits"; |
| 5 | import { postSchool } from "@/api/schools"; |
| 6 | import type { School } from "@/api/client"; |
| 7 | import { ModuleCatalogGrid } from "@/components/modules/module-catalog-grid"; |
| 8 | import { formatModuleError } from "@/lib/module-labels"; |
| 9 | import { Alert } from "@/components/ui/alert"; |
| 10 | import { Button } from "@/components/ui/button"; |
| 11 | import { Input } from "@/components/ui/input"; |
| 12 | import { Label } from "@/components/ui/label"; |
| 13 | import { SidePanel } from "@/components/ui/side-panel"; |
| 14 | import { useT } from "@/stores/preferences-context"; |
| 15 | |
| 16 | /** Creates a school with its first admin and, optionally, a hand-picked module set. */ |
| 17 | export function CreateSchoolPanel(props: { open: boolean; onOpenChange: (open: boolean) => void; onCreated: (school: School) => void }) { |
| 18 | const t = useT(); |
| 19 | const [catalog] = createResource(() => props.open || undefined, () => getModulesCatalog()); |
| 20 | const [limits] = createResource( |
| 21 | () => props.open || undefined, |
| 22 | () => getLimits().catch(() => null), |
| 23 | ); |
| 24 | const [name, setName] = createSignal(""); |
| 25 | const [adminUsername, setAdminUsername] = createSignal(""); |
| 26 | const [adminPassword, setAdminPassword] = createSignal(""); |
| 27 | const [everything, setEverything] = createSignal(true); |
| 28 | const [picked, setPicked] = createSignal<string[]>([]); |
| 29 | const [error, setError] = createSignal(""); |
| 30 | const [pending, setPending] = createSignal(false); |
| 31 | |
| 32 | const reset = () => { |
| 33 | setName(""); |
| 34 | setAdminUsername(""); |
| 35 | setAdminPassword(""); |
| 36 | setEverything(true); |
| 37 | setPicked([]); |
| 38 | setError(""); |
| 39 | }; |
| 40 | |
| 41 | const packageModules = (pkg: string) => catalog()?.packages.find((entry) => entry.package === pkg)?.modules ?? []; |
| 42 | |
| 43 | const submit = async (event: SubmitEvent) => { |
| 44 | event.preventDefault(); |
| 45 | setError(""); |
| 46 | setPending(true); |
| 47 | try { |
| 48 | const school = await postSchool({ |
| 49 | name: name().trim(), |
| 50 | admin_username: adminUsername().trim(), |
| 51 | admin_password: adminPassword(), |
| 52 | // Omitted sells the whole catalog; an explicit list is checked for |
| 53 | // missing dependencies before anything is created (409). |
| 54 | modules: everything() ? undefined : picked(), |
| 55 | }); |
| 56 | reset(); |
| 57 | props.onCreated(school); |
| 58 | } catch (err) { |
| 59 | setError(formatModuleError(err, t)); |
| 60 | } finally { |
| 61 | setPending(false); |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | return ( |
| 66 | <SidePanel guardUnsaved |
| 67 | size="wide" |
| 68 | open={props.open} |
| 69 | onOpenChange={(open) => { |
| 70 | props.onOpenChange(open); |
| 71 | if (!open) reset(); |
| 72 | }} |
| 73 | title={t("builder.createSchool")} |
| 74 | description={t("builder.createSchoolHint")} |
| 75 | > |
| 76 | <form class="space-y-4" onSubmit={submit}> |
| 77 | <Show when={error()}> |
| 78 | <Alert variant="destructive">{error()}</Alert> |
| 79 | </Show> |
| 80 | <div class="grid gap-4 sm:grid-cols-2"> |
| 81 | <div class="space-y-1.5"> |
| 82 | <Label for="school-name">{t("builder.schoolName")}</Label> |
| 83 | <Input id="school-name" class="rounded-lg" required maxlength={limits()?.user.max_school_name_len ?? 120} value={name()} onInput={(e) => setName(e.currentTarget.value)} /> |
| 84 | </div> |
| 85 | <div class="space-y-1.5"> |
| 86 | <Label for="school-admin-username">{t("builder.adminUsername")}</Label> |
| 87 | <Input id="school-admin-username" class="rounded-lg" required minlength={limits()?.user.min_username_len ?? 3} maxlength={limits()?.user.max_username_len ?? 32} autocomplete="off" value={adminUsername()} onInput={(e) => setAdminUsername(e.currentTarget.value)} /> |
| 88 | </div> |
| 89 | <div class="space-y-1.5"> |
| 90 | <Label for="school-admin-password">{t("builder.adminPassword")}</Label> |
| 91 | <Input id="school-admin-password" class="rounded-lg" type="password" required minlength={limits()?.user.min_password_len ?? 6} maxlength={limits()?.user.max_password_len ?? 128} autocomplete="new-password" value={adminPassword()} onInput={(e) => setAdminPassword(e.currentTarget.value)} /> |
| 92 | </div> |
| 93 | </div> |
| 94 | <label class="flex items-center gap-2 text-sm"> |
| 95 | <input type="checkbox" checked={everything()} onChange={(e) => setEverything(e.currentTarget.checked)} /> |
| 96 | {t("builder.sellEverything")} |
| 97 | </label> |
| 98 | <Show when={!everything() && catalog()}> |
| 99 | {(value) => ( |
| 100 | <ModuleCatalogGrid |
| 101 | stacked |
| 102 | catalog={value()} |
| 103 | enabled={picked()} |
| 104 | onToggle={(module, next) => setPicked((current) => (next ? [...current, module] : current.filter((name) => name !== module)))} |
| 105 | onTogglePackage={(pkg, next) => |
| 106 | setPicked((current) => { |
| 107 | const modules = packageModules(pkg); |
| 108 | return next ? [...new Set([...current, ...modules])] : current.filter((name) => !modules.includes(name)); |
| 109 | }) |
| 110 | } |
| 111 | /> |
| 112 | )} |
| 113 | </Show> |
| 114 | <div class="flex flex-wrap items-center gap-2"> |
| 115 | <Button type="button" variant="outline" class="h-10 rounded-lg" onClick={() => props.onOpenChange(false)}> |
| 116 | {t("common.cancel")} |
| 117 | </Button> |
| 118 | <Button type="submit" class="h-10 rounded-lg" disabled={pending()}> |
| 119 | {t("common.create")} |
| 120 | </Button> |
| 121 | </div> |
| 122 | </form> |
| 123 | </SidePanel> |
| 124 | ); |
| 125 | } |