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