import React, { useEffect, useState } from "react"; // hooks import useTimer from "hooks/use-timer"; import useToast from "hooks/use-toast"; import { useRouter } from "next/router"; // types import { IEmailCheckData } from "types/auth"; // icons import { CircleCheck, XCircle } from "lucide-react"; // ui import { Button, Input } from "@plane/ui"; // helpers import { API_BASE_URL } from "@/helpers/common.helper"; // services import { AuthService } from "@/services/authentication.service"; import { EAuthModes } from "./root"; type Props = { email: string; mode: EAuthModes; handleEmailClear: () => void; submitButtonText: string; }; type TUniqueCodeFormValues = { email: string; code: string; }; const defaultValues: TUniqueCodeFormValues = { email: "", code: "", }; // services const authService = new AuthService(); export const UniqueCodeForm: React.FC = (props) => { const { email, mode, handleEmailClear, submitButtonText } = props; // states const [uniqueCodeFormData, setUniqueCodeFormData] = useState({ ...defaultValues, email }); const [isRequestingNewCode, setIsRequestingNewCode] = useState(false); const [csrfToken, setCsrfToken] = useState(undefined); // router const router = useRouter(); const { next_path } = router.query; // toast alert const { setToastAlert } = useToast(); // 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); setToastAlert({ type: "success", title: "Success!", message: "A new unique code has been sent to your email.", }); handleFormChange("code", ""); }) .catch((err) => setToastAlert({ type: "error", title: "Error!", message: err?.error ?? "Something went wrong. Please try again.", }) ); }; const handleRequestNewCode = async () => { setIsRequestingNewCode(true); await handleSendNewCode(uniqueCodeFormData.email) .then(() => setResendCodeTimer(30)) .finally(() => setIsRequestingNewCode(false)); }; useEffect(() => { if (csrfToken === undefined) authService.requestCSRFToken().then((data) => data?.csrf_token && setCsrfToken(data.csrf_token)); }, [csrfToken]); useEffect(() => { setIsRequestingNewCode(true); handleSendNewCode(email) .then(() => setResendCodeTimer(30)) .finally(() => setIsRequestingNewCode(false)); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const isRequestNewCodeDisabled = isRequestingNewCode || resendTimerCode > 0; return (
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" disabled /> {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

); };