diff --git a/src/components/users/create-user-panel.tsx b/src/components/users/create-user-panel.tsx new file mode 100644 index 0000000..108d8fd --- /dev/null +++ b/src/components/users/create-user-panel.tsx @@ -0,0 +1,130 @@ +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; +}) { + const t = useT(); + const [limits] = createResource(() => getLimits()); + const [username, setUsername] = createSignal(""); + const [password, setPassword] = createSignal(""); + const [role, setRole] = createSignal("student"); + const [error, setError] = createSignal(""); + const [pending, setPending] = createSignal(false); + + const userLimits = () => limits.latest?.user; + + const reset = () => { + setUsername(""); + setPassword(""); + setRole("student"); + 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 created = await postUser({ username: username().trim(), password: password(), role: role() }); + 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")}

+
+
+ + +
+
+ + +
+
+
+ ); +} diff --git a/src/i18n/messages.ts b/src/i18n/messages.ts index d67cc51..e0986e0 100644 --- a/src/i18n/messages.ts +++ b/src/i18n/messages.ts @@ -1185,6 +1185,12 @@ export type MessageKey = | "admin.tabStudents" | "admin.tabInvites" | "admin.inviteUser" + | "admin.createUser" + | "admin.createUserHint" + | "admin.createUserPassword" + | "admin.createUserPasswordHint" + | "admin.createUserReserved" + | "admin.createUserDone" | "admin.username" | "admin.id" | "admin.role" @@ -3270,6 +3276,12 @@ const en: Dict = { "admin.tabStudents": "Students", "admin.tabInvites": "Invites", "admin.inviteUser": "Invite user", + "admin.createUser": "Add user", + "admin.createUserHint": "Open an account directly — no invite or self-registration.", + "admin.createUserPassword": "Password", + "admin.createUserPasswordHint": "If this username already exists at another school, enter that account's current password to add it here.", + "admin.createUserReserved": "This username is reserved. Pick another one.", + "admin.createUserDone": "Account {username} created.", "admin.username": "Username", "admin.id": "Id", "admin.role": "Role", @@ -5275,6 +5287,12 @@ const tr: Dict = { "admin.tabStudents": "Öğrenciler", "admin.tabInvites": "Davetler", "admin.inviteUser": "Kullanıcı davet et", + "admin.createUser": "Kullanıcı ekle", + "admin.createUserHint": "Davet veya kendi kaydı olmadan doğrudan hesap açın.", + "admin.createUserPassword": "Şifre", + "admin.createUserPasswordHint": "Bu kullanıcı adı başka bir okulda zaten varsa, o hesabın mevcut şifresini girin; hesap bu okula da eklenir.", + "admin.createUserReserved": "Bu kullanıcı adı ayrılmış. Başka bir ad seçin.", + "admin.createUserDone": "{username} hesabı oluşturuldu.", "admin.username": "Kullanıcı adı", "admin.id": "Id", "admin.role": "Rol", diff --git a/src/pages/admin-users-page.tsx b/src/pages/admin-users-page.tsx index b31ea8d..a38227b 100644 --- a/src/pages/admin-users-page.tsx +++ b/src/pages/admin-users-page.tsx @@ -7,11 +7,13 @@ import { formatApiError } from "@/api/client"; import type { Role, User } from "@/api/client"; import { RouteGuard } from "@/components/layout/route-guard"; import { UserTable } from "@/components/users/user-table"; +import { CreateUserPanel } from "@/components/users/create-user-panel"; import { ParentStudentsPanel } from "@/components/users/parent-students-panel"; import { Alert } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { ComingSoonBadge } from "@/components/ui/coming-soon"; import { DataTableSkeleton } from "@/components/ui/data-table"; +import { IconPlus } from "@/components/ui/icons"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { createFlash } from "@/lib/flash"; import { useAuth } from "@/stores/auth-context"; @@ -34,6 +36,7 @@ function AdminUsersContent() { const [error, setError] = createSignal(""); const [selectedParent, setSelectedParent] = createSignal(null); const [roleTab, setRoleTab] = createSignal("all"); + const [creating, setCreating] = createSignal(false); const [list, { refetch }] = createResource(async () => (await getUsers({ limit: 200 })).items); const allUsers = () => list() ?? []; @@ -85,10 +88,16 @@ function AdminUsersContent() { title={t("admin.title")} description={t("admin.subtitle")} actions={ - + <> + + + } users={visibleUsers() as User[]} currentUserId={auth.user()!.id} @@ -99,6 +108,15 @@ function AdminUsersContent() { + { + setCreating(false); + setFlash(t("admin.createUserDone", { username: user.username })); + void refetch(); + }} + /> {(u) => (