Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 57 additions & 10 deletions apps/app/components/account/email-code-form.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { useState } from "react";
import React, { useEffect, 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";
import useTimer from "hooks/use-timer";
// icons

// types
Expand All @@ -17,12 +18,19 @@ type EmailCodeFormValues = {

export const EmailCodeForm = ({ onSuccess }: any) => {
const [codeSent, setCodeSent] = useState(false);
const [codeResent, setCodeResent] = useState(false);
const [isCodeResending, setIsCodeResending] = useState(false);
const [errorResendingCode, setErrorResendingCode] = useState(false);

const { setToastAlert } = useToast();
const { timer: resendCodeTimer, setTimer: setResendCodeTimer } = useTimer();

const {
register,
handleSubmit,
setError,
setValue,
getValues,
formState: { errors, isSubmitting, isValid, isDirty },
} = useForm<EmailCodeFormValues>({
defaultValues: {
Expand All @@ -34,15 +42,24 @@ export const EmailCodeForm = ({ onSuccess }: any) => {
reValidateMode: "onChange",
});

const isResendDisabled =
resendCodeTimer > 0 || isCodeResending || isSubmitting || errorResendingCode;

const onSubmit = async ({ email }: EmailCodeFormValues) => {
setErrorResendingCode(false);
await authenticationService
.emailCode({ email })
.then((res) => {
setValue("key", res.key);
setCodeSent(true);
})
.catch((err) => {
console.log(err);
setErrorResendingCode(true);
setToastAlert({
title: "Oops!",
type: "error",
message: err?.error,
});
});
};

Expand All @@ -66,18 +83,26 @@ export const EmailCodeForm = ({ onSuccess }: any) => {
});
};

const emailOld = getValues("email");

useEffect(() => {
Comment thread
sriramveeraghanta marked this conversation as resolved.
setErrorResendingCode(false);
}, [emailOld]);

return (
<>
<form className="mt-5 space-y-5">
{codeSent && (
{(codeSent || codeResent) && (
<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">
Please check your mail for code.
{codeResent
? "Please check your mail for new code."
: "Please check your mail for code."}
</p>
</div>
</div>
Expand Down Expand Up @@ -114,15 +139,33 @@ export const EmailCodeForm = ({ onSuccess }: any) => {
error={errors.token}
placeholder="Enter code"
/>
{/* <button
<button
type="button"
className="text-xs outline-none hover:text-theme cursor-pointer"
className={`text-xs mt-5 w-full flex justify-end outline-none hover:text-theme cursor-pointer ${
resendCodeTimer > 0 ? "text-gray-400" : "text-theme"
} `}
onClick={() => {
handleSubmit(onSubmit);
setIsCodeResending(true);
onSubmit({ email: getValues("email") }).then(() => {
setCodeResent(true);
setIsCodeResending(false);
setResendCodeTimer(30);
});
}}
disabled={isResendDisabled}
>
Resend code
</button> */}
{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>
</div>
)}
<div>
Expand All @@ -139,7 +182,11 @@ export const EmailCodeForm = ({ onSuccess }: any) => {
<Button
type="submit"
className="w-full text-center"
onClick={handleSubmit(onSubmit)}
onClick={() => {
handleSubmit(onSubmit)().then(() => {
setResendCodeTimer(30);
});
}}
disabled={isSubmitting || (!isValid && isDirty)}
>
{isSubmitting ? "Sending code..." : "Send code"}
Expand Down
19 changes: 19 additions & 0 deletions apps/app/hooks/use-timer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useState, useEffect } from "react";

const TIMER = 30;

const useTimer = (initialValue: number = TIMER) => {
const [timer, setTimer] = useState(initialValue);

useEffect(() => {
const interval = setInterval(() => {
setTimer((prev) => prev - 1);
}, 1000);

return () => clearInterval(interval);
}, []);

return { timer, setTimer };
};

export default useTimer;