"use client"; import { useState, type FormEvent } from "react"; import { Alert, AlertDescription } from "@/app/components/ui/alert"; 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 { ErrorState } from "@/app/components/ui/states"; import { useT } from "@/app/lib/i18n/MessagesProvider"; import { changeAdminUserPassword } from "@/app/lib/services/adminUsers"; const MIN_LENGTH = 6; export function ChangePasswordDialog({ userId, onClose, }: { userId: number; onClose: () => void; }) { const t = useT(); const [password, setPassword] = useState(""); const [confirm, setConfirm] = useState(""); const [fieldError, setFieldError] = useState(null); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); const [saving, setSaving] = useState(false); async function handleSubmit(event: FormEvent) { event.preventDefault(); setError(null); if (!password || !confirm) { setFieldError(t("UsrPwEnterBoth")); return; } if (password.length < MIN_LENGTH) { setFieldError(t("UsrPwTooShort", { min: MIN_LENGTH })); return; } if (password !== confirm) { setFieldError(t("UsrPwMismatch")); return; } setFieldError(null); setSaving(true); try { await changeAdminUserPassword(userId, password); setSuccess(true); } catch (err) { setError(err instanceof Error ? err.message : "UsrPwChangeFailed"); } finally { setSaving(false); } } return ( !open && onClose()}> {t("UsrChangePassword")} {success ? ( <> {t("UsrPwChanged")} ) : (
{error && } {fieldError && (

{fieldError}

)}
setPassword(e.target.value)} disabled={saving} />
setConfirm(e.target.value)} disabled={saving} />
)}
); }