import React, { useEffect, useState } from "react"; import { CircleCheck, XCircle } from "lucide-react"; import { IEmailCheckData } from "@plane/types"; import { Button, Input, Spinner, TOAST_TYPE, setToast } from "@plane/ui"; // helpers import { EAuthModes } from "@/helpers/authentication.helper"; import { API_BASE_URL } from "@/helpers/common.helper"; // hooks import useTimer from "@/hooks/use-timer"; // services import { AuthService } from "@/services/auth.service"; type Props = { email: string; handleEmailClear: () => void; submitButtonText: string; mode: EAuthModes; }; type TUniqueCodeFormValues = { email: string; code: string; }; const defaultValues: TUniqueCodeFormValues = { email: "", code: "", }; // services const authService = new AuthService(); export const AuthUniqueCodeForm: React.FC = (props) => { const { email, handleEmailClear, submitButtonText, mode } = props; // states const [uniqueCodeFormData, setUniqueCodeFormData] = useState({ ...defaultValues, email }); const [isRequestingNewCode, setIsRequestingNewCode] = useState(false); const [csrfToken, setCsrfToken] = useState(undefined); const [isSubmitting, setIsSubmitting] = useState(false); // store hooks // const { captureEvent } = useEventTracker(); // timer const { timer: resendTimerCode, setTimer: setResendCodeTimer } = useTimer(30); const handleFormChange = (key: keyof TUniqueCodeFormValues, value: string) => setUniqueCodeFormData((prev) => ({ ...prev, [key]: value })); const handleSendNewCode = async (email: string) => { const payload: IEmailCheckData = { email, }; await authService .generateUniqueCode(payload) .then(() => { setResendCodeTimer(30); setToast({ type: TOAST_TYPE.SUCCESS, title: "Success!", message: "A new unique code has been sent to your email.", }); handleFormChange("code", ""); }) .catch((err) => setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: err?.error ?? "Something went wrong while generating unique code. Please try again.", }) ); }; const handleRequestNewCode = async (email: string) => { setIsRequestingNewCode(true); await handleSendNewCode(email) .then(() => setResendCodeTimer(30)) .finally(() => setIsRequestingNewCode(false)); }; useEffect(() => { if (csrfToken === undefined) authService.requestCSRFToken().then((data) => data?.csrf_token && setCsrfToken(data.csrf_token)); }, [csrfToken]); useEffect(() => { handleRequestNewCode(email); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const isRequestNewCodeDisabled = isRequestingNewCode || resendTimerCode > 0; const isButtonDisabled = isRequestingNewCode || !uniqueCodeFormData.code || isSubmitting; return (
setIsSubmitting(true)} onError={() => setIsSubmitting(false)} >
handleFormChange("email", e.target.value)} // FIXME: // hasError={Boolean(errors.email)} placeholder="name@company.com" className="h-[46px] w-full border border-onboarding-border-100 pr-12 placeholder:text-onboarding-text-400" /> {uniqueCodeFormData.email.length > 0 && ( )}
handleFormChange("code", e.target.value)} // FIXME: // hasError={Boolean(errors.code)} placeholder="gets-sets-flys" className="h-[46px] w-full border border-onboarding-border-100 !bg-onboarding-background-200 pr-12 placeholder:text-onboarding-text-400" autoFocus /> {/* )} /> */}

Paste the code sent to your email

); };