import { For, Show, createSignal } from "solid-js"; import { createResource } from "@/lib/create-resource"; import { formatApiError } from "@/api/client"; import type { Role, User } from "@/api/client"; import { getLimits } from "@/api/limits"; import { postUser } from "@/api/users"; import type { MessageKey } from "@/i18n/messages"; import { Alert } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select } from "@/components/ui/select"; import { SidePanel } from "@/components/ui/side-panel"; import { ROLES } from "@/lib/roles"; import { useT } from "@/stores/preferences-context"; /** * Admin-only: opens a school account directly (POST /users), no invite and no * self-registration. A username that already exists as a person elsewhere is * attached to this school only when the password matches — the server's 409 * says so, the form just relays it. */ export function CreateUserPanel(props: { open: boolean; onOpenChange: (open: boolean) => void; onCreated: (user: User) => void; /** Fix the new account to one role when opened from a role-specific roster. */ fixedRole?: Role; }) { const t = useT(); const [limits] = createResource(() => getLimits()); const [username, setUsername] = createSignal(""); const [password, setPassword] = createSignal(""); const [role, setRole] = createSignal(props.fixedRole ?? "student"); const [studentNumber, setStudentNumber] = createSignal(""); const [error, setError] = createSignal(""); const [pending, setPending] = createSignal(false); const userLimits = () => limits.latest?.user; // Only a student account may hold a number; the server 400s on any other // role, so the field is asked for — and sent — only when the role is student. const effectiveRole = () => props.fixedRole ?? role(); const reset = () => { setUsername(""); setPassword(""); setRole(props.fixedRole ?? "student"); setStudentNumber(""); setError(""); }; const close = () => { reset(); props.onOpenChange(false); }; const validate = (): string | null => { const reserved = userLimits()?.reserved_usernames ?? []; const name = username().trim().toLowerCase(); if (reserved.some((r) => r.toLowerCase() === name)) return t("admin.createUserReserved"); return null; }; const submit = async (event: SubmitEvent) => { event.preventDefault(); setError(""); const msg = validate(); if (msg) { setError(msg); return; } setPending(true); try { const number = studentNumber().trim(); const created = await postUser({ username: username().trim(), password: password(), role: effectiveRole(), // Blank is the same as omitted, so an unnumbered student stays unnumbered. ...(effectiveRole() === "student" && number ? { student_number: number } : {}), }); reset(); props.onCreated(created); } catch (err) { setError(formatApiError(err)); } finally { setPending(false); } }; return ( { if (!open) close(); }} title={t("admin.createUser")} description={t("admin.createUserHint")} >
{error()}
setUsername(e.currentTarget.value)} />
setPassword(e.currentTarget.value)} />

{t("admin.createUserPasswordHint")}

setStudentNumber(e.currentTarget.value)} />

{t("admin.studentNumberHint")}

); }