import { zodResolver } from "@hookform/resolvers/zod"; import { WorkflowActions } from "@prisma/client"; import { isValidPhoneNumber } from "libphonenumber-js"; import { Dispatch, SetStateAction, useState } from "react"; import { Controller, useForm } from "react-hook-form"; import { z } from "zod"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import PhoneInput from "@calcom/ui/form/PhoneInputLazy"; import { Button, Dialog, DialogClose, DialogContent, DialogFooter, EmailField, Form, Label, Select, } from "@calcom/ui/v2"; import CheckboxField from "@calcom/ui/v2/core/form/Checkbox"; import { WORKFLOW_ACTIONS } from "../../lib/constants"; import { getWorkflowActionOptions } from "../../lib/getOptions"; interface IAddActionDialog { isOpenDialog: boolean; setIsOpenDialog: Dispatch>; addAction: (action: WorkflowActions, sendTo?: string, numberRequired?: boolean) => void; } type AddActionFormValues = { action: WorkflowActions; sendTo?: string; numberRequired?: boolean; }; export const AddActionDialog = (props: IAddActionDialog) => { const { t } = useLocale(); const { isOpenDialog, setIsOpenDialog, addAction } = props; const [isPhoneNumberNeeded, setIsPhoneNumberNeeded] = useState(false); const [isEmailAddressNeeded, setIsEmailAddressNeeded] = useState(false); const actionOptions = getWorkflowActionOptions(t); const formSchema = z.object({ action: z.enum(WORKFLOW_ACTIONS), sendTo: z .string() .refine((val) => isValidPhoneNumber(val) || val.includes("@")) .optional(), numberRequired: z.boolean().optional(), }); const form = useForm({ mode: "onSubmit", defaultValues: { action: WorkflowActions.EMAIL_HOST, }, resolver: zodResolver(formSchema), }); return (
{ addAction(values.action, values.sendTo, values.numberRequired); form.unregister("sendTo"); form.unregister("action"); form.unregister("numberRequired"); setIsOpenDialog(false); setIsPhoneNumberNeeded(false); setIsEmailAddressNeeded(false); }}>
{ return (