Files
co-lite/solvway-admin-portal/app/components/app/ChangePasswordDialog.tsx
T
ngqileandClaude Opus 5.5 8ce9fcf471 feat: portal password hints, company hint, interest and role delete, dashboard notes
- Login: company-code hint after an invalid-credentials failure with an empty company.
- Password forms show the active rules (label-first, count-safe) and tie hint and refusal to the input.
- Interests and roles: soft delete with confirm dialogs; deactivate now confirms too.
- Dashboard figure explanations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-12-02 02:59:44 +08:00

87 lines
3.3 KiB
TypeScript

"use client";
import { useState, type FormEvent } from "react";
import { Button } from "@/app/components/ui/button";
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/app/components/ui/dialog";
import { Input } from "@/app/components/ui/input";
import { Label } from "@/app/components/ui/label";
import { PasswordFields } from "@/app/components/users/PasswordFields";
import { passwordRuleError } from "@/app/components/users/PasswordRulesHint";
import { changeOwnPassword } from "@/app/lib/services/adminUsers";
import { useT } from "@/app/lib/i18n/MessagesProvider";
/** The signed-in admin's own password change; mounted only while open, so every field starts empty. */
export default function ChangePasswordDialog({ onClose, onDone }: { onClose: () => void; onDone: () => void }) {
const t = useT();
const [current, setCurrent] = useState("");
const [password, setPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [error, setError] = useState<string | null>(null);
const [saving, setSaving] = useState(false);
const [ruleError, setRuleError] = useState<string | undefined>();
async function submit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
if (!current || !password || !confirmPassword) return setError("PwdChangeRequired");
if (password !== confirmPassword) return setError("UsrPasswordMismatch");
setSaving(true);
setError(null);
setRuleError(undefined);
try {
await changeOwnPassword(current, password, confirmPassword);
onClose();
onDone();
} catch (err) {
const rule = passwordRuleError(err, t);
if (rule) setRuleError(rule);
else setError(err instanceof Error ? err.message : "PwdChangeRequired");
setSaving(false);
}
}
return (
<Dialog open onOpenChange={(o) => !o && onClose()}>
<DialogContent>
<DialogHeader>
<DialogTitle>{t("PwdChangeMenu")}</DialogTitle>
</DialogHeader>
<form onSubmit={submit} className="space-y-4" noValidate>
<div className="space-y-1.5">
<Label htmlFor="current-password">{t("PwdCurrent")}</Label>
<Input
id="current-password"
type="password"
autoComplete="current-password"
value={current}
onChange={(e) => setCurrent(e.target.value)}
disabled={saving}
/>
</div>
<PasswordFields
passwordLabel="PwdNew"
password={password}
confirmPassword={confirmPassword}
onPassword={setPassword}
onConfirmPassword={setConfirmPassword}
disabled={saving}
passwordError={ruleError}
/>
{error && (
<div role="alert" className="rounded-md border border-destructive/30 bg-destructive/5 p-3 text-sm text-destructive">
{t(error)}
</div>
)}
<DialogFooter>
<Button type="button" variant="outline" onClick={onClose}>
{t("Cancel")}
</Button>
<Button type="submit" disabled={saving}>
{t("PwdChangeMenu")}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
}