import type { Dispatch, SetStateAction } from "react"; import { useCallback, useEffect, useState } from "react"; import useDigitInput from "react-digit-input"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { Button, Dialog, DialogClose, DialogContent, DialogFooter, DialogHeader, Icon, Input, Label, } from "@calcom/ui"; export const VerifyCodeDialog = ({ isOpenDialog, setIsOpenDialog, email, isUserSessionRequiredToVerify = true, verifyCodeWithSessionNotRequired, verifyCodeWithSessionRequired, resetErrors, setIsPending, isPending, error, }: { isOpenDialog: boolean; setIsOpenDialog: Dispatch>; email: string; isUserSessionRequiredToVerify?: boolean; verifyCodeWithSessionNotRequired: (code: string, email: string) => void; verifyCodeWithSessionRequired: (code: string, email: string) => void; resetErrors: () => void; isPending: boolean; setIsPending: (status: boolean) => void; error: string; }) => { const { t } = useLocale(); const [value, setValue] = useState(""); const [hasVerified, setHasVerified] = useState(false); const digits = useDigitInput({ acceptedCharacters: /^[0-9]$/, length: 6, value, onChange: useCallback((value: string) => { // whenever there's a change in the input, we reset the error value. resetErrors(); setValue(value); }, []), }); const verifyCode = useCallback(() => { resetErrors(); setIsPending(true); if (isUserSessionRequiredToVerify) { verifyCodeWithSessionRequired(value, email); } else { verifyCodeWithSessionNotRequired(value, email); } setHasVerified(true); }, [ resetErrors, setIsPending, isUserSessionRequiredToVerify, verifyCodeWithSessionRequired, value, email, verifyCodeWithSessionNotRequired, ]); useEffect(() => { // trim the input value because "react-digit-input" creates a string of the given length, // even when some digits are missing. And finally we use regex to check if the value consists // of 6 non-empty digits. if (hasVerified || error || isPending || !/^\d{6}$/.test(value.trim())) return; verifyCode(); }, [error, isPending, value, verifyCode, hasVerified]); useEffect(() => setValue(""), [isOpenDialog]); const digitClassName = "h-12 w-12 !text-xl text-center"; return ( { setValue(""); resetErrors(); }}>
{error && (

{error}

)} setIsOpenDialog(false)} />
); };