infoalloggi-monorepo/apps/infoalloggi/src/forms/FormChangePassword.tsx

211 lines
5.7 KiB
TypeScript
Raw Normal View History

2025-08-28 18:27:07 +02:00
import { Eye, EyeOff } from "lucide-react";
import { useRouter } from "next/router";
2025-08-28 18:27:07 +02:00
import toast from "react-hot-toast";
import { z } from "zod/v4";
2025-08-04 17:45:44 +02:00
import {
2025-08-28 18:27:07 +02:00
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
2025-08-04 17:45:44 +02:00
} 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";
2025-08-28 18:27:07 +02:00
import { useZodForm } from "~/lib/zodForm";
import { useTranslation } from "~/providers/I18nProvider";
import { api } from "~/utils/api";
2025-08-04 17:45:44 +02:00
export const ChangePasswordForm = () => {
2025-08-28 18:27:07 +02:00
const {
isVisible,
getStrengthColor,
passwordRefine,
getStrengthTxt,
strengthScore,
toggleVisibility,
} = useStrongPassword();
const router = useRouter();
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
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"),
2025-08-28 18:27:07 +02:00
})
.superRefine(({ oldPsw, newPsw, confirmPsw }, refinementContext) => {
2025-08-28 18:27:07 +02:00
passwordRefine({
password: newPsw,
path: "newPsw",
2025-08-29 16:18:32 +02:00
refinementContext,
2025-08-28 18:27:07 +02:00
});
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"],
});
}
2025-08-28 18:27:07 +02:00
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
type ChangePasswordFormValues = z.infer<typeof ChangePasswordFormSchema>;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const { locale, t } = useTranslation();
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
z.config(z.locales[locale]());
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const form = useZodForm(ChangePasswordFormSchema, {
defaultValues: {
newPsw: "",
oldPsw: "",
confirmPsw: "",
2025-08-28 18:27:07 +02:00
},
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const { mutate } = api.auth.ChangePassword.useMutation({
2025-08-29 16:18:32 +02:00
onError: (error) => {
toast.error(error.message);
},
onSuccess: async () => {
2025-08-28 18:27:07 +02:00
toast.success(t.pwReset.pwAggiornata);
form.reset();
await router.push("/area-riservata/dashboard");
2025-08-28 18:27:07 +02:00
},
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
function onSubmit(fields: ChangePasswordFormValues) {
mutate({
newpassword: fields.newPsw,
oldpassword: fields.oldPsw,
2025-08-28 18:27:07 +02:00
});
}
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return (
<Form {...form}>
2025-08-29 16:18:32 +02:00
<form className="space-y-8 px-0.5" onSubmit={form.handleSubmit(onSubmit)}>
2025-08-28 18:27:07 +02:00
<FormField
control={form.control}
name="oldPsw"
2025-08-28 18:27:07 +02:00
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="oldpw">{t.pwReset.oldPw}</FormLabel>
<FormMessage />
</div>
<FormControl>
<Input
placeholder=""
{...field}
autoComplete="current-password"
2025-08-29 16:18:32 +02:00
id="oldpw"
2025-08-28 18:27:07 +02:00
/>
</FormControl>
</FormItem>
)}
/>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<FormField
control={form.control}
name="newPsw"
2025-08-28 18:27:07 +02:00
render={({ field }) => (
<FormItem>
<div className="flex flex-col gap-x-2">
<FormLabel className="text-neutral-600" htmlFor="newPsw">
2025-08-28 18:27:07 +02:00
{t.pwReset.newPw}
</FormLabel>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<FormMessage
className={cn(
"whitespace-pre-wrap",
getStrengthTxt(strengthScore),
)}
/>
</div>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<FormControl>
<div className="relative">
<Input
{...field}
2025-08-29 16:18:32 +02:00
aria-describedby="password-strength"
aria-invalid={form.getFieldState("newPsw").invalid}
2025-08-28 18:27:07 +02:00
autoComplete="new-password"
className="pe-9"
id="newPsw"
2025-08-28 18:27:07 +02:00
onChange={async (e) => {
field.onChange(e);
await form.trigger("newPsw");
2025-08-28 18:27:07 +02:00
}}
2025-08-29 16:18:32 +02:00
placeholder="Password"
type={isVisible ? "text" : "password"}
2025-08-28 18:27:07 +02:00
/>
<button
2025-08-29 16:18:32 +02:00
aria-controls="password"
2025-08-28 18:27:07 +02:00
aria-label={isVisible ? "Hide password" : "Show password"}
aria-pressed={isVisible}
2025-10-10 16:18:43 +02:00
className="absolute end-0 top-1 flex size-9 items-center justify-center rounded-e-lg text-muted-foreground/80 transition-shadow hover:text-foreground focus-visible:border focus-visible:border-ring focus-visible:text-foreground focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring/30 focus-visible:ring-offset-2 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50"
2025-08-29 16:18:32 +02:00
onClick={toggleVisibility}
type="button"
2025-08-28 18:27:07 +02:00
>
{isVisible ? (
2025-08-29 16:18:32 +02:00
<EyeOff aria-hidden="true" size={16} strokeWidth={2} />
2025-08-28 18:27:07 +02:00
) : (
2025-08-29 16:18:32 +02:00
<Eye aria-hidden="true" size={16} strokeWidth={2} />
2025-08-28 18:27:07 +02:00
)}
</button>
<div
2025-08-29 16:18:32 +02:00
aria-label="Password strength"
aria-valuemax={4}
aria-valuemin={0}
aria-valuenow={strengthScore}
2025-10-10 16:18:43 +02:00
className="mt-3 mb-4 h-1 w-full overflow-hidden rounded-full bg-border"
2025-08-28 18:27:07 +02:00
role="progressbar"
>
<div
className={cn(
"h-full transition-all duration-500 ease-out",
getStrengthColor(strengthScore),
)}
style={{ width: `${(strengthScore / 4) * 100}%` }}
/>
</div>
</div>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name="confirmPsw"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="confirmpw">{t.pwReset.confirmPw}</FormLabel>
<FormMessage />
</div>
<FormControl>
<Input
placeholder=""
{...field}
autoComplete="off"
id="confirmpw"
type="password"
/>
</FormControl>
</FormItem>
)}
/>
2025-08-28 18:27:07 +02:00
<Button type="submit">{t.pwReset.aggiorna}</Button>
</form>
</Form>
);
2025-08-04 17:45:44 +02:00
};