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

107 lines
2.5 KiB
TypeScript
Raw Normal View History

2025-08-28 18:27:07 +02:00
import { useRouter } from "next/router";
import { useState } from "react";
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";
2025-08-28 18:27:07 +02:00
import { Button } from "~/components/ui/button";
import { useZodForm } from "~/lib/zodForm";
2025-08-04 17:45:44 +02:00
import { useTranslation } from "~/providers/I18nProvider";
import { api } from "~/utils/api";
export const FormPswReset = () => {
2025-08-28 18:27:07 +02:00
const router = useRouter();
const LoginSchema = z.object({
email: z.email(),
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
type LoginFormValues = z.infer<typeof LoginSchema>;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const defaultValues: LoginFormValues = {
email: "",
};
const { locale, t } = useTranslation();
z.config(z.locales[locale]());
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const form = useZodForm(LoginSchema, {
defaultValues: defaultValues,
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const { mutate } = api.auth.sendResetLink.useMutation({
2025-08-29 16:18:32 +02:00
onError: () => {
toast.error(t.auth.login.fail);
},
2025-08-28 18:27:07 +02:00
onMutate: () => {
setAlreadyRequested(true);
},
onSuccess: async () => {
toast.success(
"Verifica la tua casella di posta per il link di reset password",
);
await router.push("/login");
},
});
const [altreadyRequested, setAlreadyRequested] = useState(false);
function onSubmit(fields: LoginFormValues) {
mutate({
email: fields.email,
});
form.reset();
}
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return (
<>
<div className="text-center">
<div className="my-8 space-y-5">
<h3 className="text-3xl font-bold text-gray-800 sm:text-3xl">
{t.pwReset.titolo}
</h3>
</div>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<Form {...form}>
<form
className="space-y-8 px-0.5"
2025-08-29 16:18:32 +02:00
onSubmit={form.handleSubmit(onSubmit)}
2025-08-28 18:27:07 +02:00
>
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="email">{t.pwReset.email}</FormLabel>{" "}
<FormMessage />
</div>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<FormControl>
<Input
placeholder="esempio@email.com"
{...field}
id="email"
/>
</FormControl>
</FormItem>
)}
/>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<Button
2025-08-29 16:18:32 +02:00
className="w-full rounded-lg bg-indigo-600 px-4 py-2 font-medium text-white duration-150 hover:bg-indigo-500 active:bg-indigo-600"
2025-08-28 18:27:07 +02:00
disabled={altreadyRequested}
type="submit"
>
{t.pwReset.get_link}
</Button>
</form>
</Form>
</div>
</>
);
2025-08-04 17:45:44 +02:00
};