import { z } from "zod/v4"; import { useZodForm } from "~/lib/zodForm"; 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 toast from "react-hot-toast"; import { api } from "~/utils/api"; import { useTranslation } from "~/providers/I18nProvider"; import { useStrongPassword } from "~/hooks/useStrongPassword"; import { cn } from "~/lib/utils"; import { Eye, EyeOff } from "lucide-react"; export const ChangePasswordForm = () => { const { isVisible, getStrengthColor, passwordRefine, getStrengthTxt, strengthScore, toggleVisibility, } = useStrongPassword(); const ChangePasswordFormSchema = z .object({ oldpassword: z.string(), newpassword: z.string(), }) .superRefine(({ newpassword }, refinementContext) => { passwordRefine({ password: newpassword, refinementContext, path: "newpassword", }); }); type ChangePasswordFormValues = z.infer; const { locale, t } = useTranslation(); z.config(z.locales[locale]()); const form = useZodForm(ChangePasswordFormSchema, { defaultValues: { oldpassword: "", newpassword: "", }, }); const { mutate } = api.auth.ChangePassword.useMutation({ onSuccess: () => { toast.success(t.pwReset.pwAggiornata); form.reset(); }, onError: (error) => { toast.error(error.message); }, }); function onSubmit(fields: ChangePasswordFormValues) { mutate({ oldpassword: fields.oldpassword, newpassword: fields.newpassword, }); } return (
(
{t.pwReset.oldPw}
)} /> (
{t.pwReset.newPw}
{ field.onChange(e); await form.trigger("newpassword"); }} aria-invalid={form.getFieldState("newpassword").invalid} aria-describedby="password-strength" />
)} /> ); };