import React, { useState } from "react"; import { useForm } from "react-hook-form"; // ui import { CheckCircleIcon } from "@heroicons/react/20/solid"; import { Button, Input } from "components/ui"; // services import authenticationService from "services/authentication.service"; import useToast from "hooks/use-toast"; // icons // types type EmailCodeFormValues = { email: string; key?: string; token?: string; }; export const EmailCodeForm = ({ onSuccess }: any) => { const [codeSent, setCodeSent] = useState(false); const { setToastAlert } = useToast(); const { register, handleSubmit, setError, setValue, formState: { errors, isSubmitting, isValid, isDirty }, } = useForm({ defaultValues: { email: "", key: "", token: "", }, mode: "onChange", reValidateMode: "onChange", }); const onSubmit = async ({ email }: EmailCodeFormValues) => { await authenticationService .emailCode({ email }) .then((res) => { setValue("key", res.key); setCodeSent(true); }) .catch((err) => { console.log(err); }); }; const handleSignin = async (formData: EmailCodeFormValues) => { await authenticationService .magicSignIn(formData) .then((response) => { onSuccess(response); }) .catch((error) => { console.log(error); setToastAlert({ title: "Oops!", type: "error", message: "Enter the correct code to sign in", }); setError("token" as keyof EmailCodeFormValues, { type: "manual", message: error.error, }); }); }; return ( <>
{codeSent && (

Please check your mail for code.

)}
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test( value ) || "Email ID is not valid", }} error={errors.email} placeholder="Enter your Email ID" />
{codeSent && (
{/* { console.log("Triggered"); handleSubmit(onSubmit); }} > Resend code */}
)}
{codeSent ? ( ) : ( )}
); };