2023-02-17 11:28:00 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2022-11-19 14:21:26 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
// ui
|
|
|
|
import { CheckCircleIcon } from "@heroicons/react/20/solid";
|
2023-03-21 07:16:12 +00:00
|
|
|
import { Input, PrimaryButton, SecondaryButton } from "components/ui";
|
2023-01-26 18:12:20 +00:00
|
|
|
// services
|
|
|
|
import authenticationService from "services/authentication.service";
|
2023-01-31 12:39:11 +00:00
|
|
|
import useToast from "hooks/use-toast";
|
2023-02-17 11:28:00 +00:00
|
|
|
import useTimer from "hooks/use-timer";
|
2023-01-26 18:12:20 +00:00
|
|
|
// icons
|
2022-11-19 14:21:26 +00:00
|
|
|
|
|
|
|
// types
|
2023-01-26 18:12:20 +00:00
|
|
|
type EmailCodeFormValues = {
|
2022-11-19 14:21:26 +00:00
|
|
|
email: string;
|
|
|
|
key?: string;
|
|
|
|
token?: string;
|
|
|
|
};
|
|
|
|
|
2023-01-26 18:12:20 +00:00
|
|
|
export const EmailCodeForm = ({ onSuccess }: any) => {
|
2022-11-19 14:21:26 +00:00
|
|
|
const [codeSent, setCodeSent] = useState(false);
|
2023-02-17 11:28:00 +00:00
|
|
|
const [codeResent, setCodeResent] = useState(false);
|
|
|
|
const [isCodeResending, setIsCodeResending] = useState(false);
|
|
|
|
const [errorResendingCode, setErrorResendingCode] = useState(false);
|
|
|
|
|
2023-01-31 12:39:11 +00:00
|
|
|
const { setToastAlert } = useToast();
|
2023-02-17 11:28:00 +00:00
|
|
|
const { timer: resendCodeTimer, setTimer: setResendCodeTimer } = useTimer();
|
|
|
|
|
2022-11-19 14:21:26 +00:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
setError,
|
|
|
|
setValue,
|
2023-02-17 11:28:00 +00:00
|
|
|
getValues,
|
2023-01-26 18:12:20 +00:00
|
|
|
formState: { errors, isSubmitting, isValid, isDirty },
|
|
|
|
} = useForm<EmailCodeFormValues>({
|
2022-11-19 14:21:26 +00:00
|
|
|
defaultValues: {
|
|
|
|
email: "",
|
|
|
|
key: "",
|
|
|
|
token: "",
|
|
|
|
},
|
|
|
|
mode: "onChange",
|
|
|
|
reValidateMode: "onChange",
|
|
|
|
});
|
|
|
|
|
2023-02-17 11:28:00 +00:00
|
|
|
const isResendDisabled =
|
|
|
|
resendCodeTimer > 0 || isCodeResending || isSubmitting || errorResendingCode;
|
|
|
|
|
2023-02-13 14:08:58 +00:00
|
|
|
const onSubmit = async ({ email }: EmailCodeFormValues) => {
|
2023-02-17 11:28:00 +00:00
|
|
|
setErrorResendingCode(false);
|
2023-02-13 14:08:58 +00:00
|
|
|
await authenticationService
|
2022-11-19 14:21:26 +00:00
|
|
|
.emailCode({ email })
|
|
|
|
.then((res) => {
|
|
|
|
setValue("key", res.key);
|
|
|
|
setCodeSent(true);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2023-02-17 11:28:00 +00:00
|
|
|
setErrorResendingCode(true);
|
|
|
|
setToastAlert({
|
|
|
|
title: "Oops!",
|
|
|
|
type: "error",
|
|
|
|
message: err?.error,
|
|
|
|
});
|
2022-11-19 14:21:26 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-02-13 14:08:58 +00:00
|
|
|
const handleSignin = async (formData: EmailCodeFormValues) => {
|
|
|
|
await authenticationService
|
2022-11-19 14:21:26 +00:00
|
|
|
.magicSignIn(formData)
|
2023-01-26 18:12:20 +00:00
|
|
|
.then((response) => {
|
|
|
|
onSuccess(response);
|
2022-11-19 14:21:26 +00:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2023-01-31 12:39:11 +00:00
|
|
|
setToastAlert({
|
|
|
|
title: "Oops!",
|
|
|
|
type: "error",
|
2023-02-21 13:44:12 +00:00
|
|
|
message: error?.response?.data?.error ?? "Enter the correct code to sign in",
|
2023-01-31 12:39:11 +00:00
|
|
|
});
|
2023-01-26 18:12:20 +00:00
|
|
|
setError("token" as keyof EmailCodeFormValues, {
|
2022-11-19 14:21:26 +00:00
|
|
|
type: "manual",
|
|
|
|
message: error.error,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-02-17 11:28:00 +00:00
|
|
|
const emailOld = getValues("email");
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setErrorResendingCode(false);
|
|
|
|
}, [emailOld]);
|
|
|
|
|
2022-11-19 14:21:26 +00:00
|
|
|
return (
|
|
|
|
<>
|
2023-03-21 07:16:12 +00:00
|
|
|
<form className="space-y-5 py-5 px-5">
|
2023-02-17 11:28:00 +00:00
|
|
|
{(codeSent || codeResent) && (
|
2022-11-19 14:21:26 +00:00
|
|
|
<div className="rounded-md bg-green-50 p-4">
|
|
|
|
<div className="flex">
|
|
|
|
<div className="flex-shrink-0">
|
|
|
|
<CheckCircleIcon className="h-5 w-5 text-green-400" aria-hidden="true" />
|
|
|
|
</div>
|
|
|
|
<div className="ml-3">
|
|
|
|
<p className="text-sm font-medium text-green-800">
|
2023-02-17 11:28:00 +00:00
|
|
|
{codeResent
|
|
|
|
? "Please check your mail for new code."
|
|
|
|
: "Please check your mail for code."}
|
2022-11-19 14:21:26 +00:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<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}
|
2023-04-03 09:37:39 +00:00
|
|
|
placeholder="Enter your Email ID"
|
2022-11-19 14:21:26 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{codeSent && (
|
|
|
|
<div>
|
|
|
|
<Input
|
|
|
|
id="token"
|
|
|
|
type="token"
|
|
|
|
name="token"
|
|
|
|
register={register}
|
|
|
|
validations={{
|
|
|
|
required: "Code is required",
|
|
|
|
}}
|
|
|
|
error={errors.token}
|
|
|
|
placeholder="Enter code"
|
|
|
|
/>
|
2023-02-17 11:28:00 +00:00
|
|
|
<button
|
2023-02-14 14:35:32 +00:00
|
|
|
type="button"
|
2023-03-17 05:10:38 +00:00
|
|
|
className={`mt-5 flex w-full justify-end text-xs outline-none ${
|
|
|
|
isResendDisabled ? "cursor-default text-gray-400" : "cursor-pointer text-theme"
|
2023-02-17 11:28:00 +00:00
|
|
|
} `}
|
2023-02-13 14:08:58 +00:00
|
|
|
onClick={() => {
|
2023-02-17 11:28:00 +00:00
|
|
|
setIsCodeResending(true);
|
|
|
|
onSubmit({ email: getValues("email") }).then(() => {
|
|
|
|
setCodeResent(true);
|
|
|
|
setIsCodeResending(false);
|
|
|
|
setResendCodeTimer(30);
|
|
|
|
});
|
2023-02-13 14:08:58 +00:00
|
|
|
}}
|
2023-02-17 11:28:00 +00:00
|
|
|
disabled={isResendDisabled}
|
2023-02-13 14:08:58 +00:00
|
|
|
>
|
2023-02-17 11:28:00 +00:00
|
|
|
{resendCodeTimer > 0 ? (
|
|
|
|
<p className="text-right">
|
|
|
|
Didn{"'"}t receive code? Get new code in {resendCodeTimer} seconds.
|
|
|
|
</p>
|
|
|
|
) : isCodeResending ? (
|
|
|
|
"Sending code..."
|
|
|
|
) : errorResendingCode ? (
|
|
|
|
"Please try again later"
|
|
|
|
) : (
|
|
|
|
"Resend code"
|
|
|
|
)}
|
|
|
|
</button>
|
2022-11-19 14:21:26 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div>
|
2023-02-13 14:08:58 +00:00
|
|
|
{codeSent ? (
|
2023-03-21 07:16:12 +00:00
|
|
|
<PrimaryButton
|
2023-02-13 14:08:58 +00:00
|
|
|
type="submit"
|
|
|
|
className="w-full text-center"
|
2023-03-21 07:16:12 +00:00
|
|
|
size="md"
|
2023-02-13 14:08:58 +00:00
|
|
|
onClick={handleSubmit(handleSignin)}
|
2023-03-17 05:10:38 +00:00
|
|
|
loading={isSubmitting || (!isValid && isDirty)}
|
2023-02-13 14:08:58 +00:00
|
|
|
>
|
|
|
|
{isSubmitting ? "Signing in..." : "Sign in"}
|
2023-03-21 07:16:12 +00:00
|
|
|
</PrimaryButton>
|
2023-02-13 14:08:58 +00:00
|
|
|
) : (
|
2023-03-21 07:16:12 +00:00
|
|
|
<PrimaryButton
|
2023-02-13 14:08:58 +00:00
|
|
|
className="w-full text-center"
|
2023-03-21 07:16:12 +00:00
|
|
|
size="md"
|
2023-02-17 11:28:00 +00:00
|
|
|
onClick={() => {
|
|
|
|
handleSubmit(onSubmit)().then(() => {
|
|
|
|
setResendCodeTimer(30);
|
|
|
|
});
|
|
|
|
}}
|
2023-03-17 05:10:38 +00:00
|
|
|
loading={isSubmitting || (!isValid && isDirty)}
|
2023-02-13 14:08:58 +00:00
|
|
|
>
|
|
|
|
{isSubmitting ? "Sending code..." : "Send code"}
|
2023-03-21 07:16:12 +00:00
|
|
|
</PrimaryButton>
|
2023-02-13 14:08:58 +00:00
|
|
|
)}
|
2022-11-19 14:21:26 +00:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|