"use client"; import React, { useEffect, useState } from "react"; import { useParams } from "next/navigation"; // icons import { CircleCheck, XCircle } from "lucide-react"; // ui import { Button, Input, Spinner } from "@plane/ui"; // helpers import { API_BASE_URL } from "@/helpers/common.helper"; // hooks import useTimer from "@/hooks/use-timer"; import useToast from "@/hooks/use-toast"; // services import { AuthService } from "@/services/auth.service"; // types import { IEmailCheckData } from "@/types/auth"; 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); const [isSubmitting, setIsSubmitting] = useState(false); // router const { next_path } = useParams(); // 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; 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" 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 autoComplete="off" />

Paste the code sent to your email

); };