feat(webui): add create-user dialog
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
"use client";
|
||||
|
||||
import { FormEvent, useEffect, useState } from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { toast } from "sonner";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import {
|
||||
isValidPassword,
|
||||
isValidUsername,
|
||||
type ManagedUserRole,
|
||||
} from "@/lib/userManagement";
|
||||
|
||||
interface UserCreateDialogProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
onChanged: () => Promise<void>;
|
||||
}
|
||||
|
||||
export function UserCreateDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
onChanged,
|
||||
}: UserCreateDialogProps) {
|
||||
const t = useTranslations("userManagement");
|
||||
const [username, setUsername] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const [role, setRole] = useState<ManagedUserRole>("user");
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) {
|
||||
setUsername("");
|
||||
setPassword("");
|
||||
setRole("user");
|
||||
setError(null);
|
||||
setSaving(false);
|
||||
}
|
||||
}, [open]);
|
||||
|
||||
const canSubmit =
|
||||
isValidUsername(username.trim()) && isValidPassword(password) && !saving;
|
||||
|
||||
const submit = async (event: FormEvent<HTMLFormElement>) => {
|
||||
event.preventDefault();
|
||||
if (!canSubmit) return;
|
||||
setSaving(true);
|
||||
setError(null);
|
||||
try {
|
||||
const response = await fetch("/api/auth/users", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
credentials: "same-origin",
|
||||
body: JSON.stringify({ username: username.trim(), password, role }),
|
||||
});
|
||||
const body = (await response.json().catch(() => ({}))) as {
|
||||
message?: string;
|
||||
error?: string;
|
||||
};
|
||||
if (!response.ok) {
|
||||
throw new Error(body.message || body.error || "Request failed.");
|
||||
}
|
||||
toast.success(t("create.created", { username: username.trim() }));
|
||||
onOpenChange(false);
|
||||
await onChanged();
|
||||
} catch (reason) {
|
||||
setError(reason instanceof Error ? reason.message : "Request failed.");
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="sm:max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{t("create.title")}</DialogTitle>
|
||||
<DialogDescription>{t("create.description")}</DialogDescription>
|
||||
</DialogHeader>
|
||||
<form method="post" onSubmit={submit} className="space-y-4">
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="create-username">{t("create.username")}</Label>
|
||||
<Input
|
||||
id="create-username"
|
||||
autoComplete="off"
|
||||
value={username}
|
||||
onChange={(event) => setUsername(event.target.value)}
|
||||
disabled={saving}
|
||||
autoFocus
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{t("create.usernameHint")}
|
||||
</p>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="create-password">{t("create.password")}</Label>
|
||||
<Input
|
||||
id="create-password"
|
||||
type="password"
|
||||
autoComplete="new-password"
|
||||
value={password}
|
||||
onChange={(event) => setPassword(event.target.value)}
|
||||
disabled={saving}
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{t("create.passwordHint")}
|
||||
</p>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="create-role">{t("create.role")}</Label>
|
||||
<Select
|
||||
value={role}
|
||||
onValueChange={(value) =>
|
||||
setRole(value === "admin" ? "admin" : "user")
|
||||
}
|
||||
disabled={saving}
|
||||
>
|
||||
<SelectTrigger id="create-role" className="w-full">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="user">{t("roles.user")}</SelectItem>
|
||||
<SelectItem value="admin">{t("roles.admin")}</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
{error && (
|
||||
<p role="alert" className="text-sm text-destructive">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
<DialogFooter>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => onOpenChange(false)}
|
||||
disabled={saving}
|
||||
>
|
||||
{t("create.cancel")}
|
||||
</Button>
|
||||
<Button type="submit" disabled={!canSubmit}>
|
||||
{saving ? t("create.creating") : t("create.submit")}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user