2022-07-14 00:10:45 +00:00
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
2022-07-28 19:58:26 +00:00
|
|
|
import {
|
|
|
|
TimeUnit,
|
|
|
|
WorkflowActions,
|
|
|
|
WorkflowStep,
|
|
|
|
WorkflowTemplates,
|
|
|
|
WorkflowTriggerEvents,
|
|
|
|
} from "@prisma/client";
|
2022-07-14 00:10:45 +00:00
|
|
|
import { isValidPhoneNumber } from "libphonenumber-js";
|
|
|
|
import { useSession } from "next-auth/react";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-07-28 19:58:26 +00:00
|
|
|
import { stringOrNumber } from "@calcom/prisma/zod-utils";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-07-28 19:58:26 +00:00
|
|
|
import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery";
|
2022-07-14 00:10:45 +00:00
|
|
|
import { Alert } from "@calcom/ui/Alert";
|
2022-07-27 02:24:00 +00:00
|
|
|
import { Icon } from "@calcom/ui/Icon";
|
2022-07-14 00:10:45 +00:00
|
|
|
import Loader from "@calcom/ui/Loader";
|
2022-07-28 19:58:26 +00:00
|
|
|
import Shell from "@calcom/ui/Shell";
|
|
|
|
import { Option } from "@calcom/ui/form/MultiSelectCheckboxes";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2022-07-28 19:58:26 +00:00
|
|
|
import LicenseRequired from "../../common/components/LicenseRequired";
|
|
|
|
import WorkflowDetailsPage from "../components/WorkflowDetailsPage";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
export type FormValues = {
|
|
|
|
name: string;
|
|
|
|
activeOn: Option[];
|
|
|
|
steps: WorkflowStep[];
|
|
|
|
trigger: WorkflowTriggerEvents;
|
|
|
|
time?: number;
|
|
|
|
timeUnit?: TimeUnit;
|
|
|
|
};
|
|
|
|
|
2022-07-20 19:48:40 +00:00
|
|
|
const formSchema = z.object({
|
|
|
|
name: z.string(),
|
|
|
|
activeOn: z.object({ value: z.string(), label: z.string() }).array(),
|
2022-07-28 19:58:26 +00:00
|
|
|
trigger: z.nativeEnum(WorkflowTriggerEvents),
|
2022-07-20 19:48:40 +00:00
|
|
|
time: z.number().gte(0).optional(),
|
2022-07-28 19:58:26 +00:00
|
|
|
timeUnit: z.nativeEnum(TimeUnit).optional(),
|
2022-07-20 19:48:40 +00:00
|
|
|
steps: z
|
|
|
|
.object({
|
|
|
|
id: z.number(),
|
|
|
|
stepNumber: z.number(),
|
2022-07-28 19:58:26 +00:00
|
|
|
action: z.nativeEnum(WorkflowActions),
|
2022-07-20 19:48:40 +00:00
|
|
|
workflowId: z.number(),
|
2022-07-28 19:58:26 +00:00
|
|
|
reminderBody: z.string().nullable(),
|
|
|
|
emailSubject: z.string().nullable(),
|
|
|
|
template: z.nativeEnum(WorkflowTemplates),
|
2022-07-20 19:48:40 +00:00
|
|
|
sendTo: z
|
|
|
|
.string()
|
|
|
|
.refine((val) => isValidPhoneNumber(val))
|
|
|
|
.nullable(),
|
|
|
|
})
|
|
|
|
.array(),
|
|
|
|
});
|
|
|
|
|
2022-07-28 19:58:26 +00:00
|
|
|
const querySchema = z.object({
|
|
|
|
workflow: stringOrNumber,
|
|
|
|
});
|
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
function WorkflowPage() {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const session = useSession();
|
|
|
|
const router = useRouter();
|
|
|
|
const me = useMeQuery();
|
|
|
|
const isFreeUser = me.data?.plan === "FREE";
|
|
|
|
|
|
|
|
const [editIcon, setEditIcon] = useState(true);
|
|
|
|
const [selectedEventTypes, setSelectedEventTypes] = useState<Option[]>([]);
|
|
|
|
const [isAllDataLoaded, setIsAllDataLoaded] = useState(false);
|
|
|
|
|
|
|
|
const form = useForm<FormValues>({
|
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
});
|
2022-07-28 19:58:26 +00:00
|
|
|
const { workflow: workflowId } = router.isReady ? querySchema.parse(router.query) : { workflow: -1 };
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
data: workflow,
|
2022-07-21 00:33:35 +00:00
|
|
|
isError,
|
|
|
|
error,
|
2022-07-14 00:10:45 +00:00
|
|
|
dataUpdatedAt,
|
2022-11-10 23:40:01 +00:00
|
|
|
} = trpc.viewer.workflows.get.useQuery(
|
|
|
|
{ id: +workflowId },
|
|
|
|
{
|
|
|
|
enabled: router.isReady && !!workflowId,
|
|
|
|
}
|
|
|
|
);
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-08-26 01:04:44 +00:00
|
|
|
if (workflow && !form.getValues("name")) {
|
2022-07-14 00:10:45 +00:00
|
|
|
setSelectedEventTypes(
|
2022-07-28 19:58:26 +00:00
|
|
|
workflow.activeOn.map((active) => ({
|
|
|
|
value: String(active.eventType.id),
|
|
|
|
label: active.eventType.title,
|
|
|
|
})) || []
|
2022-07-14 00:10:45 +00:00
|
|
|
);
|
|
|
|
const activeOn = workflow.activeOn
|
2022-07-28 19:58:26 +00:00
|
|
|
? workflow.activeOn.map((active) => ({
|
|
|
|
value: active.eventType.id.toString(),
|
|
|
|
label: active.eventType.slug,
|
|
|
|
}))
|
2022-07-14 00:10:45 +00:00
|
|
|
: undefined;
|
|
|
|
form.setValue("name", workflow.name);
|
|
|
|
form.setValue("steps", workflow.steps);
|
|
|
|
form.setValue("trigger", workflow.trigger);
|
|
|
|
form.setValue("time", workflow.time || undefined);
|
|
|
|
form.setValue("timeUnit", workflow.timeUnit || undefined);
|
|
|
|
form.setValue("activeOn", activeOn || []);
|
|
|
|
setIsAllDataLoaded(true);
|
|
|
|
}
|
|
|
|
}, [dataUpdatedAt]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Shell
|
|
|
|
title="Title"
|
|
|
|
heading={
|
2022-07-20 19:48:40 +00:00
|
|
|
session.data?.hasValidLicense &&
|
|
|
|
isAllDataLoaded && (
|
2022-07-14 00:10:45 +00:00
|
|
|
<div className="group relative cursor-pointer" onClick={() => setEditIcon(false)}>
|
|
|
|
{editIcon ? (
|
|
|
|
<>
|
|
|
|
<h1
|
|
|
|
style={{ fontSize: 22, letterSpacing: "-0.0009em" }}
|
|
|
|
className="inline pl-0 text-gray-900 focus:text-black group-hover:text-gray-500">
|
|
|
|
{form.getValues("name") && form.getValues("name") !== ""
|
|
|
|
? form.getValues("name")
|
|
|
|
: workflow?.name}
|
|
|
|
</h1>
|
2022-08-03 16:01:29 +00:00
|
|
|
<Icon.FiEdit2 className="ml-1 -mt-1 inline h-4 w-4 text-gray-700 group-hover:text-gray-500" />
|
2022-07-14 00:10:45 +00:00
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<div style={{ marginBottom: -11 }}>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
autoFocus
|
|
|
|
style={{ top: -6, fontSize: 22 }}
|
|
|
|
required
|
|
|
|
className="relative h-10 w-full cursor-pointer border-none bg-transparent pl-0 text-gray-900 hover:text-gray-700 focus:text-black focus:outline-none focus:ring-0"
|
|
|
|
placeholder={t("Custom workflow")}
|
|
|
|
{...form.register("name")}
|
|
|
|
defaultValue={workflow?.name}
|
|
|
|
onBlur={() => {
|
|
|
|
setEditIcon(true);
|
|
|
|
form.getValues("name") === "" && form.setValue("name", workflow?.name || "");
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}>
|
|
|
|
<LicenseRequired>
|
|
|
|
{isFreeUser ? (
|
2022-07-15 16:55:59 +00:00
|
|
|
<Alert className="border " severity="warning" title={t("pro_feature_workflows")} />
|
2022-07-14 00:10:45 +00:00
|
|
|
) : (
|
|
|
|
<>
|
2022-07-21 00:33:35 +00:00
|
|
|
{!isError ? (
|
|
|
|
<>
|
|
|
|
{isAllDataLoaded ? (
|
|
|
|
<WorkflowDetailsPage
|
|
|
|
form={form}
|
|
|
|
workflowId={+workflowId}
|
|
|
|
selectedEventTypes={selectedEventTypes}
|
|
|
|
setSelectedEventTypes={setSelectedEventTypes}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<Loader />
|
|
|
|
)}
|
|
|
|
</>
|
2022-07-14 00:10:45 +00:00
|
|
|
) : (
|
2022-07-21 00:33:35 +00:00
|
|
|
<Alert severity="error" title="Something went wrong" message={error.message} />
|
2022-07-14 00:10:45 +00:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</LicenseRequired>
|
|
|
|
</Shell>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default WorkflowPage;
|