import React, { useState } from "react"; // react hook form import { useForm } from "react-hook-form"; // services import authenticationService from "services/authentication.service"; // hooks import useToast from "hooks/use-toast"; // components import { EmailResetPasswordForm } from "components/account"; // ui import { Input, SecondaryButton } from "components/ui"; // types type EmailPasswordFormValues = { email: string; password?: string; medium?: string; }; export const EmailPasswordForm = ({ handleSignIn }: any) => { const [isResettingPassword, setIsResettingPassword] = useState(false); const { setToastAlert } = useToast(); const { register, handleSubmit, setError, formState: { errors, isSubmitting, isValid, isDirty }, } = useForm({ defaultValues: { email: "", password: "", medium: "email", }, mode: "onChange", reValidateMode: "onChange", }); const onSubmit = (formData: EmailPasswordFormValues) => { authenticationService .emailLogin(formData) .then((response) => { if (handleSignIn) handleSignIn(response); }) .catch((error) => { console.log(error); setToastAlert({ title: "Oops!", type: "error", message: "Enter the correct email address and password to sign in", }); if (!error?.response?.data) return; Object.keys(error.response.data).forEach((key) => { const err = error.response.data[key]; console.log(err); setError(key as keyof EmailPasswordFormValues, { type: "manual", message: Array.isArray(err) ? err.join(", ") : err, }); }); }); }; return ( <> {isResettingPassword ? ( ) : (
/^(([^<>()[\]\\.,;:\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" />
{isSubmitting ? "Signing in..." : "Sign In"}
)} ); };