2022-07-14 00:10:45 +00:00
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
import { WorkflowActions } from "@prisma/client";
|
|
|
|
import { isValidPhoneNumber } from "libphonenumber-js";
|
2022-07-28 19:58:26 +00:00
|
|
|
import { Dispatch, SetStateAction, useState } from "react";
|
2022-07-14 00:10:45 +00:00
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-11-23 02:55:25 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Checkbox,
|
|
|
|
Dialog,
|
|
|
|
DialogClose,
|
|
|
|
DialogContent,
|
|
|
|
DialogFooter,
|
|
|
|
EmailField,
|
|
|
|
Form,
|
|
|
|
Label,
|
|
|
|
PhoneInput,
|
|
|
|
Select,
|
|
|
|
TextField,
|
|
|
|
} 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";
|
|
|
|
import { getWorkflowActionOptions } from "../lib/getOptions";
|
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>>;
|
2022-11-23 02:55:25 +00:00
|
|
|
addAction: (action: WorkflowActions, sendTo?: string, numberRequired?: boolean, sender?: string) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
sender?: string;
|
|
|
|
};
|
|
|
|
|
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-11-23 22:42:42 +00:00
|
|
|
const actionOptions = getWorkflowActionOptions(t);
|
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(),
|
|
|
|
sender: z
|
|
|
|
.string()
|
|
|
|
.refine((val) => onlyLettersNumbersSpaces(val))
|
|
|
|
.nullable(),
|
2022-07-14 00:10:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const form = useForm<AddActionFormValues>({
|
|
|
|
mode: "onSubmit",
|
|
|
|
defaultValues: {
|
|
|
|
action: WorkflowActions.EMAIL_HOST,
|
2022-11-23 02:55:25 +00:00
|
|
|
sender: "Cal",
|
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-07-14 00:10:45 +00:00
|
|
|
return (
|
|
|
|
<Dialog open={isOpenDialog} onOpenChange={setIsOpenDialog}>
|
2022-11-23 02:55:25 +00:00
|
|
|
<DialogContent type="creation" useOwnActionButtons={true} 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) => {
|
2022-11-23 02:55:25 +00:00
|
|
|
addAction(values.action, values.sendTo, values.numberRequired, values.sender);
|
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}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{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">
|
2022-07-14 00:10:45 +00:00
|
|
|
<PhoneInput<AddActionFormValues>
|
|
|
|
control={form.control}
|
|
|
|
name="sendTo"
|
2022-11-23 02:55:25 +00:00
|
|
|
className="rounded-md"
|
2022-07-14 00:10:45 +00:00
|
|
|
placeholder={t("enter_phone_number")}
|
|
|
|
id="sendTo"
|
|
|
|
required
|
|
|
|
/>
|
|
|
|
{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>
|
|
|
|
)}
|
|
|
|
{isSenderIdNeeded && (
|
|
|
|
<div className="mt-5">
|
|
|
|
<TextField
|
|
|
|
label={t("sender_id")}
|
|
|
|
type="text"
|
|
|
|
placeholder="Cal"
|
|
|
|
maxLength={11}
|
|
|
|
{...form.register(`sender`)}
|
|
|
|
/>
|
|
|
|
</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>
|
|
|
|
<DialogClose asChild>
|
|
|
|
<Button
|
|
|
|
color="secondary"
|
|
|
|
onClick={() => {
|
|
|
|
setIsOpenDialog(false);
|
|
|
|
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
|
|
|
setIsPhoneNumberNeeded(false);
|
2022-11-23 02:55:25 +00:00
|
|
|
setIsEmailAddressNeeded(false);
|
|
|
|
setIsSenderIdNeeded(false);
|
2022-07-14 00:10:45 +00:00
|
|
|
}}>
|
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
|
|
|
</DialogClose>
|
|
|
|
<Button type="submit">{t("add")}</Button>
|
|
|
|
</DialogFooter>
|
|
|
|
</Form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|