Files
EvoScientist-WebUI/src/app/components/ChangePasswordDialog.tsx
T
m4 4e66e6a7f4 refactor: split i18n catalogs into domain modules, rework dialog components
In-progress work committed to unblock the config import/export plan:
- i18n: new changePassword/modelConfig/imageModels/systemConfig catalogs (zh+en)
- AccountDialog/ChangePasswordDialog/ConfigDialog/ImageModelsDialog rewired
- RegistryEditor/ImageModelsEditor updated for the new catalogs
- system-config tabs restyled; page/layout follow-ons
2026-08-12 19:45:18 +08:00

144 lines
4.2 KiB
TypeScript

"use client";
import { FormEvent, useEffect, useState } from "react";
import { useTranslations } from "next-intl";
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";
interface ChangePasswordDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
}
export function ChangePasswordDialog({
open,
onOpenChange,
}: ChangePasswordDialogProps) {
const t = useTranslations("changePassword");
const [currentPassword, setCurrentPassword] = useState("");
const [newPassword, setNewPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [error, setError] = useState<string | null>(null);
const [saving, setSaving] = useState(false);
useEffect(() => {
if (!open) {
setCurrentPassword("");
setNewPassword("");
setConfirmPassword("");
setError(null);
setSaving(false);
}
}, [open]);
const submit = async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
if (saving) return;
setSaving(true);
setError(null);
try {
const response = await fetch("/api/auth/password", {
method: "POST",
headers: { "Content-Type": "application/json" },
credentials: "same-origin",
body: JSON.stringify({
currentPassword,
newPassword,
confirmPassword,
}),
});
const body = (await response.json().catch(() => ({}))) as {
error?: string;
};
if (!response.ok) {
throw new Error(body.error || t("failed"));
}
onOpenChange(false);
} catch (reason) {
setError(
reason instanceof Error ? reason.message : t("failed")
);
setSaving(false);
}
};
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>{t("title")}</DialogTitle>
<DialogDescription>{t("description")}</DialogDescription>
</DialogHeader>
<form method="post" onSubmit={submit} className="space-y-4">
<div className="space-y-1.5">
<Label htmlFor="current-password">{t("currentPassword")}</Label>
<Input
id="current-password"
type="password"
autoComplete="current-password"
value={currentPassword}
onChange={(event) => setCurrentPassword(event.target.value)}
disabled={saving}
required
autoFocus
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="new-password">{t("newPassword")}</Label>
<Input
id="new-password"
type="password"
autoComplete="new-password"
value={newPassword}
onChange={(event) => setNewPassword(event.target.value)}
disabled={saving}
minLength={8}
required
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="confirm-password">{t("confirmPassword")}</Label>
<Input
id="confirm-password"
type="password"
autoComplete="new-password"
value={confirmPassword}
onChange={(event) => setConfirmPassword(event.target.value)}
disabled={saving}
minLength={8}
required
/>
</div>
{error && (
<p role="alert" className="text-sm text-destructive">
{error}
</p>
)}
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => onOpenChange(false)}
disabled={saving}
>
{t("cancel")}
</Button>
<Button type="submit" disabled={saving}>
{saving ? t("saving") : t("submit")}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
}