import { Eye, EyeOff } from "lucide-react"; import { useRouter } from "next/router"; import toast from "react-hot-toast"; import { z } from "zod/v4"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "~/components/custom_ui/form"; import Input from "~/components/custom_ui/input"; import { Button } from "~/components/ui/button"; import { useStrongPassword } from "~/hooks/useStrongPassword"; import { cn } from "~/lib/utils"; import { useZodForm } from "~/lib/zodForm"; import { useTranslation } from "~/providers/I18nProvider"; import { api } from "~/utils/api"; export const ChangePasswordForm = () => { const { isVisible, getStrengthColor, passwordRefine, getStrengthTxt, strengthScore, toggleVisibility, } = useStrongPassword(); const router = useRouter(); const ChangePasswordFormSchema = z .object({ oldPsw: z.string().min(1, "Inserisci la password corrente"), newPsw: z.string(), confirmPsw: z.string().min(1, "Ripeti la nuova password"), }) .superRefine(({ oldPsw, newPsw, confirmPsw }, refinementContext) => { passwordRefine({ password: newPsw, path: "newPsw", refinementContext, }); if (oldPsw === newPsw) { refinementContext.addIssue({ code: "custom", message: "La nuova password deve essere diversa da quella corrente", path: ["newPsw"], }); } if (newPsw !== confirmPsw) { refinementContext.addIssue({ code: "custom", message: "Le password non coincidono", path: ["confirmPsw"], }); } }); type ChangePasswordFormValues = z.infer; const { locale, t } = useTranslation(); z.config(z.locales[locale]()); const form = useZodForm(ChangePasswordFormSchema, { defaultValues: { newPsw: "", oldPsw: "", confirmPsw: "", }, }); const { mutate } = api.auth.ChangePassword.useMutation({ onError: (error) => { toast.error(error.message); }, onSuccess: async () => { toast.success(t.pwReset.pwAggiornata); form.reset(); await router.push("/area-riservata/dashboard"); }, }); function onSubmit(fields: ChangePasswordFormValues) { mutate({ newpassword: fields.newPsw, oldpassword: fields.oldPsw, }); } return (
(
{t.pwReset.oldPw}
)} /> (
{t.pwReset.newPw}
{ field.onChange(e); await form.trigger("newPsw"); }} placeholder="Password" type={isVisible ? "text" : "password"} />
)} /> (
{t.pwReset.confirmPw}
)} /> ); };