import type { Dispatch, SetStateAction } from "react"; import { useCallback, useEffect, useState } from "react"; import useDigitInput from "react-digit-input"; import { useBookerStoreContext } from "@calcom/features/bookings/Booker/BookerStoreProvider"; import { Dialog } from "@calcom/features/components/controlled-dialog"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { Button } from "@calcom/ui/components/button"; import { DialogContent, DialogFooter, DialogHeader, DialogClose } from "@calcom/ui/components/dialog"; import { Input } from "@calcom/ui/components/form"; import { Label } from "@calcom/ui/components/form"; import { InfoIcon } from "@coss/ui/icons"; 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 setVerificationCode = useBookerStoreContext((state) => state.setVerificationCode); 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); } setVerificationCode(value); setHasVerified(true); }, [ resetErrors, setIsPending, isUserSessionRequiredToVerify, verifyCodeWithSessionRequired, value, email, verifyCodeWithSessionNotRequired, setVerificationCode, ]); 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, hasVerified]); useEffect(() => setValue(""), [isOpenDialog]); const digitClassName = "h-12 w-12 text-xl! text-center"; return ( { resetErrors(); }}>
{error && (

{error}

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