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, WorkflowTriggerEvents } from "@prisma/client";
|
2022-07-14 00:10:45 +00:00
|
|
|
import { isValidPhoneNumber } from "libphonenumber-js";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { useState } from "react";
|
2022-07-28 19:58:26 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
2022-07-14 00:10:45 +00:00
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-07-28 19:58:26 +00:00
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
2022-07-14 00:10:45 +00:00
|
|
|
import showToast from "@calcom/lib/notification";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-07-14 00:10:45 +00:00
|
|
|
import { Button } from "@calcom/ui";
|
|
|
|
import { Dialog, DialogClose, DialogContent, DialogTrigger } from "@calcom/ui/Dialog";
|
2022-07-27 02:24:00 +00:00
|
|
|
import { Icon } from "@calcom/ui/Icon";
|
2022-07-28 19:58:26 +00:00
|
|
|
import PhoneInput from "@calcom/ui/form/PhoneInputLazy";
|
|
|
|
import Select from "@calcom/ui/form/Select";
|
2022-07-14 00:10:45 +00:00
|
|
|
import { Form, TextField } from "@calcom/ui/form/fields";
|
2022-07-28 19:58:26 +00:00
|
|
|
|
|
|
|
import { TIME_UNIT, WORKFLOW_ACTIONS, WORKFLOW_TRIGGER_EVENTS } from "../lib/constants";
|
2022-07-14 00:10:45 +00:00
|
|
|
import {
|
|
|
|
getWorkflowActionOptions,
|
|
|
|
getWorkflowTimeUnitOptions,
|
|
|
|
getWorkflowTriggerOptions,
|
2022-07-28 19:58:26 +00:00
|
|
|
} from "../lib/getOptions";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
type WorkflowFormValues = {
|
|
|
|
name: string;
|
|
|
|
trigger: WorkflowTriggerEvents;
|
|
|
|
action: WorkflowActions;
|
|
|
|
time?: number;
|
|
|
|
timeUnit?: TimeUnit;
|
|
|
|
sendTo?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function NewWorkflowButton() {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const router = useRouter();
|
|
|
|
const [showTimeSection, setShowTimeSection] = useState(false);
|
|
|
|
const [isPhoneNumberNeeded, setIsPhoneNumberNeeded] = useState(false);
|
|
|
|
const triggerOptions = getWorkflowTriggerOptions(t);
|
|
|
|
const actionOptions = getWorkflowActionOptions(t);
|
|
|
|
const timeUnitOptions = getWorkflowTimeUnitOptions(t);
|
|
|
|
|
|
|
|
const formSchema = z.object({
|
|
|
|
name: z.string().nonempty(),
|
|
|
|
trigger: z.enum(WORKFLOW_TRIGGER_EVENTS),
|
|
|
|
action: z.enum(WORKFLOW_ACTIONS),
|
|
|
|
time: z.number().min(1).optional(),
|
|
|
|
timeUnit: z.enum(TIME_UNIT).optional(),
|
|
|
|
sendTo: z
|
|
|
|
.string()
|
|
|
|
.refine((val) => isValidPhoneNumber(val))
|
|
|
|
.optional(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const form = useForm<WorkflowFormValues>({
|
|
|
|
defaultValues: {
|
|
|
|
timeUnit: TimeUnit.HOUR,
|
|
|
|
},
|
|
|
|
mode: "onSubmit",
|
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
});
|
|
|
|
|
|
|
|
const createMutation = trpc.useMutation("viewer.workflows.create", {
|
|
|
|
onSuccess: async ({ workflow }) => {
|
|
|
|
await router.replace("/workflows/" + workflow.id);
|
|
|
|
setIsPhoneNumberNeeded(false);
|
|
|
|
setShowTimeSection(false);
|
|
|
|
showToast(t("workflow_created_successfully", { workflowName: workflow.name }), "success");
|
|
|
|
},
|
|
|
|
onError: (err) => {
|
|
|
|
if (err instanceof HttpError) {
|
|
|
|
const message = `${err.statusCode}: ${err.message}`;
|
|
|
|
showToast(message, "error");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err.data?.code === "UNAUTHORIZED") {
|
|
|
|
const message = `${err.data.code}: You are not able to create this event`;
|
|
|
|
showToast(message, "error");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog name="new-workflow">
|
|
|
|
<DialogTrigger asChild>
|
2022-08-03 16:01:29 +00:00
|
|
|
<Button StartIcon={Icon.FiPlus}>{t("new_workflow_btn")}</Button>
|
2022-07-14 00:10:45 +00:00
|
|
|
</DialogTrigger>
|
|
|
|
<DialogContent>
|
|
|
|
<div className="mb-4">
|
|
|
|
<h3 className="text-lg font-bold leading-6 text-gray-900" id="modal-title">
|
|
|
|
{t("add_new_workflow")}
|
|
|
|
</h3>
|
|
|
|
</div>
|
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
handleSubmit={(values) => {
|
|
|
|
form.clearErrors();
|
|
|
|
createMutation.mutate(values);
|
|
|
|
}}>
|
|
|
|
<>
|
|
|
|
<div className="mt-9">
|
|
|
|
<TextField
|
|
|
|
label={t("workflow_name")}
|
|
|
|
placeholder={t("custom_workflow")}
|
|
|
|
{...form.register("name")}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="mt-5 space-y-1">
|
|
|
|
<label htmlFor="label" className="mt-5 block text-sm font-medium text-gray-700">
|
|
|
|
{t("trigger")}:
|
|
|
|
</label>
|
|
|
|
<Controller
|
|
|
|
name="trigger"
|
|
|
|
control={form.control}
|
|
|
|
render={() => {
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
isSearchable={false}
|
2022-07-27 02:24:00 +00:00
|
|
|
className="block w-full min-w-0 flex-1 rounded-sm text-sm"
|
2022-07-14 00:10:45 +00:00
|
|
|
onChange={(val) => {
|
|
|
|
if (val) {
|
|
|
|
form.setValue("trigger", val.value);
|
|
|
|
form.clearErrors("trigger");
|
|
|
|
if (val.value === WorkflowTriggerEvents.BEFORE_EVENT) {
|
|
|
|
setShowTimeSection(true);
|
|
|
|
} else {
|
|
|
|
setShowTimeSection(false);
|
|
|
|
form.unregister("time");
|
|
|
|
form.unregister("timeUnit");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
options={triggerOptions}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{form.formState.errors.trigger && (
|
|
|
|
<p className="mt-1 text-sm text-red-500">{form.formState.errors.trigger.message}</p>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
{showTimeSection && (
|
|
|
|
<div className="mt-5 mb-4 space-y-1">
|
|
|
|
<label htmlFor="label" className="mb-2 block text-sm font-medium text-gray-700">
|
|
|
|
{t("how_long_before")}
|
|
|
|
</label>
|
|
|
|
<div className="flex">
|
|
|
|
<input
|
|
|
|
type="number"
|
|
|
|
min="1"
|
|
|
|
defaultValue={24}
|
2022-07-27 02:24:00 +00:00
|
|
|
className="mr-5 block w-32 rounded-sm border-gray-300 px-3 py-2 text-sm marker:border focus:border-neutral-800 focus:outline-none focus:ring-1 focus:ring-neutral-800"
|
2022-07-14 00:10:45 +00:00
|
|
|
{...form.register("time", { valueAsNumber: true })}
|
|
|
|
/>
|
|
|
|
<div className="w-28">
|
|
|
|
<Controller
|
|
|
|
name="timeUnit"
|
|
|
|
control={form.control}
|
|
|
|
render={() => {
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
isSearchable={false}
|
2022-07-27 02:24:00 +00:00
|
|
|
className="block min-w-0 flex-1 rounded-sm text-sm"
|
2022-07-14 00:10:45 +00:00
|
|
|
onChange={(val) => {
|
|
|
|
if (val) {
|
|
|
|
form.setValue("timeUnit", val.value);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
defaultValue={timeUnitOptions[1]}
|
|
|
|
options={timeUnitOptions}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div className="mt-5 space-y-1">
|
|
|
|
<label htmlFor="label" className="block text-sm font-medium text-gray-700">
|
|
|
|
{t("action")}:
|
|
|
|
</label>
|
|
|
|
<Controller
|
|
|
|
name="action"
|
|
|
|
control={form.control}
|
|
|
|
render={() => {
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
isSearchable={false}
|
2022-07-27 02:24:00 +00:00
|
|
|
className="block w-full min-w-0 flex-1 rounded-sm text-sm"
|
2022-07-14 00:10:45 +00:00
|
|
|
onChange={(val) => {
|
|
|
|
if (val) {
|
|
|
|
form.setValue("action", val.value);
|
|
|
|
form.clearErrors("action");
|
|
|
|
if (val.value === WorkflowActions.SMS_NUMBER) {
|
|
|
|
setIsPhoneNumberNeeded(true);
|
|
|
|
} else {
|
|
|
|
setIsPhoneNumberNeeded(false);
|
|
|
|
form.unregister("sendTo");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
options={actionOptions}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{form.formState.errors.action && (
|
|
|
|
<p className="mt-1 text-sm text-red-500">{form.formState.errors.action.message}</p>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
{isPhoneNumberNeeded && (
|
|
|
|
<div className="mt-5 space-y-1">
|
|
|
|
<label htmlFor="sendTo" className="block text-sm font-medium text-gray-700 dark:text-white">
|
|
|
|
{t("phone_number")}
|
|
|
|
</label>
|
|
|
|
<div className="mt-1">
|
|
|
|
<PhoneInput<WorkflowFormValues>
|
|
|
|
control={form.control}
|
|
|
|
name="sendTo"
|
|
|
|
placeholder={t("enter_phone_number")}
|
|
|
|
id="sendTo"
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{form.formState.errors.sendTo && (
|
|
|
|
<p className="mt-1 text-sm text-red-500">{form.formState.errors.sendTo.message}</p>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
2022-08-09 23:27:49 +00:00
|
|
|
<div className=" mt-8 flex flex-row-reverse gap-x-2">
|
2022-08-03 22:22:38 +00:00
|
|
|
<Button type="submit" disabled={createMutation.isLoading}>
|
|
|
|
{t("continue")}
|
|
|
|
</Button>
|
2022-07-14 00:10:45 +00:00
|
|
|
<DialogClose asChild>
|
|
|
|
<Button
|
|
|
|
color="secondary"
|
|
|
|
onClick={() => {
|
|
|
|
setShowTimeSection(false);
|
|
|
|
setIsPhoneNumberNeeded(false);
|
|
|
|
form.clearErrors();
|
|
|
|
form.setValue("name", "");
|
|
|
|
}}>
|
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
|
|
|
</DialogClose>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
}
|