2022-11-19 14:21:26 +00:00
|
|
|
import React from "react";
|
2023-03-17 05:10:38 +00:00
|
|
|
|
2022-11-19 14:21:26 +00:00
|
|
|
import Link from "next/link";
|
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-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-01-26 18:12:20 +00:00
|
|
|
export const EmailPasswordForm = ({ onSuccess }: any) => {
|
2023-01-31 12:39:11 +00:00
|
|
|
const { setToastAlert } = useToast();
|
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) => {
|
|
|
|
onSuccess(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,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<>
|
2023-03-21 07:16:12 +00:00
|
|
|
<form className="mt-5 py-5 px-5" onSubmit={handleSubmit(onSubmit)}>
|
2022-11-19 14:21:26 +00:00
|
|
|
<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>
|
2023-01-26 18:12:20 +00:00
|
|
|
<div className="mt-2 flex items-center justify-between">
|
|
|
|
<div className="ml-auto text-sm">
|
2022-11-19 14:21:26 +00:00
|
|
|
<Link href={"/forgot-password"}>
|
2022-12-13 15:52:44 +00:00
|
|
|
<a className="font-medium text-theme hover:text-indigo-500">Forgot your password?</a>
|
2022-11-19 14:21:26 +00:00
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="mt-5">
|
2023-03-17 05:10:38 +00:00
|
|
|
<SecondaryButton
|
2022-11-19 14:21:26 +00:00
|
|
|
type="submit"
|
2023-03-17 05:10:38 +00:00
|
|
|
className="w-full text-center"
|
|
|
|
loading={isSubmitting || (!isValid && isDirty)}
|
2022-11-19 14:21:26 +00:00
|
|
|
>
|
|
|
|
{isSubmitting ? "Signing in..." : "Sign In"}
|
2023-03-17 05:10:38 +00:00
|
|
|
</SecondaryButton>
|
2022-11-19 14:21:26 +00:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|