"use client"; import React, { useEffect, useState } from "react"; import { CircleCheck, XCircle } from "lucide-react"; import { Button, Input, Spinner } from "@plane/ui"; // helpers import { API_BASE_URL } from "@/helpers/common.helper"; // hooks import useTimer from "@/hooks/use-timer"; // services import { AuthService } from "@/services/auth.service"; // types import { EAuthModes } from "@/types/auth"; // services const authService = new AuthService(); type TAuthUniqueCodeForm = { mode: EAuthModes; email: string; handleEmailClear: () => void; generateEmailUniqueCode: (email: string) => Promise<{ code: string } | undefined>; }; type TUniqueCodeFormValues = { email: string; code: string; }; const defaultValues: TUniqueCodeFormValues = { email: "", code: "", }; export const AuthUniqueCodeForm: React.FC = (props) => { const { mode, email, handleEmailClear, generateEmailUniqueCode } = props; // hooks // const { captureEvent } = useEventTracker(); // derived values const defaultResetTimerValue = 5; // states const [uniqueCodeFormData, setUniqueCodeFormData] = useState({ ...defaultValues, email }); const [isRequestingNewCode, setIsRequestingNewCode] = useState(false); const [csrfToken, setCsrfToken] = useState(undefined); const [isSubmitting, setIsSubmitting] = useState(false); // timer const { timer: resendTimerCode, setTimer: setResendCodeTimer } = useTimer(0); const handleFormChange = (key: keyof TUniqueCodeFormValues, value: string) => setUniqueCodeFormData((prev) => ({ ...prev, [key]: value })); const generateNewCode = async (email: string) => { try { setIsRequestingNewCode(true); const uniqueCode = await generateEmailUniqueCode(email); setResendCodeTimer(defaultResetTimerValue); handleFormChange("code", uniqueCode?.code || ""); setIsRequestingNewCode(false); } catch { setResendCodeTimer(0); console.error("Error while requesting new code"); setIsRequestingNewCode(false); } }; useEffect(() => { if (csrfToken === undefined) authService.requestCSRFToken().then((data) => data?.csrf_token && setCsrfToken(data.csrf_token)); }, [csrfToken]); const isRequestNewCodeDisabled = isRequestingNewCode || resendTimerCode > 0; const isButtonDisabled = isRequestingNewCode || !uniqueCodeFormData.code || isSubmitting; return (
setIsSubmitting(true)} onError={() => setIsSubmitting(false)} >
handleFormChange("email", e.target.value)} placeholder="name@company.com" className={`disable-autofill-style h-[46px] w-full placeholder:text-onboarding-text-400 border-0`} disabled /> {uniqueCodeFormData.email.length > 0 && (
)}
handleFormChange("code", e.target.value)} placeholder="gets-sets-flys" className="disable-autofill-style 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

); };