2022-07-14 00:10:45 +00:00
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
import { WorkflowActions } from "@prisma/client";
|
|
|
|
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";
|
|
|
|
|
2022-12-12 11:00:15 +00:00
|
|
|
import { SENDER_ID } from "@calcom/lib/constants";
|
2023-01-18 14:32:39 +00:00
|
|
|
import { SENDER_NAME } from "@calcom/lib/constants";
|
2022-07-14 00:10:45 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-12-15 00:11:57 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2022-11-23 02:55:25 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Checkbox,
|
|
|
|
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,
|
|
|
|
} from "@calcom/ui";
|
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);
|
|
|
|
} 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);
|
|
|
|
} 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;
|
|
|
|
|
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")}>
|
2022-07-14 00:10:45 +00:00
|
|
|
<div className="space-x-3 ">
|
|
|
|
<div className="pt-1">
|
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
handleSubmit={(values) => {
|
2023-01-18 14:32:39 +00:00
|
|
|
addAction(
|
|
|
|
values.action,
|
|
|
|
values.sendTo,
|
|
|
|
values.numberRequired,
|
|
|
|
values.senderId,
|
|
|
|
values.senderName
|
|
|
|
);
|
2022-07-14 00:10:45 +00:00
|
|
|
form.unregister("sendTo");
|
|
|
|
form.unregister("action");
|
2022-11-23 02:55:25 +00:00
|
|
|
form.unregister("numberRequired");
|
2022-07-14 00:10:45 +00:00
|
|
|
setIsOpenDialog(false);
|
|
|
|
setIsPhoneNumberNeeded(false);
|
2022-11-23 02:55:25 +00:00
|
|
|
setIsEmailAddressNeeded(false);
|
|
|
|
setIsSenderIdNeeded(false);
|
2022-07-14 00:10:45 +00:00
|
|
|
}}>
|
2022-11-23 02:55:25 +00:00
|
|
|
<div className="mt-5 space-y-1">
|
|
|
|
<Label htmlFor="label">{t("action")}:</Label>
|
2022-07-14 00:10:45 +00:00
|
|
|
<Controller
|
|
|
|
name="action"
|
|
|
|
control={form.control}
|
|
|
|
render={() => {
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
isSearchable={false}
|
2022-11-23 02:55:25 +00:00
|
|
|
className="text-sm"
|
2022-07-14 00:10:45 +00:00
|
|
|
defaultValue={actionOptions[0]}
|
2022-11-23 02:55:25 +00:00
|
|
|
onChange={handleSelectAction}
|
2022-07-14 00:10:45 +00:00
|
|
|
options={actionOptions}
|
2022-12-15 00:11:57 +00:00
|
|
|
isOptionDisabled={(option: {
|
|
|
|
label: string;
|
|
|
|
value: WorkflowActions;
|
2023-01-11 18:41:45 +00:00
|
|
|
needsUpgrade: boolean;
|
|
|
|
}) => option.needsUpgrade}
|
2022-07-14 00:10:45 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{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">
|
2022-11-23 02:55:25 +00:00
|
|
|
<Label htmlFor="sendTo">{t("phone_number")}</Label>
|
|
|
|
<div className="mt-1 mb-5">
|
2023-03-02 18:15:28 +00:00
|
|
|
<Controller
|
2022-07-14 00:10:45 +00:00
|
|
|
control={form.control}
|
|
|
|
name="sendTo"
|
2023-03-02 18:15:28 +00:00
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<PhoneInput
|
|
|
|
className="rounded-md"
|
|
|
|
placeholder={t("enter_phone_number")}
|
|
|
|
id="sendTo"
|
|
|
|
required
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
/>
|
|
|
|
)}
|
2022-07-14 00:10:45 +00:00
|
|
|
/>
|
|
|
|
{form.formState.errors.sendTo && (
|
|
|
|
<p className="mt-1 text-sm text-red-500">{form.formState.errors.sendTo.message}</p>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-11-23 02:55:25 +00:00
|
|
|
{isEmailAddressNeeded && (
|
|
|
|
<div className="mt-5">
|
|
|
|
<EmailField required label={t("email_address")} {...form.register("sendTo")} />
|
|
|
|
</div>
|
|
|
|
)}
|
2023-01-18 14:32:39 +00:00
|
|
|
{isSenderIdNeeded ? (
|
|
|
|
<>
|
|
|
|
<div className="mt-5">
|
|
|
|
<Label>{t("sender_id")}</Label>
|
|
|
|
<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>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
) : (
|
2022-11-23 02:55:25 +00:00
|
|
|
<div className="mt-5">
|
2023-01-18 14:32:39 +00:00
|
|
|
<Label>{t("sender_name")}</Label>
|
|
|
|
<Input type="text" placeholder={SENDER_NAME} {...form.register(`senderName`)} />
|
2022-11-23 02:55:25 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{form.getValues("action") === WorkflowActions.SMS_ATTENDEE && (
|
|
|
|
<div className="mt-5">
|
|
|
|
<Controller
|
|
|
|
name="numberRequired"
|
|
|
|
control={form.control}
|
|
|
|
render={() => (
|
|
|
|
<Checkbox
|
|
|
|
defaultChecked={form.getValues("numberRequired") || false}
|
|
|
|
description={t("make_phone_number_required")}
|
|
|
|
onChange={(e) => form.setValue("numberRequired", e.target.checked)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-07-14 00:10:45 +00:00
|
|
|
<DialogFooter>
|
2022-11-28 19:14:38 +00:00
|
|
|
<DialogClose
|
|
|
|
onClick={() => {
|
|
|
|
setIsOpenDialog(false);
|
|
|
|
form.unregister("sendTo");
|
|
|
|
form.unregister("action");
|
|
|
|
form.unregister("numberRequired");
|
|
|
|
setIsPhoneNumberNeeded(false);
|
|
|
|
setIsEmailAddressNeeded(false);
|
|
|
|
setIsSenderIdNeeded(false);
|
|
|
|
}}
|
|
|
|
/>
|
2022-07-14 00:10:45 +00:00
|
|
|
<Button type="submit">{t("add")}</Button>
|
|
|
|
</DialogFooter>
|
|
|
|
</Form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|