2022-07-14 00:10:45 +00:00
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
import { isValidPhoneNumber } from "libphonenumber-js";
|
2023-03-12 23:31:55 +00:00
|
|
|
import type { Dispatch, SetStateAction } from "react";
|
|
|
|
import { useState } from "react";
|
2022-07-14 00:10:45 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
2023-07-11 15:48:44 +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";
|
2023-05-02 11:44:05 +00:00
|
|
|
import { WorkflowActions } from "@calcom/prisma/enums";
|
2022-12-15 00:11:57 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-11-23 02:55:25 +00:00
|
|
|
import {
|
|
|
|
Button,
|
2023-07-19 19:45:13 +00:00
|
|
|
CheckboxField,
|
2022-11-23 02:55:25 +00:00
|
|
|
Dialog,
|
|
|
|
DialogClose,
|
|
|
|
DialogContent,
|
|
|
|
DialogFooter,
|
|
|
|
EmailField,
|
|
|
|
Form,
|
2023-01-18 14:32:39 +00:00
|
|
|
Input,
|
2022-11-23 02:55:25 +00:00
|
|
|
Label,
|
|
|
|
PhoneInput,
|
|
|
|
Select,
|
2023-06-11 03:20:13 +00:00
|
|
|
Tooltip,
|
2022-11-23 02:55:25 +00:00
|
|
|
} from "@calcom/ui";
|
2023-06-11 03:20:13 +00:00
|
|
|
import { Info } from "@calcom/ui/components/icon";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
2022-07-28 19:58:26 +00:00
|
|
|
import { WORKFLOW_ACTIONS } from "../lib/constants";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { onlyLettersNumbersSpaces } from "../pages/workflow";
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
interface IAddActionDialog {
|
|
|
|
isOpenDialog: boolean;
|
|
|
|
setIsOpenDialog: Dispatch<SetStateAction<boolean>>;
|
2023-01-18 14:32:39 +00:00
|
|
|
addAction: (
|
|
|
|
action: WorkflowActions,
|
|
|
|
sendTo?: string,
|
|
|
|
numberRequired?: boolean,
|
|
|
|
senderId?: string,
|
|
|
|
senderName?: string
|
|
|
|
) => void;
|
2022-11-23 02:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface ISelectActionOption {
|
|
|
|
label: string;
|
|
|
|
value: WorkflowActions;
|
2022-07-14 00:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AddActionFormValues = {
|
|
|
|
action: WorkflowActions;
|
|
|
|
sendTo?: string;
|
2022-11-23 02:55:25 +00:00
|
|
|
numberRequired?: boolean;
|
2023-01-18 14:32:39 +00:00
|
|
|
senderId?: string;
|
|
|
|
senderName?: string;
|
2022-11-23 02:55:25 +00:00
|
|
|
};
|
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
export const AddActionDialog = (props: IAddActionDialog) => {
|
|
|
|
const { t } = useLocale();
|
2022-11-23 22:42:42 +00:00
|
|
|
const { isOpenDialog, setIsOpenDialog, addAction } = props;
|
2022-07-14 00:10:45 +00:00
|
|
|
const [isPhoneNumberNeeded, setIsPhoneNumberNeeded] = useState(false);
|
2022-11-23 02:55:25 +00:00
|
|
|
const [isSenderIdNeeded, setIsSenderIdNeeded] = useState(false);
|
|
|
|
const [isEmailAddressNeeded, setIsEmailAddressNeeded] = useState(false);
|
2022-12-15 00:11:57 +00:00
|
|
|
const { data: actionOptions } = trpc.viewer.workflows.getWorkflowActionOptions.useQuery();
|
2022-07-14 00:10:45 +00:00
|
|
|
|
|
|
|
const formSchema = z.object({
|
|
|
|
action: z.enum(WORKFLOW_ACTIONS),
|
|
|
|
sendTo: z
|
|
|
|
.string()
|
2022-11-23 02:55:25 +00:00
|
|
|
.refine((val) => isValidPhoneNumber(val) || val.includes("@"))
|
2022-07-14 00:10:45 +00:00
|
|
|
.optional(),
|
2022-11-23 02:55:25 +00:00
|
|
|
numberRequired: z.boolean().optional(),
|
2023-01-18 14:32:39 +00:00
|
|
|
senderId: z
|
2022-11-23 02:55:25 +00:00
|
|
|
.string()
|
|
|
|
.refine((val) => onlyLettersNumbersSpaces(val))
|
|
|
|
.nullable(),
|
2023-01-18 14:32:39 +00:00
|
|
|
senderName: z.string().nullable(),
|
2022-07-14 00:10:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const form = useForm<AddActionFormValues>({
|
|
|
|
mode: "onSubmit",
|
|
|
|
defaultValues: {
|
|
|
|
action: WorkflowActions.EMAIL_HOST,
|
2023-01-18 14:32:39 +00:00
|
|
|
senderId: SENDER_ID,
|
|
|
|
senderName: SENDER_NAME,
|
2022-07-14 00:10:45 +00:00
|
|
|
},
|
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
});
|
|
|
|
|
2022-11-23 02:55:25 +00:00
|
|
|
const handleSelectAction = (newValue: ISelectActionOption | null) => {
|
|
|
|
if (newValue) {
|
|
|
|
form.setValue("action", newValue.value);
|
|
|
|
if (newValue.value === WorkflowActions.SMS_NUMBER) {
|
|
|
|
setIsPhoneNumberNeeded(true);
|
|
|
|
setIsSenderIdNeeded(true);
|
|
|
|
setIsEmailAddressNeeded(false);
|
2023-07-18 20:27:54 +00:00
|
|
|
form.resetField("senderId", { defaultValue: SENDER_ID });
|
2022-11-23 02:55:25 +00:00
|
|
|
} else if (newValue.value === WorkflowActions.EMAIL_ADDRESS) {
|
|
|
|
setIsEmailAddressNeeded(true);
|
|
|
|
setIsSenderIdNeeded(false);
|
|
|
|
setIsPhoneNumberNeeded(false);
|
|
|
|
} else if (newValue.value === WorkflowActions.SMS_ATTENDEE) {
|
|
|
|
setIsSenderIdNeeded(true);
|
|
|
|
setIsEmailAddressNeeded(false);
|
|
|
|
setIsPhoneNumberNeeded(false);
|
2023-07-18 20:27:54 +00:00
|
|
|
form.resetField("senderId", { defaultValue: SENDER_ID });
|
2023-07-11 15:48:44 +00:00
|
|
|
} else if (newValue.value === WorkflowActions.WHATSAPP_NUMBER) {
|
|
|
|
setIsSenderIdNeeded(false);
|
|
|
|
setIsPhoneNumberNeeded(true);
|
|
|
|
setIsEmailAddressNeeded(false);
|
2022-11-23 02:55:25 +00:00
|
|
|
} else {
|
|
|
|
setIsSenderIdNeeded(false);
|
|
|
|
setIsEmailAddressNeeded(false);
|
|
|
|
setIsPhoneNumberNeeded(false);
|
|
|
|
}
|
|
|
|
form.unregister("sendTo");
|
|
|
|
form.unregister("numberRequired");
|
|
|
|
form.clearErrors("action");
|
|
|
|
form.clearErrors("sendTo");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-15 00:11:57 +00:00
|
|
|
if (!actionOptions) return null;
|
|
|
|
|
2023-07-11 15:48:44 +00:00
|
|
|
const canRequirePhoneNumber = (workflowStep: string) => {
|
|
|
|
return (
|
2023-07-18 20:27:54 +00:00
|
|
|
WorkflowActions.SMS_ATTENDEE === workflowStep || WorkflowActions.WHATSAPP_ATTENDEE === workflowStep
|
|
|
|
);
|
|
|
|
};
|
2023-07-11 15:48:44 +00:00
|
|
|
|
|
|
|
const showSender = (action: string) => {
|
2023-07-18 20:27:54 +00:00
|
|
|
return (
|
|
|
|
!isSenderIdNeeded &&
|
|
|
|
!(WorkflowActions.WHATSAPP_NUMBER === action || WorkflowActions.WHATSAPP_ATTENDEE === action)
|
|
|
|
);
|
|
|
|
};
|
2023-07-11 15:48:44 +00:00
|
|
|
|
2022-07-14 00:10:45 +00:00
|
|
|
return (
|
|
|
|
<Dialog open={isOpenDialog} onOpenChange={setIsOpenDialog}>
|
2022-11-28 19:14:38 +00:00
|
|
|
<DialogContent type="creation" title={t("add_action")}>
|
2023-06-29 07:47:16 +00:00
|
|
|
<div className="-mt-3 space-x-3">
|
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
handleSubmit={(values) => {
|
|
|
|
addAction(
|
|
|
|
values.action,
|
|
|
|
values.sendTo,
|
|
|
|
values.numberRequired,
|
|
|
|
values.senderId,
|
|
|
|
values.senderName
|
|
|
|
);
|
|
|
|
form.unregister("sendTo");
|
|
|
|
form.unregister("action");
|
|
|
|
form.unregister("numberRequired");
|
|
|
|
setIsOpenDialog(false);
|
|
|
|
setIsPhoneNumberNeeded(false);
|
|
|
|
setIsEmailAddressNeeded(false);
|
|
|
|
setIsSenderIdNeeded(false);
|
|
|
|
}}>
|
|
|
|
<div className="space-y-1">
|
|
|
|
<Label htmlFor="label">{t("action")}:</Label>
|
|
|
|
<Controller
|
|
|
|
name="action"
|
|
|
|
control={form.control}
|
|
|
|
render={() => {
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
isSearchable={false}
|
|
|
|
className="text-sm"
|
|
|
|
defaultValue={actionOptions[0]}
|
|
|
|
onChange={handleSelectAction}
|
|
|
|
options={actionOptions}
|
|
|
|
isOptionDisabled={(option: {
|
|
|
|
label: string;
|
|
|
|
value: WorkflowActions;
|
|
|
|
needsUpgrade: boolean;
|
2023-08-10 18:52:36 +00:00
|
|
|
needsVerification: boolean;
|
|
|
|
}) => option.needsUpgrade || option.needsVerification}
|
2022-07-14 00:10:45 +00:00
|
|
|
/>
|
2023-06-29 07:47:16 +00:00
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{form.formState.errors.action && (
|
|
|
|
<p className="mt-1 text-sm text-red-500">{form.formState.errors.action.message}</p>
|
2022-07-14 00:10:45 +00:00
|
|
|
)}
|
2023-06-29 07:47:16 +00:00
|
|
|
</div>
|
|
|
|
{isPhoneNumberNeeded && (
|
|
|
|
<div className="mt-5 space-y-1">
|
|
|
|
<Label htmlFor="sendTo">{t("phone_number")}</Label>
|
2023-07-18 20:27:54 +00:00
|
|
|
<div className="mb-5 mt-1">
|
2022-11-23 02:55:25 +00:00
|
|
|
<Controller
|
|
|
|
control={form.control}
|
2023-06-29 07:47:16 +00:00
|
|
|
name="sendTo"
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<PhoneInput
|
|
|
|
className="rounded-md"
|
|
|
|
placeholder={t("enter_phone_number")}
|
|
|
|
id="sendTo"
|
|
|
|
required
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
2022-11-23 02:55:25 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
2023-06-29 07:47:16 +00:00
|
|
|
{form.formState.errors.sendTo && (
|
|
|
|
<p className="mt-1 text-sm text-red-500">{form.formState.errors.sendTo.message}</p>
|
|
|
|
)}
|
2022-11-23 02:55:25 +00:00
|
|
|
</div>
|
2023-06-29 07:47:16 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{isEmailAddressNeeded && (
|
|
|
|
<div className="mt-5">
|
|
|
|
<EmailField required label={t("email_address")} {...form.register("sendTo")} />
|
|
|
|
</div>
|
|
|
|
)}
|
2023-07-11 15:48:44 +00:00
|
|
|
{isSenderIdNeeded && (
|
2023-06-29 07:47:16 +00:00
|
|
|
<>
|
|
|
|
<div className="mt-5">
|
|
|
|
<div className="flex">
|
|
|
|
<Label>{t("sender_id")}</Label>
|
|
|
|
<Tooltip content={t("sender_id_info")}>
|
|
|
|
<Info className="ml-2 mr-1 mt-0.5 h-4 w-4 text-gray-500" />
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
<Input type="text" placeholder={SENDER_ID} maxLength={11} {...form.register(`senderId`)} />
|
|
|
|
</div>
|
|
|
|
{form.formState.errors && form.formState?.errors?.senderId && (
|
|
|
|
<p className="mt-1 text-xs text-red-500">{t("sender_id_error_message")}</p>
|
|
|
|
)}
|
|
|
|
</>
|
2023-07-11 15:48:44 +00:00
|
|
|
)}
|
2023-07-18 20:27:54 +00:00
|
|
|
{showSender(form.getValues("action")) && (
|
2023-06-29 07:47:16 +00:00
|
|
|
<div className="mt-5">
|
|
|
|
<Label>{t("sender_name")}</Label>
|
|
|
|
<Input type="text" placeholder={SENDER_NAME} {...form.register(`senderName`)} />
|
|
|
|
</div>
|
|
|
|
)}
|
2023-07-11 15:48:44 +00:00
|
|
|
{canRequirePhoneNumber(form.getValues("action")) && (
|
2023-06-29 07:47:16 +00:00
|
|
|
<div className="mt-5">
|
|
|
|
<Controller
|
|
|
|
name="numberRequired"
|
|
|
|
control={form.control}
|
|
|
|
render={() => (
|
2023-07-19 19:45:13 +00:00
|
|
|
<CheckboxField
|
2023-06-29 07:47:16 +00:00
|
|
|
defaultChecked={form.getValues("numberRequired") || false}
|
|
|
|
description={t("make_phone_number_required")}
|
|
|
|
onChange={(e) => form.setValue("numberRequired", e.target.checked)}
|
|
|
|
/>
|
|
|
|
)}
|
2022-11-28 19:14:38 +00:00
|
|
|
/>
|
2023-06-29 07:47:16 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<DialogFooter showDivider className="mt-12">
|
|
|
|
<DialogClose
|
|
|
|
onClick={() => {
|
|
|
|
setIsOpenDialog(false);
|
|
|
|
form.unregister("sendTo");
|
|
|
|
form.unregister("action");
|
|
|
|
form.unregister("numberRequired");
|
|
|
|
setIsPhoneNumberNeeded(false);
|
|
|
|
setIsEmailAddressNeeded(false);
|
|
|
|
setIsSenderIdNeeded(false);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Button type="submit">{t("add")}</Button>
|
|
|
|
</DialogFooter>
|
|
|
|
</Form>
|
2022-07-14 00:10:45 +00:00
|
|
|
</div>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|