import type { App_RoutingForms_Form } from "@prisma/client"; import { usePathname, useRouter, useSearchParams } from "next/navigation"; import { createContext, forwardRef, useContext, useState } from "react"; import { Controller, useForm } from "react-hook-form"; import { v4 as uuidv4 } from "uuid"; import { z } from "zod"; import { useOrgBranding } from "@calcom/features/ee/organizations/context/provider"; import { RoutingFormEmbedButton, RoutingFormEmbedDialog } from "@calcom/features/embed/RoutingFormEmbed"; import { classNames } from "@calcom/lib"; import { CAL_URL } from "@calcom/lib/constants"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { useRouterQuery } from "@calcom/lib/hooks/useRouterQuery"; import { trpc } from "@calcom/trpc/react"; import type { ButtonProps } from "@calcom/ui"; import { Button, ConfirmationDialogContent, Dialog, DialogClose, DialogContent, DialogFooter, Dropdown, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, Form, SettingsToggle, showToast, Switch, TextAreaField, TextField, } from "@calcom/ui"; import { MoreHorizontal } from "@calcom/ui/components/icon"; import getFieldIdentifier from "../lib/getFieldIdentifier"; import type { SerializableForm } from "../types/types"; type RoutingForm = SerializableForm; const newFormModalQuerySchema = z.object({ action: z.literal("new").or(z.literal("duplicate")), target: z.string().optional(), }); export const useOpenModal = () => { const router = useRouter(); const pathname = usePathname(); const searchParams = useSearchParams(); const openModal = (option: z.infer) => { const newQuery = new URLSearchParams(searchParams ?? undefined); newQuery.set("dialog", "new-form"); Object.keys(option).forEach((key) => { newQuery.set(key, option[key as keyof typeof option] || ""); }); router.push(`${pathname}?${newQuery.toString()}`); }; return openModal; }; function NewFormDialog({ appUrl }: { appUrl: string }) { const routerQuery = useRouterQuery(); const { t } = useLocale(); const router = useRouter(); const utils = trpc.useContext(); const mutation = trpc.viewer.appRoutingForms.formMutation.useMutation({ onSuccess: (_data, variables) => { router.push(`${appUrl}/form-edit/${variables.id}`); }, onError: (err) => { showToast(err.message || t("something_went_wrong"), "error"); }, onSettled: () => { utils.viewer.appRoutingForms.forms.invalidate(); }, }); const hookForm = useForm<{ name: string; description: string; shouldConnect: boolean; }>(); const { action, target } = routerQuery as z.infer; const formToDuplicate = action === "duplicate" ? target : null; const teamId = action === "new" ? Number(target) : null; const { register } = hookForm; return (

{t("form_description")}

{ const formId = uuidv4(); mutation.mutate({ id: formId, ...values, addFallback: true, teamId, duplicateFrom: formToDuplicate, }); }}>
{action === "duplicate" && ( { return ( { onChange(checked); }} /> ); }} /> )}
); } const dropdownCtx = createContext<{ dropdown: boolean }>({ dropdown: false }); export const FormActionsDropdown = ({ children, disabled, }: { disabled?: boolean; children: React.ReactNode; }) => { return (