2023-02-27 07:24:43 +00:00
|
|
|
import type { WorkflowActions } from "@prisma/client";
|
2023-04-13 02:10:23 +00:00
|
|
|
import { WorkflowTemplates, SchedulingType } from "@prisma/client";
|
2022-07-14 00:10:45 +00:00
|
|
|
import { useRouter } from "next/router";
|
2023-02-27 07:24:43 +00:00
|
|
|
import type { Dispatch, SetStateAction } from "react";
|
|
|
|
import { useMemo, useState } from "react";
|
|
|
|
import type { UseFormReturn } from "react-hook-form";
|
|
|
|
import { Controller } from "react-hook-form";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2023-01-18 14:32:39 +00:00
|
|
|
import { SENDER_ID, SENDER_NAME } from "@calcom/lib/constants";
|
2022-07-14 00:10:45 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-11-23 02:55:25 +00:00
|
|
|
import type { MultiSelectCheckboxesOptionType as Option } from "@calcom/ui";
|
2023-01-23 23:08:01 +00:00
|
|
|
import { Button, Label, MultiSelectCheckboxes, TextField } from "@calcom/ui";
|
2023-04-12 15:26:31 +00:00
|
|
|
import { ArrowDown, Trash2 } from "@calcom/ui/components/icon";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2023-01-18 14:32:39 +00:00
|
|
|
import { isSMSAction } from "../lib/isSMSAction";
|
2022-07-28 19:58:26 +00:00
|
|
|
import type { FormValues } from "../pages/workflow";
|
|
|
|
import { AddActionDialog } from "./AddActionDialog";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { DeleteDialog } from "./DeleteDialog";
|
2022-07-28 19:58:26 +00:00
|
|
|
import WorkflowStepContainer from "./WorkflowStepContainer";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
interface Props {
|
2022-07-20 19:48:40 +00:00
|
|
|
form: UseFormReturn<FormValues>;
|
2022-07-14 00:10:45 +00:00
|
|
|
workflowId: number;
|
|
|
|
selectedEventTypes: Option[];
|
|
|
|
setSelectedEventTypes: Dispatch<SetStateAction<Option[]>>;
|
2023-02-27 07:24:43 +00:00
|
|
|
teamId?: number;
|
|
|
|
isMixedEventType: boolean;
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default function WorkflowDetailsPage(props: Props) {
|
2023-02-27 07:24:43 +00:00
|
|
|
const { form, workflowId, selectedEventTypes, setSelectedEventTypes, teamId, isMixedEventType } = props;
|
2022-07-14 00:10:45 +00:00
|
|
|
const { t } = useLocale();
|
|
|
|
const router = useRouter();
|
2022-11-23 02:55:25 +00:00
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
const [isAddActionDialogOpen, setIsAddActionDialogOpen] = useState(false);
|
|
|
|
const [reload, setReload] = useState(false);
|
2022-11-23 02:55:25 +00:00
|
|
|
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const { data, isLoading } = trpc.viewer.eventTypes.getByViewer.useQuery();
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2022-07-20 19:48:40 +00:00
|
|
|
const eventTypeOptions = useMemo(
|
|
|
|
() =>
|
2023-02-27 07:24:43 +00:00
|
|
|
data?.eventTypeGroups.reduce((options, group) => {
|
2023-03-03 16:36:23 +00:00
|
|
|
/** don't show team event types for user workflow */
|
|
|
|
if (!teamId && group.teamId) return options;
|
|
|
|
/** only show correct team event types for team workflows */
|
|
|
|
if (teamId && teamId !== group.teamId) return options;
|
2023-02-27 07:24:43 +00:00
|
|
|
return [
|
2022-07-20 19:48:40 +00:00
|
|
|
...options,
|
2023-04-13 02:10:23 +00:00
|
|
|
...group.eventTypes
|
|
|
|
.filter(
|
|
|
|
(evType) =>
|
|
|
|
!evType.metadata?.managedEventConfig && evType.schedulingType !== SchedulingType.MANAGED
|
|
|
|
)
|
|
|
|
.map((eventType) => ({
|
|
|
|
value: String(eventType.id),
|
|
|
|
label: eventType.title,
|
|
|
|
})),
|
2023-02-27 07:24:43 +00:00
|
|
|
];
|
|
|
|
}, [] as Option[]) || [],
|
2022-07-20 19:48:40 +00:00
|
|
|
[data]
|
|
|
|
);
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2023-02-27 07:24:43 +00:00
|
|
|
let allEventTypeOptions = eventTypeOptions;
|
|
|
|
const distinctEventTypes = new Set();
|
|
|
|
|
|
|
|
if (!teamId && isMixedEventType) {
|
|
|
|
allEventTypeOptions = [...eventTypeOptions, ...selectedEventTypes];
|
|
|
|
allEventTypeOptions = allEventTypeOptions.filter((option) => {
|
|
|
|
const duplicate = distinctEventTypes.has(option.value);
|
|
|
|
distinctEventTypes.add(option.value);
|
|
|
|
return !duplicate;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-18 14:32:39 +00:00
|
|
|
const addAction = (
|
|
|
|
action: WorkflowActions,
|
|
|
|
sendTo?: string,
|
|
|
|
numberRequired?: boolean,
|
|
|
|
sender?: string,
|
|
|
|
senderName?: string
|
|
|
|
) => {
|
2022-07-14 00:10:45 +00:00
|
|
|
const steps = form.getValues("steps");
|
|
|
|
const id =
|
2022-07-20 19:48:40 +00:00
|
|
|
steps?.length > 0
|
2022-07-14 00:10:45 +00:00
|
|
|
? steps.sort((a, b) => {
|
|
|
|
return a.id - b.id;
|
|
|
|
})[0].id - 1
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
const step = {
|
|
|
|
id: id > 0 ? 0 : id, //id of new steps always <= 0
|
|
|
|
action,
|
|
|
|
stepNumber:
|
|
|
|
steps && steps.length > 0
|
|
|
|
? steps.sort((a, b) => {
|
|
|
|
return a.stepNumber - b.stepNumber;
|
|
|
|
})[steps.length - 1].stepNumber + 1
|
|
|
|
: 1,
|
|
|
|
sendTo: sendTo || null,
|
|
|
|
workflowId: workflowId,
|
|
|
|
reminderBody: null,
|
|
|
|
emailSubject: null,
|
2022-11-23 02:55:25 +00:00
|
|
|
template: WorkflowTemplates.CUSTOM,
|
|
|
|
numberRequired: numberRequired || false,
|
2023-01-18 14:32:39 +00:00
|
|
|
sender: isSMSAction(action) ? sender || SENDER_ID : SENDER_ID,
|
|
|
|
senderName: !isSMSAction(action) ? senderName || SENDER_NAME : SENDER_NAME,
|
2022-12-15 21:54:40 +00:00
|
|
|
numberVerificationPending: false,
|
2022-07-14 00:10:45 +00:00
|
|
|
};
|
|
|
|
steps?.push(step);
|
|
|
|
form.setValue("steps", steps);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-11-23 02:55:25 +00:00
|
|
|
<>
|
|
|
|
<div className="my-8 sm:my-0 md:flex">
|
|
|
|
<div className="pl-2 pr-3 md:sticky md:top-6 md:h-0 md:pl-0">
|
|
|
|
<div className="mb-5">
|
|
|
|
<TextField label={`${t("workflow_name")}:`} type="text" {...form.register("name")} />
|
|
|
|
</div>
|
|
|
|
<Label>{t("which_event_type_apply")}</Label>
|
2022-07-14 00:10:45 +00:00
|
|
|
<Controller
|
|
|
|
name="activeOn"
|
|
|
|
control={form.control}
|
|
|
|
render={() => {
|
|
|
|
return (
|
|
|
|
<MultiSelectCheckboxes
|
2023-02-27 07:24:43 +00:00
|
|
|
options={allEventTypeOptions}
|
2022-07-14 00:10:45 +00:00
|
|
|
isLoading={isLoading}
|
2022-11-23 02:55:25 +00:00
|
|
|
className="w-full md:w-64"
|
2022-07-14 00:10:45 +00:00
|
|
|
setSelected={setSelectedEventTypes}
|
|
|
|
selected={selectedEventTypes}
|
|
|
|
setValue={(s: Option[]) => {
|
|
|
|
form.setValue("activeOn", s);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="md:border-subtle my-7 border-transparent md:border-t" />
|
2022-11-23 02:55:25 +00:00
|
|
|
<Button
|
|
|
|
type="button"
|
2023-04-12 15:26:31 +00:00
|
|
|
StartIcon={Trash2}
|
2022-11-23 02:55:25 +00:00
|
|
|
color="destructive"
|
|
|
|
className="border"
|
|
|
|
onClick={() => setDeleteDialogOpen(true)}>
|
|
|
|
{t("delete_workflow")}
|
|
|
|
</Button>
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="border-subtle my-7 border-t md:border-none" />
|
2022-07-14 00:10:45 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
{/* Workflow Trigger Event & Steps */}
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="bg-muted border-subtle w-full rounded-md border p-3 py-5 md:ml-3 md:p-8">
|
2022-07-14 00:10:45 +00:00
|
|
|
{form.getValues("trigger") && (
|
|
|
|
<div>
|
2023-02-27 07:24:43 +00:00
|
|
|
<WorkflowStepContainer form={form} teamId={teamId} />
|
2022-07-14 00:10:45 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{form.getValues("steps") && (
|
|
|
|
<>
|
|
|
|
{form.getValues("steps")?.map((step) => {
|
|
|
|
return (
|
|
|
|
<WorkflowStepContainer
|
|
|
|
key={step.id}
|
|
|
|
form={form}
|
|
|
|
step={step}
|
|
|
|
reload={reload}
|
|
|
|
setReload={setReload}
|
2023-02-27 07:24:43 +00:00
|
|
|
teamId={teamId}
|
2022-07-14 00:10:45 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</>
|
|
|
|
)}
|
2022-11-23 02:55:25 +00:00
|
|
|
<div className="my-3 flex justify-center">
|
2023-04-12 15:26:31 +00:00
|
|
|
<ArrowDown className="text-subtle stroke-[1.5px] text-3xl" />
|
2022-07-14 00:10:45 +00:00
|
|
|
</div>
|
|
|
|
<div className="flex justify-center">
|
2022-11-23 22:42:42 +00:00
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
onClick={() => setIsAddActionDialogOpen(true)}
|
|
|
|
color="secondary"
|
2023-04-05 18:14:46 +00:00
|
|
|
className="bg-default">
|
2022-07-14 00:10:45 +00:00
|
|
|
{t("add_action")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-11-23 02:55:25 +00:00
|
|
|
</div>
|
2022-07-14 00:10:45 +00:00
|
|
|
<AddActionDialog
|
|
|
|
isOpenDialog={isAddActionDialogOpen}
|
|
|
|
setIsOpenDialog={setIsAddActionDialogOpen}
|
|
|
|
addAction={addAction}
|
2022-11-23 02:55:25 +00:00
|
|
|
/>
|
|
|
|
<DeleteDialog
|
|
|
|
isOpenDialog={deleteDialogOpen}
|
|
|
|
setIsOpenDialog={setDeleteDialogOpen}
|
|
|
|
workflowId={workflowId}
|
|
|
|
additionalFunction={async () => await router.push("/workflows")}
|
2022-07-14 00:10:45 +00:00
|
|
|
/>
|
2022-11-23 02:55:25 +00:00
|
|
|
</>
|
2022-07-14 00:10:45 +00:00
|
|
|
);
|
|
|
|
}
|