👤 admins add a school account directly
Browse files at this commit082ed247cf390d0d11d6f13570d0ccb69e373ce2Parent: 2c19dff- feat
- 👤 admins add a school account directly
3 changed files +170 -4
- src/components/users/create-user-panel.tsx +130 -0
- src/i18n/messages.ts +18 -0
- src/pages/admin-users-page.tsx +22 -4
src/components/users/create-user-panel.tsxadded+130 -0
| @@ -0,0 +1,130 @@ | |||
| 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 | +}) { | ||
| 28 | + const t = useT(); | ||
| 29 | + const [limits] = createResource(() => getLimits()); | ||
| 30 | + const [username, setUsername] = createSignal(""); | ||
| 31 | + const [password, setPassword] = createSignal(""); | ||
| 32 | + const [role, setRole] = createSignal<Role>("student"); | ||
| 33 | + const [error, setError] = createSignal(""); | ||
| 34 | + const [pending, setPending] = createSignal(false); | ||
| 35 | + | ||
| 36 | + const userLimits = () => limits.latest?.user; | ||
| 37 | + | ||
| 38 | + const reset = () => { | ||
| 39 | + setUsername(""); | ||
| 40 | + setPassword(""); | ||
| 41 | + setRole("student"); | ||
| 42 | + setError(""); | ||
| 43 | + }; | ||
| 44 | + | ||
| 45 | + const close = () => { | ||
| 46 | + reset(); | ||
| 47 | + props.onOpenChange(false); | ||
| 48 | + }; | ||
| 49 | + | ||
| 50 | + const validate = (): string | null => { | ||
| 51 | + const reserved = userLimits()?.reserved_usernames ?? []; | ||
| 52 | + const name = username().trim().toLowerCase(); | ||
| 53 | + if (reserved.some((r) => r.toLowerCase() === name)) return t("admin.createUserReserved"); | ||
| 54 | + return null; | ||
| 55 | + }; | ||
| 56 | + | ||
| 57 | + const submit = async (event: SubmitEvent) => { | ||
| 58 | + event.preventDefault(); | ||
| 59 | + setError(""); | ||
| 60 | + const msg = validate(); | ||
| 61 | + if (msg) { setError(msg); return; } | ||
| 62 | + setPending(true); | ||
| 63 | + try { | ||
| 64 | + const created = await postUser({ username: username().trim(), password: password(), role: role() }); | ||
| 65 | + reset(); | ||
| 66 | + props.onCreated(created); | ||
| 67 | + } catch (err) { | ||
| 68 | + setError(formatApiError(err)); | ||
| 69 | + } finally { | ||
| 70 | + setPending(false); | ||
| 71 | + } | ||
| 72 | + }; | ||
| 73 | + | ||
| 74 | + return ( | ||
| 75 | + <SidePanel | ||
| 76 | + open={props.open} | ||
| 77 | + onOpenChange={(open) => { if (!open) close(); }} | ||
| 78 | + title={t("admin.createUser")} | ||
| 79 | + description={t("admin.createUserHint")} | ||
| 80 | + > | ||
| 81 | + <form class="space-y-4" onSubmit={submit}> | ||
| 82 | + <Show when={error()}> | ||
| 83 | + <Alert variant="destructive">{error()}</Alert> | ||
| 84 | + </Show> | ||
| 85 | + <div class="space-y-1.5"> | ||
| 86 | + <Label for="create-user-username">{t("admin.username")}</Label> | ||
| 87 | + <Input | ||
| 88 | + id="create-user-username" | ||
| 89 | + class="h-10 rounded-lg" | ||
| 90 | + required | ||
| 91 | + minlength={userLimits()?.min_username_len} | ||
| 92 | + maxlength={userLimits()?.max_username_len} | ||
| 93 | + autocomplete="off" | ||
| 94 | + value={username()} | ||
| 95 | + onInput={(e) => setUsername(e.currentTarget.value)} | ||
| 96 | + /> | ||
| 97 | + </div> | ||
| 98 | + <div class="space-y-1.5"> | ||
| 99 | + <Label for="create-user-password">{t("admin.createUserPassword")}</Label> | ||
| 100 | + <Input | ||
| 101 | + id="create-user-password" | ||
| 102 | + class="h-10 rounded-lg" | ||
| 103 | + type="password" | ||
| 104 | + required | ||
| 105 | + minlength={userLimits()?.min_password_len} | ||
| 106 | + maxlength={userLimits()?.max_password_len} | ||
| 107 | + autocomplete="new-password" | ||
| 108 | + value={password()} | ||
| 109 | + onInput={(e) => setPassword(e.currentTarget.value)} | ||
| 110 | + /> | ||
| 111 | + <p class="text-xs text-muted-foreground">{t("admin.createUserPasswordHint")}</p> | ||
| 112 | + </div> | ||
| 113 | + <div class="space-y-1.5"> | ||
| 114 | + <Label for="create-user-role">{t("admin.role")}</Label> | ||
| 115 | + <Select id="create-user-role" class="h-10" value={role()} onChange={(e) => setRole(e.currentTarget.value as Role)}> | ||
| 116 | + <For each={ROLES}>{(r) => <option value={r}>{t(`role.${r}` as MessageKey)}</option>}</For> | ||
| 117 | + </Select> | ||
| 118 | + </div> | ||
| 119 | + <div class="flex flex-wrap items-center justify-end gap-2"> | ||
| 120 | + <Button type="button" variant="outline" class="h-10 rounded-lg" onClick={close}> | ||
| 121 | + {t("common.cancel")} | ||
| 122 | + </Button> | ||
| 123 | + <Button type="submit" class="h-10 rounded-lg" disabled={pending()}> | ||
| 124 | + {t("admin.createUser")} | ||
| 125 | + </Button> | ||
| 126 | + </div> | ||
| 127 | + </form> | ||
| 128 | + </SidePanel> | ||
| 129 | + ); | ||
| 130 | +} | ||
src/i18n/messages.tsmodified+18 -0
| @@ -1185,6 +1185,12 @@ export type MessageKey = | |||
| 1185 | | "admin.tabStudents" | 1185 | | "admin.tabStudents" |
| 1186 | | "admin.tabInvites" | 1186 | | "admin.tabInvites" |
| 1187 | | "admin.inviteUser" | 1187 | | "admin.inviteUser" |
| 1188 | + | "admin.createUser" | ||
| 1189 | + | "admin.createUserHint" | ||
| 1190 | + | "admin.createUserPassword" | ||
| 1191 | + | "admin.createUserPasswordHint" | ||
| 1192 | + | "admin.createUserReserved" | ||
| 1193 | + | "admin.createUserDone" | ||
| 1188 | | "admin.username" | 1194 | | "admin.username" |
| 1189 | | "admin.id" | 1195 | | "admin.id" |
| 1190 | | "admin.role" | 1196 | | "admin.role" |
| @@ -3270,6 +3276,12 @@ const en: Dict = { | |||
| 3270 | "admin.tabStudents": "Students", | 3276 | "admin.tabStudents": "Students", |
| 3271 | "admin.tabInvites": "Invites", | 3277 | "admin.tabInvites": "Invites", |
| 3272 | "admin.inviteUser": "Invite user", | 3278 | "admin.inviteUser": "Invite user", |
| 3279 | + "admin.createUser": "Add user", | ||
| 3280 | + "admin.createUserHint": "Open an account directly — no invite or self-registration.", | ||
| 3281 | + "admin.createUserPassword": "Password", | ||
| 3282 | + "admin.createUserPasswordHint": "If this username already exists at another school, enter that account's current password to add it here.", | ||
| 3283 | + "admin.createUserReserved": "This username is reserved. Pick another one.", | ||
| 3284 | + "admin.createUserDone": "Account {username} created.", | ||
| 3273 | "admin.username": "Username", | 3285 | "admin.username": "Username", |
| 3274 | "admin.id": "Id", | 3286 | "admin.id": "Id", |
| 3275 | "admin.role": "Role", | 3287 | "admin.role": "Role", |
| @@ -5275,6 +5287,12 @@ const tr: Dict = { | |||
| 5275 | "admin.tabStudents": "Öğrenciler", | 5287 | "admin.tabStudents": "Öğrenciler", |
| 5276 | "admin.tabInvites": "Davetler", | 5288 | "admin.tabInvites": "Davetler", |
| 5277 | "admin.inviteUser": "Kullanıcı davet et", | 5289 | "admin.inviteUser": "Kullanıcı davet et", |
| 5290 | + "admin.createUser": "Kullanıcı ekle", | ||
| 5291 | + "admin.createUserHint": "Davet veya kendi kaydı olmadan doğrudan hesap açın.", | ||
| 5292 | + "admin.createUserPassword": "Şifre", | ||
| 5293 | + "admin.createUserPasswordHint": "Bu kullanıcı adı başka bir okulda zaten varsa, o hesabın mevcut şifresini girin; hesap bu okula da eklenir.", | ||
| 5294 | + "admin.createUserReserved": "Bu kullanıcı adı ayrılmış. Başka bir ad seçin.", | ||
| 5295 | + "admin.createUserDone": "{username} hesabı oluşturuldu.", | ||
| 5278 | "admin.username": "Kullanıcı adı", | 5296 | "admin.username": "Kullanıcı adı", |
| 5279 | "admin.id": "Id", | 5297 | "admin.id": "Id", |
| 5280 | "admin.role": "Rol", | 5298 | "admin.role": "Rol", |
src/pages/admin-users-page.tsxmodified+22 -4
| @@ -7,11 +7,13 @@ import { formatApiError } from "@/api/client"; | |||
| 7 | import type { Role, User } from "@/api/client"; | 7 | import type { Role, User } from "@/api/client"; |
| 8 | import { RouteGuard } from "@/components/layout/route-guard"; | 8 | import { RouteGuard } from "@/components/layout/route-guard"; |
| 9 | import { UserTable } from "@/components/users/user-table"; | 9 | import { UserTable } from "@/components/users/user-table"; |
| 10 | +import { CreateUserPanel } from "@/components/users/create-user-panel"; | ||
| 10 | import { ParentStudentsPanel } from "@/components/users/parent-students-panel"; | 11 | import { ParentStudentsPanel } from "@/components/users/parent-students-panel"; |
| 11 | import { Alert } from "@/components/ui/alert"; | 12 | import { Alert } from "@/components/ui/alert"; |
| 12 | import { Button } from "@/components/ui/button"; | 13 | import { Button } from "@/components/ui/button"; |
| 13 | import { ComingSoonBadge } from "@/components/ui/coming-soon"; | 14 | import { ComingSoonBadge } from "@/components/ui/coming-soon"; |
| 14 | import { DataTableSkeleton } from "@/components/ui/data-table"; | 15 | import { DataTableSkeleton } from "@/components/ui/data-table"; |
| 16 | +import { IconPlus } from "@/components/ui/icons"; | ||
| 15 | import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; | 17 | import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; |
| 16 | import { createFlash } from "@/lib/flash"; | 18 | import { createFlash } from "@/lib/flash"; |
| 17 | import { useAuth } from "@/stores/auth-context"; | 19 | import { useAuth } from "@/stores/auth-context"; |
| @@ -34,6 +36,7 @@ function AdminUsersContent() { | |||
| 34 | const [error, setError] = createSignal(""); | 36 | const [error, setError] = createSignal(""); |
| 35 | const [selectedParent, setSelectedParent] = createSignal<User | null>(null); | 37 | const [selectedParent, setSelectedParent] = createSignal<User | null>(null); |
| 36 | const [roleTab, setRoleTab] = createSignal<RoleTab>("all"); | 38 | const [roleTab, setRoleTab] = createSignal<RoleTab>("all"); |
| 39 | + const [creating, setCreating] = createSignal(false); | ||
| 37 | 40 | ||
| 38 | const [list, { refetch }] = createResource(async () => (await getUsers({ limit: 200 })).items); | 41 | const [list, { refetch }] = createResource(async () => (await getUsers({ limit: 200 })).items); |
| 39 | const allUsers = () => list() ?? []; | 42 | const allUsers = () => list() ?? []; |
| @@ -85,10 +88,16 @@ function AdminUsersContent() { | |||
| 85 | title={t("admin.title")} | 88 | title={t("admin.title")} |
| 86 | description={t("admin.subtitle")} | 89 | description={t("admin.subtitle")} |
| 87 | actions={ | 90 | actions={ |
| 88 | - <Button size="sm" variant="outline" class="rounded-lg" disabled title={t("comingSoon.title")}> | 91 | + <> |
| 89 | - {t("admin.inviteUser")} | 92 | + <Button size="sm" variant="outline" class="min-w-[7.5rem] rounded-lg" disabled title={t("comingSoon.title")}> |
| 90 | - <ComingSoonBadge class="ml-1.5" /> | 93 | + {t("admin.inviteUser")} |
| 91 | - </Button> | 94 | + <ComingSoonBadge class="ml-1.5" /> |
| 95 | + </Button> | ||
| 96 | + <Button size="sm" class="min-w-[7.5rem] rounded-lg" onClick={() => setCreating(true)}> | ||
| 97 | + <IconPlus class="mr-1.5 h-4 w-4" /> | ||
| 98 | + {t("admin.createUser")} | ||
| 99 | + </Button> | ||
| 100 | + </> | ||
| 92 | } | 101 | } |
| 93 | users={visibleUsers() as User[]} | 102 | users={visibleUsers() as User[]} |
| 94 | currentUserId={auth.user()!.id} | 103 | currentUserId={auth.user()!.id} |
| @@ -99,6 +108,15 @@ function AdminUsersContent() { | |||
| 99 | </Show> | 108 | </Show> |
| 100 | </Suspense> | 109 | </Suspense> |
| 101 | </section> | 110 | </section> |
| 111 | + <CreateUserPanel | ||
| 112 | + open={creating()} | ||
| 113 | + onOpenChange={setCreating} | ||
| 114 | + onCreated={(user) => { | ||
| 115 | + setCreating(false); | ||
| 116 | + setFlash(t("admin.createUserDone", { username: user.username })); | ||
| 117 | + void refetch(); | ||
| 118 | + }} | ||
| 119 | + /> | ||
| 102 | <Show when={selectedParent()} keyed> | 120 | <Show when={selectedParent()} keyed> |
| 103 | {(u) => ( | 121 | {(u) => ( |
| 104 | <ParentStudentsPanel | 122 | <ParentStudentsPanel |