2022-08-31 19:42:37 +00:00
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
import { WorkflowActions } from "@prisma/client";
|
|
|
|
import { isValidPhoneNumber } from "libphonenumber-js";
|
|
|
|
import { Dispatch, SetStateAction, useState } from "react";
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
|
|
import PhoneInput from "@calcom/ui/form/PhoneInputLazy";
|
2022-10-10 13:40:20 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Dialog,
|
|
|
|
DialogClose,
|
|
|
|
DialogContent,
|
|
|
|
DialogFooter,
|
|
|
|
EmailField,
|
|
|
|
Form,
|
|
|
|
Label,
|
|
|
|
Select,
|
|
|
|
} from "@calcom/ui/v2";
|
2022-10-18 12:47:15 +00:00
|
|
|
import CheckboxField from "@calcom/ui/v2/core/form/Checkbox";
|
2022-08-31 19:42:37 +00:00
|
|
|
|
|
|
|
import { WORKFLOW_ACTIONS } from "../../lib/constants";
|
|
|
|
import { getWorkflowActionOptions } from "../../lib/getOptions";
|
|
|
|
|
|
|
|
interface IAddActionDialog {
|
|
|
|
isOpenDialog: boolean;
|
|
|
|
setIsOpenDialog: Dispatch<SetStateAction<boolean>>;
|
2022-10-18 12:47:15 +00:00
|
|
|
addAction: (action: WorkflowActions, sendTo?: string, numberRequired?: boolean) => void;
|
2022-08-31 19:42:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AddActionFormValues = {
|
|
|
|
action: WorkflowActions;
|
|
|
|
sendTo?: string;
|
2022-10-18 12:47:15 +00:00
|
|
|
numberRequired?: boolean;
|
2022-08-31 19:42:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const AddActionDialog = (props: IAddActionDialog) => {
|
|
|
|
const { t } = useLocale();
|
|
|
|
const { isOpenDialog, setIsOpenDialog, addAction } = props;
|
|
|
|
const [isPhoneNumberNeeded, setIsPhoneNumberNeeded] = useState(false);
|
2022-10-10 13:40:20 +00:00
|
|
|
const [isEmailAddressNeeded, setIsEmailAddressNeeded] = useState(false);
|
2022-08-31 19:42:37 +00:00
|
|
|
const actionOptions = getWorkflowActionOptions(t);
|
|
|
|
|
|
|
|
const formSchema = z.object({
|
|
|
|
action: z.enum(WORKFLOW_ACTIONS),
|
|
|
|
sendTo: z
|
|
|
|
.string()
|
2022-10-10 13:40:20 +00:00
|
|
|
.refine((val) => isValidPhoneNumber(val) || val.includes("@"))
|
2022-08-31 19:42:37 +00:00
|
|
|
.optional(),
|
2022-10-18 12:47:15 +00:00
|
|
|
numberRequired: z.boolean().optional(),
|
2022-08-31 19:42:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const form = useForm<AddActionFormValues>({
|
|
|
|
mode: "onSubmit",
|
|
|
|
defaultValues: {
|
|
|
|
action: WorkflowActions.EMAIL_HOST,
|
|
|
|
},
|
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog open={isOpenDialog} onOpenChange={setIsOpenDialog}>
|
2022-09-06 03:29:00 +00:00
|
|
|
<DialogContent type="creation" useOwnActionButtons={true} title={t("add_action")}>
|
2022-08-31 19:42:37 +00:00
|
|
|
<div className="space-x-3 ">
|
|
|
|
<div className="pt-1">
|
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
handleSubmit={(values) => {
|
2022-10-18 12:47:15 +00:00
|
|
|
addAction(values.action, values.sendTo, values.numberRequired);
|
2022-08-31 19:42:37 +00:00
|
|
|
form.unregister("sendTo");
|
|
|
|
form.unregister("action");
|
2022-10-18 12:47:15 +00:00
|
|
|
form.unregister("numberRequired");
|
2022-08-31 19:42:37 +00:00
|
|
|
setIsOpenDialog(false);
|
|
|
|
setIsPhoneNumberNeeded(false);
|
2022-10-10 13:40:20 +00:00
|
|
|
setIsEmailAddressNeeded(false);
|
2022-08-31 19:42:37 +00:00
|
|
|
}}>
|
2022-09-06 03:29:00 +00:00
|
|
|
<div className="mt-5 space-y-1">
|
|
|
|
<Label htmlFor="label">{t("action")}:</Label>
|
2022-08-31 19:42:37 +00:00
|
|
|
<Controller
|
|
|
|
name="action"
|
|
|
|
control={form.control}
|
|
|
|
render={() => {
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
isSearchable={false}
|
2022-09-06 03:29:00 +00:00
|
|
|
className="text-sm"
|
2022-08-31 19:42:37 +00:00
|
|
|
defaultValue={actionOptions[0]}
|
|
|
|
onChange={(val) => {
|
|
|
|
if (val) {
|
|
|
|
form.setValue("action", val.value);
|
|
|
|
if (val.value === WorkflowActions.SMS_NUMBER) {
|
|
|
|
setIsPhoneNumberNeeded(true);
|
2022-10-10 13:40:20 +00:00
|
|
|
setIsEmailAddressNeeded(false);
|
|
|
|
} else if (val.value === WorkflowActions.EMAIL_ADDRESS) {
|
|
|
|
setIsEmailAddressNeeded(true);
|
|
|
|
setIsPhoneNumberNeeded(false);
|
2022-08-31 19:42:37 +00:00
|
|
|
} else {
|
2022-10-10 13:40:20 +00:00
|
|
|
setIsEmailAddressNeeded(false);
|
2022-08-31 19:42:37 +00:00
|
|
|
setIsPhoneNumberNeeded(false);
|
|
|
|
}
|
2022-10-10 13:40:20 +00:00
|
|
|
form.unregister("sendTo");
|
2022-10-18 12:47:15 +00:00
|
|
|
form.unregister("numberRequired");
|
2022-08-31 19:42:37 +00:00
|
|
|
form.clearErrors("action");
|
2022-10-10 13:40:20 +00:00
|
|
|
form.clearErrors("sendTo");
|
2022-08-31 19:42:37 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
options={actionOptions}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{form.formState.errors.action && (
|
|
|
|
<p className="mt-1 text-sm text-red-500">{form.formState.errors.action.message}</p>
|
|
|
|
)}
|
|
|
|
</div>
|
2022-10-18 12:47:15 +00:00
|
|
|
{form.getValues("action") === WorkflowActions.SMS_ATTENDEE && (
|
|
|
|
<div className="mt-5">
|
|
|
|
<Controller
|
|
|
|
name="numberRequired"
|
|
|
|
control={form.control}
|
|
|
|
render={() => (
|
|
|
|
<CheckboxField
|
|
|
|
defaultChecked={form.getValues("numberRequired") || false}
|
|
|
|
description={t("make_phone_number_required")}
|
|
|
|
onChange={(e) => form.setValue("numberRequired", e.target.checked)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-08-31 19:42:37 +00:00
|
|
|
{isPhoneNumberNeeded && (
|
|
|
|
<div className="mt-5 space-y-1">
|
2022-09-06 03:29:00 +00:00
|
|
|
<Label htmlFor="sendTo">{t("phone_number")}</Label>
|
2022-08-31 19:42:37 +00:00
|
|
|
<div className="mt-1">
|
|
|
|
<PhoneInput<AddActionFormValues>
|
|
|
|
control={form.control}
|
|
|
|
name="sendTo"
|
|
|
|
className="rounded-md"
|
|
|
|
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-10-10 13:40:20 +00:00
|
|
|
{isEmailAddressNeeded && (
|
|
|
|
<div className="mt-5">
|
|
|
|
<EmailField required label={t("email_address")} {...form.register("sendTo")} />
|
|
|
|
</div>
|
|
|
|
)}
|
2022-08-31 19:42:37 +00:00
|
|
|
<DialogFooter>
|
|
|
|
<DialogClose asChild>
|
|
|
|
<Button
|
|
|
|
color="secondary"
|
|
|
|
onClick={() => {
|
|
|
|
setIsOpenDialog(false);
|
|
|
|
form.unregister("sendTo");
|
|
|
|
form.unregister("action");
|
2022-10-18 12:47:15 +00:00
|
|
|
form.unregister("numberRequired");
|
2022-08-31 19:42:37 +00:00
|
|
|
setIsPhoneNumberNeeded(false);
|
2022-10-10 13:40:20 +00:00
|
|
|
setIsEmailAddressNeeded(false);
|
2022-08-31 19:42:37 +00:00
|
|
|
}}>
|
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
|
|
|
</DialogClose>
|
|
|
|
<Button type="submit">{t("add")}</Button>
|
|
|
|
</DialogFooter>
|
|
|
|
</Form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|