"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import { useTranslations } from "next-intl"; import Image from "next/image"; import { useEffect, useState } from "react"; import { SubmitHandler, useForm } from "react-hook-form"; import { showToast } from "../../../../utils/toast"; import { z } from "zod"; import { errorToast, FormPassword } from "../../../../components"; import { Button, CardContent, CardDescription, CardHeader, CardTitle, Form } from "../../../../shadcnui"; import { useAuthContext } from "../../contexts"; import { AuthService } from "../../data/auth.service"; import { AuthComponent } from "../../enums"; export function AcceptInvitation() { const { setComponentType, params, setParams } = useAuthContext(); const [showConfirmation, setShowConfirmation] = useState(false); const [error, setError] = useState(undefined); const t = useTranslations(); useEffect(() => { async function validateCode(code: string) { try { const payload: any = { code: code, }; await AuthService.validateCode(payload); } catch (e) { setError(e instanceof Error ? e.message : String(e)); errorToast({ title: t(`common.errors.error`), error: e }); } } if (params && params.code) { validateCode(params.code); } else { setError(t(`auth.errors.invalid_invitation_code`)); } }, []); const formSchema = z .object({ password: z.string().min(1, { message: t(`user.fields.password.error`), }), passwordRetype: z.string().min(1, { message: t("auth.errors.password_retype_required"), }), }) .refine((data) => data.password === data.passwordRetype, { message: t("auth.fields.retype_password.error_not_match"), path: ["passwordRetype"], }); const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { password: "", passwordRetype: "", }, }); const onSubmit: SubmitHandler> = async (values: z.infer) => { try { if (!params?.code) return; const payload = { code: params?.code, password: values.password, }; await AuthService.acceptInvitation(payload); setShowConfirmation(true); showToast(t("auth.account_activated"), { description: t("auth.account_activated_description"), }); setTimeout(() => { setComponentType(AuthComponent.Login); setParams(undefined); }, 2000); } catch (_e) { errorToast({ title: t(`common.errors.error`), error }); } }; return ( <> Logo {t("auth.accept_invitation")} {error ? <>{t("auth.errors.activating_account")} : <>{t("auth.select_password")}} {showConfirmation ? ( {t("auth.activation_description")} ) : error ? ( {error} ) : (
)}
); }