hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { For, Show, createSignal } from "solid-js"; |
| 2 | import { createResource } from "@/lib/create-resource"; |
| 3 | import { formatApiError } from "@/api/client"; |
| 4 | import type { Role, User } from "@/api/client"; |
| 5 | import { getLimits } from "@/api/limits"; |
| 6 | import { postUser } from "@/api/users"; |
| 7 | import type { MessageKey } from "@/i18n/messages"; |
| 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 { Select } from "@/components/ui/select"; |
| 13 | import { SidePanel } from "@/components/ui/side-panel"; |
| 14 | import { ROLES } from "@/lib/roles"; |
| 15 | import { useT } from "@/stores/preferences-context"; |
| 16 | |
| 17 | /** |
| 18 | * Admin-only: opens a school account directly (POST /users), no invite and no |
| 19 | * self-registration. A username that already exists as a person elsewhere is |
| 20 | * attached to this school only when the password matches — the server's 409 |
| 21 | * says so, the form just relays it. |
| 22 | */ |
| 23 | export function CreateUserPanel(props: { |
| 24 | open: boolean; |
| 25 | onOpenChange: (open: boolean) => void; |
| 26 | onCreated: (user: User) => void; |
| 27 | /** Fix the new account to one role when opened from a role-specific roster. */ |
| 28 | fixedRole?: Role; |
| 29 | }) { |
| 30 | const t = useT(); |
| 31 | const [limits] = createResource(() => getLimits()); |
| 32 | const [username, setUsername] = createSignal(""); |
| 33 | const [password, setPassword] = createSignal(""); |
| 34 | const [role, setRole] = createSignal<Role>(props.fixedRole ?? "student"); |
| 35 | const [studentNumber, setStudentNumber] = createSignal(""); |
| 36 | const [error, setError] = createSignal(""); |
| 37 | const [pending, setPending] = createSignal(false); |
| 38 | |
| 39 | const userLimits = () => limits.latest?.user; |
| 40 | // Only a student account may hold a number; the server 400s on any other |
| 41 | // role, so the field is asked for — and sent — only when the role is student. |
| 42 | const effectiveRole = () => props.fixedRole ?? role(); |
| 43 | |
| 44 | const reset = () => { |
| 45 | setUsername(""); |
| 46 | setPassword(""); |
| 47 | setRole(props.fixedRole ?? "student"); |
| 48 | setStudentNumber(""); |
| 49 | setError(""); |
| 50 | }; |
| 51 | |
| 52 | const close = () => { |
| 53 | reset(); |
| 54 | props.onOpenChange(false); |
| 55 | }; |
| 56 | |
| 57 | const validate = (): string | null => { |
| 58 | const reserved = userLimits()?.reserved_usernames ?? []; |
| 59 | const name = username().trim().toLowerCase(); |
| 60 | if (reserved.some((r) => r.toLowerCase() === name)) return t("admin.createUserReserved"); |
| 61 | return null; |
| 62 | }; |
| 63 | |
| 64 | const submit = async (event: SubmitEvent) => { |
| 65 | event.preventDefault(); |
| 66 | setError(""); |
| 67 | const msg = validate(); |
| 68 | if (msg) { setError(msg); return; } |
| 69 | setPending(true); |
| 70 | try { |
| 71 | const number = studentNumber().trim(); |
| 72 | const created = await postUser({ |
| 73 | username: username().trim(), |
| 74 | password: password(), |
| 75 | role: effectiveRole(), |
| 76 | // Blank is the same as omitted, so an unnumbered student stays unnumbered. |
| 77 | ...(effectiveRole() === "student" && number ? { student_number: number } : {}), |
| 78 | }); |
| 79 | reset(); |
| 80 | props.onCreated(created); |
| 81 | } catch (err) { |
| 82 | setError(formatApiError(err)); |
| 83 | } finally { |
| 84 | setPending(false); |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | return ( |
| 89 | <SidePanel guardUnsaved |
| 90 | open={props.open} |
| 91 | onOpenChange={(open) => { if (!open) close(); }} |
| 92 | title={t("admin.createUser")} |
| 93 | description={t("admin.createUserHint")} |
| 94 | > |
| 95 | <form class="space-y-4" onSubmit={submit}> |
| 96 | <Show when={error()}> |
| 97 | <Alert variant="destructive">{error()}</Alert> |
| 98 | </Show> |
| 99 | <div class="space-y-1.5"> |
| 100 | <Label for="create-user-username">{t("admin.username")}</Label> |
| 101 | <Input |
| 102 | id="create-user-username" |
| 103 | class="h-10 rounded-lg" |
| 104 | required |
| 105 | minlength={userLimits()?.min_username_len} |
| 106 | maxlength={userLimits()?.max_username_len} |
| 107 | autocomplete="off" |
| 108 | value={username()} |
| 109 | onInput={(e) => setUsername(e.currentTarget.value)} |
| 110 | /> |
| 111 | </div> |
| 112 | <div class="space-y-1.5"> |
| 113 | <Label for="create-user-password">{t("admin.createUserPassword")}</Label> |
| 114 | <Input |
| 115 | id="create-user-password" |
| 116 | class="h-10 rounded-lg" |
| 117 | type="password" |
| 118 | required |
| 119 | minlength={userLimits()?.min_password_len} |
| 120 | maxlength={userLimits()?.max_password_len} |
| 121 | autocomplete="new-password" |
| 122 | value={password()} |
| 123 | onInput={(e) => setPassword(e.currentTarget.value)} |
| 124 | /> |
| 125 | <p class="text-xs text-muted-foreground">{t("admin.createUserPasswordHint")}</p> |
| 126 | </div> |
| 127 | <Show when={!props.fixedRole}> |
| 128 | <div class="space-y-1.5"> |
| 129 | <Label for="create-user-role">{t("admin.role")}</Label> |
| 130 | <Select id="create-user-role" class="h-10" value={role()} onChange={(e) => setRole(e.currentTarget.value as Role)}> |
| 131 | <For each={ROLES}>{(r) => <option value={r}>{t(`role.${r}` as MessageKey)}</option>}</For> |
| 132 | </Select> |
| 133 | </div> |
| 134 | </Show> |
| 135 | <Show when={effectiveRole() === "student"}> |
| 136 | <div class="space-y-1.5"> |
| 137 | <Label for="create-user-student-number">{t("roster.studentNumber")}</Label> |
| 138 | <Input |
| 139 | id="create-user-student-number" |
| 140 | class="h-10 rounded-lg" |
| 141 | maxlength={userLimits()?.max_student_number_len} |
| 142 | autocomplete="off" |
| 143 | value={studentNumber()} |
| 144 | onInput={(e) => setStudentNumber(e.currentTarget.value)} |
| 145 | /> |
| 146 | <p class="text-xs text-muted-foreground">{t("admin.studentNumberHint")}</p> |
| 147 | </div> |
| 148 | </Show> |
| 149 | <div class="flex flex-wrap items-center justify-end gap-2"> |
| 150 | <Button type="button" variant="outline" class="h-10 rounded-lg" onClick={close}> |
| 151 | {t("common.cancel")} |
| 152 | </Button> |
| 153 | <Button type="submit" class="h-10 rounded-lg" disabled={pending()}> |
| 154 | {t("admin.createUser")} |
| 155 | </Button> |
| 156 | </div> |
| 157 | </form> |
| 158 | </SidePanel> |
| 159 | ); |
| 160 | } |