"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 { setAdminUserPassword } from "@/app/lib/services/adminUsers"; import { useT } from "@/app/lib/i18n/MessagesProvider"; import { PasswordFields } from "./PasswordFields"; /** "Set temporary password" on the user edit page: the admin types it; the user must change it at next sign-in. */ export function SetPasswordButton({ userId, onDone }: { userId: number; onDone: () => void }) { const t = useT(); const [open, setOpen] = useState(false); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [error, setError] = useState(null); const [saving, setSaving] = useState(false); function close() { // The password lives only in this state; clear it whenever the dialog closes. setOpen(false); setPassword(""); setConfirmPassword(""); setError(null); } async function submit(event: FormEvent) { event.preventDefault(); if (!password) return setError("UsrPasswordRequired"); if (password !== confirmPassword) return setError("UsrPasswordMismatch"); setSaving(true); setError(null); try { await setAdminUserPassword(userId, password, confirmPassword); close(); onDone(); } catch (err) { setError(err instanceof Error ? err.message : "UsrSetPasswordFailed"); } finally { setSaving(false); } } return ( <> {open && ( !o && close()}> {t("UsrSetTempPassword")}

{t("UsrSetTempPasswordBody")}

{error && (
{t(error)}
)}
)} ); }