import type { Dispatch, SetStateAction } from "react"; import { useState, useEffect } from "react"; import useDigitInput from "react-digit-input"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { trpc } from "@calcom/trpc/react"; import { Button, Dialog, DialogClose, DialogContent, DialogFooter, DialogHeader, Label, Input, } from "@calcom/ui"; import { Info } from "@calcom/ui/components/icon"; export const VerifyCodeDialog = ({ isOpenDialog, setIsOpenDialog, email, onSuccess, isUserSessionRequiredToVerify = true, }: { isOpenDialog: boolean; setIsOpenDialog: Dispatch>; email: string; onSuccess: (isVerified: boolean) => void; isUserSessionRequiredToVerify?: boolean; }) => { const { t } = useLocale(); // Not using the mutation isLoading flag because after verifying we submit the underlying org creation form const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(""); const [value, onChange] = useState(""); const digits = useDigitInput({ acceptedCharacters: /^[0-9]$/, length: 6, value, onChange, }); const verifyCodeMutationUserSessionRequired = trpc.viewer.organizations.verifyCode.useMutation({ onSuccess: (data) => { setIsLoading(false); onSuccess(data); }, onError: (err) => { setIsLoading(false); if (err.message === "invalid_code") { setError(t("code_provided_invalid")); } }, }); const verifyCodeMutationUserSessionNotRequired = trpc.viewer.auth.verifyCodeUnAuthenticated.useMutation({ onSuccess: (data) => { setIsLoading(false); onSuccess(data); }, onError: (err) => { setIsLoading(false); if (err.message === "invalid_code") { setError(t("code_provided_invalid")); } }, }); useEffect(() => onChange(""), [isOpenDialog]); const digitClassName = "h-12 w-12 !text-xl text-center"; return ( { onChange(""); setError(""); setIsOpenDialog(open); }}>
{error && (

{error}

)}
); };