2023-06-06 16:06:13 +00:00
|
|
|
import React, { useState } from "react";
|
2023-03-17 05:10:38 +00:00
|
|
|
|
2022-11-19 14:21:26 +00:00
|
|
|
// react hook form
|
|
|
|
import { useForm } from "react-hook-form";
|
2023-03-17 05:10:38 +00:00
|
|
|
// services
|
2023-01-26 18:12:20 +00:00
|
|
|
import authenticationService from "services/authentication.service";
|
2023-03-17 05:10:38 +00:00
|
|
|
// hooks
|
2023-01-31 12:39:11 +00:00
|
|
|
import useToast from "hooks/use-toast";
|
2023-06-06 16:06:13 +00:00
|
|
|
// components
|
|
|
|
import { EmailResetPasswordForm } from "components/account";
|
2023-03-17 05:10:38 +00:00
|
|
|
// ui
|
|
|
|
import { Input, SecondaryButton } from "components/ui";
|
2022-11-19 14:21:26 +00:00
|
|
|
// types
|
2023-01-26 18:12:20 +00:00
|
|
|
type EmailPasswordFormValues = {
|
2022-11-19 14:21:26 +00:00
|
|
|
email: string;
|
|
|
|
password?: string;
|
|
|
|
medium?: string;
|
|
|
|
};
|
|
|
|
|
2023-06-05 12:18:29 +00:00
|
|
|
export const EmailPasswordForm = ({ handleSignIn }: any) => {
|
2023-06-06 16:06:13 +00:00
|
|
|
const [isResettingPassword, setIsResettingPassword] = useState(false);
|
|
|
|
|
2023-01-31 12:39:11 +00:00
|
|
|
const { setToastAlert } = useToast();
|
2023-06-06 16:06:13 +00:00
|
|
|
|
2022-11-19 14:21:26 +00:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
setError,
|
2023-01-26 18:12:20 +00:00
|
|
|
formState: { errors, isSubmitting, isValid, isDirty },
|
|
|
|
} = useForm<EmailPasswordFormValues>({
|
2022-11-19 14:21:26 +00:00
|
|
|
defaultValues: {
|
|
|
|
email: "",
|
|
|
|
password: "",
|
|
|
|
medium: "email",
|
|
|
|
},
|
|
|
|
mode: "onChange",
|
|
|
|
reValidateMode: "onChange",
|
|
|
|
});
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
const onSubmit = (formData: EmailPasswordFormValues) => {
|
2022-11-19 14:21:26 +00:00
|
|
|
authenticationService
|
|
|
|
.emailLogin(formData)
|
2023-01-26 18:12:20 +00:00
|
|
|
.then((response) => {
|
2023-06-05 12:18:29 +00:00
|
|
|
if (handleSignIn) handleSignIn(response);
|
2022-11-19 14:21:26 +00:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log(error);
|
2023-01-31 12:39:11 +00:00
|
|
|
setToastAlert({
|
|
|
|
title: "Oops!",
|
|
|
|
type: "error",
|
|
|
|
message: "Enter the correct email address and password to sign in",
|
|
|
|
});
|
2022-11-19 14:21:26 +00:00
|
|
|
if (!error?.response?.data) return;
|
|
|
|
Object.keys(error.response.data).forEach((key) => {
|
|
|
|
const err = error.response.data[key];
|
2023-04-17 19:45:26 +00:00
|
|
|
console.log(err);
|
2023-01-26 18:12:20 +00:00
|
|
|
setError(key as keyof EmailPasswordFormValues, {
|
2022-11-19 14:21:26 +00:00
|
|
|
type: "manual",
|
|
|
|
message: Array.isArray(err) ? err.join(", ") : err,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2023-06-06 16:06:13 +00:00
|
|
|
|
2022-11-19 14:21:26 +00:00
|
|
|
return (
|
|
|
|
<>
|
2023-06-06 16:06:13 +00:00
|
|
|
{isResettingPassword ? (
|
|
|
|
<EmailResetPasswordForm setIsResettingPassword={setIsResettingPassword} />
|
|
|
|
) : (
|
|
|
|
<form className="mt-5 py-5 px-5" onSubmit={handleSubmit(onSubmit)}>
|
|
|
|
<div>
|
|
|
|
<Input
|
|
|
|
id="email"
|
|
|
|
type="email"
|
|
|
|
name="email"
|
|
|
|
register={register}
|
|
|
|
validations={{
|
|
|
|
required: "Email ID is required",
|
|
|
|
validate: (value) =>
|
|
|
|
/^(([^<>()[\]\\.,;:\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"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="mt-5">
|
|
|
|
<Input
|
|
|
|
id="password"
|
|
|
|
type="password"
|
|
|
|
name="password"
|
|
|
|
register={register}
|
|
|
|
validations={{
|
|
|
|
required: "Password is required",
|
|
|
|
}}
|
|
|
|
error={errors.password}
|
|
|
|
placeholder="Enter your password"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="mt-2 flex items-center justify-between">
|
|
|
|
<div className="ml-auto text-sm">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => setIsResettingPassword(true)}
|
|
|
|
className="font-medium text-brand-accent hover:text-brand-accent"
|
|
|
|
>
|
2023-04-24 05:49:43 +00:00
|
|
|
Forgot your password?
|
2023-06-06 16:06:13 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="mt-5">
|
|
|
|
<SecondaryButton
|
|
|
|
type="submit"
|
|
|
|
className="w-full text-center"
|
|
|
|
disabled={!isValid && isDirty}
|
|
|
|
loading={isSubmitting}
|
|
|
|
>
|
|
|
|
{isSubmitting ? "Signing in..." : "Sign In"}
|
|
|
|
</SecondaryButton>
|
2022-11-19 14:21:26 +00:00
|
|
|
</div>
|
2023-06-06 16:06:13 +00:00
|
|
|
</form>
|
|
|
|
)}
|
2022-11-19 14:21:26 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|