import { Eye, EyeOff } from "lucide-react"; 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 ChangePasswordFormSchema = z .object({ newpassword: z.string(), oldpassword: z.string(), }) .superRefine(({ newpassword }, refinementContext) => { passwordRefine({ password: newpassword, path: "newpassword", refinementContext, }); }); type ChangePasswordFormValues = z.infer; const { locale, t } = useTranslation(); z.config(z.locales[locale]()); const form = useZodForm(ChangePasswordFormSchema, { defaultValues: { newpassword: "", oldpassword: "", }, }); const { mutate } = api.auth.ChangePassword.useMutation({ onError: (error) => { toast.error(error.message); }, onSuccess: () => { toast.success(t.pwReset.pwAggiornata); form.reset(); }, }); function onSubmit(fields: ChangePasswordFormValues) { mutate({ newpassword: fields.newpassword, oldpassword: fields.oldpassword, }); } return (
(
{t.pwReset.oldPw}
)} /> (
{t.pwReset.newPw}
{ field.onChange(e); await form.trigger("newpassword"); }} placeholder="Password" type={isVisible ? "text" : "password"} />
)} /> ); };