2022-08-26 00:48:50 +00:00
|
|
|
import { ErrorMessage } from "@hookform/error-message";
|
2022-05-27 23:27:41 +00:00
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
import { isValidPhoneNumber } from "libphonenumber-js";
|
|
|
|
import { useEffect } from "react";
|
|
|
|
import { Controller, useForm, useWatch } from "react-hook-form";
|
2022-11-24 11:53:29 +00:00
|
|
|
import { components } from "react-select";
|
2022-05-27 23:27:41 +00:00
|
|
|
import { z } from "zod";
|
|
|
|
|
2022-08-26 00:48:50 +00:00
|
|
|
import {
|
|
|
|
EventLocationType,
|
2022-11-23 02:55:25 +00:00
|
|
|
getEventLocationType,
|
|
|
|
getHumanReadableLocationValue,
|
|
|
|
getMessageForOrganizer,
|
2022-08-26 00:48:50 +00:00
|
|
|
LocationObject,
|
2022-11-23 02:55:25 +00:00
|
|
|
LocationType,
|
2022-08-26 00:48:50 +00:00
|
|
|
} from "@calcom/app-store/locations";
|
2022-11-24 11:53:29 +00:00
|
|
|
import { classNames } from "@calcom/lib";
|
2022-05-27 23:27:41 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-11-10 23:40:01 +00:00
|
|
|
import { RouterOutputs, trpc } from "@calcom/trpc/react";
|
2022-11-25 20:19:08 +00:00
|
|
|
import { Button, Dialog, DialogClose, DialogContent, DialogFooter, Form, Icon, PhoneInput } from "@calcom/ui";
|
2022-05-27 23:27:41 +00:00
|
|
|
|
|
|
|
import { QueryCell } from "@lib/QueryCell";
|
|
|
|
|
|
|
|
import CheckboxField from "@components/ui/form/CheckboxField";
|
|
|
|
import Select from "@components/ui/form/Select";
|
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
type BookingItem = RouterOutputs["viewer"]["bookings"]["get"]["bookings"][number];
|
2022-05-27 23:27:41 +00:00
|
|
|
|
|
|
|
type OptionTypeBase = {
|
|
|
|
label: string;
|
2022-08-26 00:48:50 +00:00
|
|
|
value: EventLocationType["type"];
|
2022-05-27 23:27:41 +00:00
|
|
|
disabled?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
interface ISetLocationDialog {
|
2022-08-26 00:48:50 +00:00
|
|
|
saveLocation: (newLocationType: EventLocationType["type"], details: { [key: string]: string }) => void;
|
2022-05-27 23:27:41 +00:00
|
|
|
selection?: OptionTypeBase;
|
|
|
|
booking?: BookingItem;
|
2022-08-26 00:48:50 +00:00
|
|
|
defaultValues?: LocationObject[];
|
2022-05-27 23:27:41 +00:00
|
|
|
setShowLocationModal: React.Dispatch<React.SetStateAction<boolean>>;
|
|
|
|
isOpenDialog: boolean;
|
|
|
|
setSelectedLocation?: (param: OptionTypeBase | undefined) => void;
|
2022-11-24 11:53:29 +00:00
|
|
|
setEditingLocationType?: (param: string) => void;
|
2022-05-27 23:27:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 00:48:50 +00:00
|
|
|
const LocationInput = (props: {
|
|
|
|
eventLocationType: EventLocationType;
|
|
|
|
locationFormMethods: ReturnType<typeof useForm>;
|
|
|
|
id: string;
|
|
|
|
required: boolean;
|
|
|
|
placeholder: string;
|
|
|
|
className?: string;
|
|
|
|
defaultValue?: string;
|
|
|
|
}): JSX.Element | null => {
|
|
|
|
const { eventLocationType, locationFormMethods, ...remainingProps } = props;
|
|
|
|
if (eventLocationType?.organizerInputType === "text") {
|
|
|
|
return (
|
|
|
|
<input {...locationFormMethods.register(eventLocationType.variable)} type="text" {...remainingProps} />
|
|
|
|
);
|
|
|
|
} else if (eventLocationType?.organizerInputType === "phone") {
|
|
|
|
return (
|
|
|
|
<PhoneInput
|
|
|
|
name={eventLocationType.variable}
|
|
|
|
control={locationFormMethods.control}
|
|
|
|
{...remainingProps}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2022-05-27 23:27:41 +00:00
|
|
|
export const EditLocationDialog = (props: ISetLocationDialog) => {
|
|
|
|
const {
|
|
|
|
saveLocation,
|
|
|
|
selection,
|
|
|
|
booking,
|
|
|
|
setShowLocationModal,
|
|
|
|
isOpenDialog,
|
|
|
|
defaultValues,
|
|
|
|
setSelectedLocation,
|
2022-11-24 11:53:29 +00:00
|
|
|
setEditingLocationType,
|
2022-05-27 23:27:41 +00:00
|
|
|
} = props;
|
|
|
|
const { t } = useLocale();
|
2022-11-10 23:40:01 +00:00
|
|
|
const locationsQuery = trpc.viewer.locationOptions.useQuery();
|
2022-05-27 23:27:41 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (selection) {
|
|
|
|
locationFormMethods.setValue("locationType", selection?.value);
|
|
|
|
}
|
2022-05-30 14:51:18 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2022-05-27 23:27:41 +00:00
|
|
|
}, [selection]);
|
|
|
|
|
|
|
|
const locationFormSchema = z.object({
|
|
|
|
locationType: z.string(),
|
2022-08-26 00:48:50 +00:00
|
|
|
phone: z.string().optional().nullable(),
|
2022-05-27 23:27:41 +00:00
|
|
|
locationAddress: z.string().optional(),
|
2022-08-26 00:48:50 +00:00
|
|
|
locationLink: z
|
|
|
|
.string()
|
|
|
|
.optional()
|
|
|
|
.superRefine((val, ctx) => {
|
|
|
|
if (
|
|
|
|
eventLocationType &&
|
|
|
|
!eventLocationType.default &&
|
|
|
|
eventLocationType.linkType === "static" &&
|
|
|
|
eventLocationType.urlRegExp
|
|
|
|
) {
|
|
|
|
const valid = z.string().regex(new RegExp(eventLocationType.urlRegExp)).safeParse(val).success;
|
|
|
|
if (!valid) {
|
|
|
|
const sampleUrl = eventLocationType.organizerInputPlaceholder;
|
|
|
|
ctx.addIssue({
|
|
|
|
code: z.ZodIssueCode.custom,
|
|
|
|
message: `Invalid URL for ${eventLocationType.label}. ${
|
|
|
|
sampleUrl ? "Sample URL: " + sampleUrl : ""
|
|
|
|
}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const valid = z.string().url().optional().safeParse(val).success;
|
|
|
|
if (!valid) {
|
|
|
|
ctx.addIssue({
|
|
|
|
code: z.ZodIssueCode.custom,
|
|
|
|
message: `Invalid URL`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}),
|
2022-05-27 23:27:41 +00:00
|
|
|
displayLocationPublicly: z.boolean().optional(),
|
|
|
|
locationPhoneNumber: z
|
|
|
|
.string()
|
2022-08-26 00:48:50 +00:00
|
|
|
.nullable()
|
|
|
|
.refine((val) => {
|
|
|
|
if (val === null) return false;
|
|
|
|
return isValidPhoneNumber(val);
|
|
|
|
})
|
2022-05-27 23:27:41 +00:00
|
|
|
.optional(),
|
|
|
|
});
|
|
|
|
|
2022-08-26 00:48:50 +00:00
|
|
|
const locationFormMethods = useForm({
|
2022-05-27 23:27:41 +00:00
|
|
|
mode: "onSubmit",
|
|
|
|
resolver: zodResolver(locationFormSchema),
|
|
|
|
});
|
|
|
|
|
|
|
|
const selectedLocation = useWatch({
|
|
|
|
control: locationFormMethods.control,
|
|
|
|
name: "locationType",
|
|
|
|
});
|
|
|
|
|
2022-08-26 00:48:50 +00:00
|
|
|
const eventLocationType = getEventLocationType(selectedLocation);
|
|
|
|
|
|
|
|
const defaultLocation = defaultValues?.find(
|
|
|
|
(location: { type: EventLocationType["type"] }) => location.type === eventLocationType?.type
|
|
|
|
);
|
|
|
|
|
|
|
|
const LocationOptions = (() => {
|
|
|
|
if (eventLocationType && eventLocationType.organizerInputType && LocationInput) {
|
|
|
|
if (!eventLocationType.variable) {
|
|
|
|
console.error("eventLocationType.variable can't be undefined");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-06-27 10:17:38 +00:00
|
|
|
<div>
|
2022-08-26 00:48:50 +00:00
|
|
|
<label htmlFor="locationInput" className="block text-sm font-medium text-gray-700">
|
|
|
|
{t(eventLocationType.messageForOrganizer || "")}
|
2022-06-27 10:17:38 +00:00
|
|
|
</label>
|
|
|
|
<div className="mt-1">
|
2022-08-26 00:48:50 +00:00
|
|
|
<LocationInput
|
|
|
|
locationFormMethods={locationFormMethods}
|
|
|
|
eventLocationType={eventLocationType}
|
|
|
|
id="locationInput"
|
|
|
|
placeholder={t(eventLocationType.organizerInputPlaceholder || "")}
|
2022-06-27 10:17:38 +00:00
|
|
|
required
|
2022-07-12 17:50:04 +00:00
|
|
|
className="block w-full rounded-sm border-gray-300 text-sm"
|
2022-08-26 00:48:50 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
2022-06-27 10:17:38 +00:00
|
|
|
defaultValue={
|
2022-08-26 00:48:50 +00:00
|
|
|
defaultLocation ? defaultLocation[eventLocationType.defaultValueVariable] : undefined
|
2022-06-27 10:17:38 +00:00
|
|
|
}
|
|
|
|
/>
|
2022-08-26 00:48:50 +00:00
|
|
|
<ErrorMessage
|
|
|
|
errors={locationFormMethods.formState.errors}
|
|
|
|
name={eventLocationType.variable}
|
|
|
|
className="mt-1 text-sm text-red-500"
|
|
|
|
as="p"
|
2022-08-08 05:57:09 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{!booking && (
|
|
|
|
<div className="mt-3">
|
|
|
|
<Controller
|
|
|
|
name="displayLocationPublicly"
|
|
|
|
control={locationFormMethods.control}
|
|
|
|
render={() => (
|
|
|
|
<CheckboxField
|
2022-08-26 00:48:50 +00:00
|
|
|
defaultChecked={defaultLocation?.displayLocationPublicly}
|
2022-08-08 05:57:09 +00:00
|
|
|
description={t("display_location_label")}
|
|
|
|
onChange={(e) =>
|
|
|
|
locationFormMethods.setValue("displayLocationPublicly", e.target.checked)
|
|
|
|
}
|
|
|
|
informationIconText={t("display_location_info_badge")}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2022-08-26 00:48:50 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return <p className="text-sm">{getMessageForOrganizer(selectedLocation, t)}</p>;
|
|
|
|
}
|
|
|
|
})();
|
2022-05-27 23:27:41 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog open={isOpenDialog}>
|
2022-11-28 19:14:38 +00:00
|
|
|
<DialogContent>
|
2022-11-25 20:19:08 +00:00
|
|
|
<div className="flex flex-row space-x-3">
|
|
|
|
<div className="bg-secondary-100 mx-auto flex h-12 w-12 flex-shrink-0 items-center justify-center rounded-full sm:mx-0 sm:h-10 sm:w-10">
|
|
|
|
<Icon.FiMapPin className="text-primary-600 h-6 w-6" />
|
|
|
|
</div>
|
|
|
|
<div className="w-full">
|
|
|
|
<div className="mt-3 text-center sm:mt-0 sm:text-left">
|
2022-05-27 23:27:41 +00:00
|
|
|
<h3 className="text-lg font-medium leading-6 text-gray-900" id="modal-title">
|
|
|
|
{t("edit_location")}
|
|
|
|
</h3>
|
|
|
|
{!booking && (
|
|
|
|
<p className="text-sm text-gray-400">{t("this_input_will_shown_booking_this_event")}</p>
|
|
|
|
)}
|
|
|
|
</div>
|
2022-11-25 20:19:08 +00:00
|
|
|
<div className="mt-3 text-center sm:mt-0 sm:text-left" />
|
2022-05-27 23:27:41 +00:00
|
|
|
|
2022-11-25 20:19:08 +00:00
|
|
|
{booking && (
|
|
|
|
<>
|
|
|
|
<p className="mt-6 mb-2 ml-1 text-sm font-bold text-black">{t("current_location")}:</p>
|
|
|
|
<p className="mb-2 ml-1 text-sm text-black">
|
|
|
|
{getHumanReadableLocationValue(booking.location, t)}
|
|
|
|
</p>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
<Form
|
|
|
|
form={locationFormMethods}
|
|
|
|
handleSubmit={async (values) => {
|
|
|
|
const { locationType: newLocation, displayLocationPublicly } = values;
|
2022-08-26 00:48:50 +00:00
|
|
|
|
2022-11-25 20:19:08 +00:00
|
|
|
let details = {};
|
|
|
|
if (newLocation === LocationType.InPerson) {
|
|
|
|
details = {
|
|
|
|
address: values.locationAddress,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const eventLocationType = getEventLocationType(newLocation);
|
2022-06-27 10:17:38 +00:00
|
|
|
|
2022-11-25 20:19:08 +00:00
|
|
|
// TODO: There can be a property that tells if it is to be saved in `link`
|
|
|
|
if (
|
|
|
|
newLocation === LocationType.Link ||
|
|
|
|
(!eventLocationType?.default && eventLocationType?.linkType === "static")
|
|
|
|
) {
|
|
|
|
details = { link: values.locationLink };
|
|
|
|
}
|
2022-05-27 23:27:41 +00:00
|
|
|
|
2022-11-25 20:19:08 +00:00
|
|
|
if (newLocation === LocationType.UserPhone) {
|
|
|
|
details = { hostPhoneNumber: values.locationPhoneNumber };
|
|
|
|
}
|
2022-08-26 00:48:50 +00:00
|
|
|
|
2022-11-25 20:19:08 +00:00
|
|
|
if (eventLocationType?.organizerInputType) {
|
|
|
|
details = {
|
|
|
|
...details,
|
|
|
|
displayLocationPublicly,
|
|
|
|
};
|
2022-11-24 11:53:29 +00:00
|
|
|
}
|
2022-11-25 20:19:08 +00:00
|
|
|
|
|
|
|
saveLocation(newLocation, details);
|
|
|
|
setShowLocationModal(false);
|
|
|
|
setSelectedLocation?.(undefined);
|
|
|
|
locationFormMethods.unregister([
|
|
|
|
"locationType",
|
|
|
|
"locationLink",
|
|
|
|
"locationAddress",
|
|
|
|
"locationPhoneNumber",
|
|
|
|
]);
|
|
|
|
}}>
|
|
|
|
<QueryCell
|
|
|
|
query={locationsQuery}
|
|
|
|
success={({ data: locationOptions }) => {
|
|
|
|
if (!locationOptions.length) return null;
|
|
|
|
if (booking) {
|
|
|
|
locationOptions.forEach((location) => {
|
|
|
|
if (location.label === "phone") {
|
|
|
|
location.options.filter((l) => l.value !== "phone");
|
|
|
|
} else if (location.label === "in person") {
|
|
|
|
location.options.filter((l) => l.value !== "attendeeInPerson");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Controller
|
|
|
|
name="locationType"
|
|
|
|
control={locationFormMethods.control}
|
|
|
|
render={() => (
|
|
|
|
<Select<{ label: string; value: string; icon?: string }>
|
|
|
|
maxMenuHeight={150}
|
|
|
|
name="location"
|
|
|
|
defaultValue={selection}
|
|
|
|
options={locationOptions}
|
|
|
|
components={{
|
|
|
|
Option: (props) => (
|
|
|
|
<components.Option {...props}>
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
{props.data.icon && (
|
|
|
|
<img src={props.data.icon} alt="cover" className="h-3.5 w-3.5" />
|
|
|
|
)}
|
|
|
|
<span
|
|
|
|
className={classNames(
|
|
|
|
"text-sm font-medium",
|
|
|
|
props.isSelected ? "text-white" : "text-gray-900"
|
|
|
|
)}>
|
|
|
|
{props.data.label}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</components.Option>
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
formatOptionLabel={(e) => (
|
|
|
|
<div className="flex items-center gap-3">
|
|
|
|
{e.icon && <img src={e.icon} alt="app-icon" className="h-5 w-5" />}
|
|
|
|
<span>{e.label}</span>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
formatGroupLabel={(e) => (
|
|
|
|
<p className="text-xs font-medium text-gray-600">{e.label}</p>
|
|
|
|
)}
|
|
|
|
isSearchable
|
|
|
|
className="my-4 block w-full min-w-0 flex-1 rounded-sm border border-gray-300 text-sm"
|
|
|
|
onChange={(val) => {
|
|
|
|
if (val) {
|
|
|
|
locationFormMethods.setValue("locationType", val.value);
|
|
|
|
locationFormMethods.unregister([
|
|
|
|
"locationLink",
|
|
|
|
"locationAddress",
|
|
|
|
"locationPhoneNumber",
|
|
|
|
]);
|
|
|
|
locationFormMethods.clearErrors([
|
|
|
|
"locationLink",
|
|
|
|
"locationPhoneNumber",
|
|
|
|
"locationAddress",
|
|
|
|
]);
|
|
|
|
setSelectedLocation?.(val);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
2022-05-27 23:27:41 +00:00
|
|
|
}}
|
2022-11-25 20:19:08 +00:00
|
|
|
/>
|
|
|
|
{selectedLocation && LocationOptions}
|
|
|
|
<DialogFooter>
|
|
|
|
<div className="mt-4 flex justify-end space-x-2">
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
setShowLocationModal(false);
|
|
|
|
setSelectedLocation?.(undefined);
|
|
|
|
setEditingLocationType?.("");
|
|
|
|
locationFormMethods.unregister("locationType");
|
|
|
|
}}
|
|
|
|
type="button"
|
|
|
|
color="secondary">
|
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
<Button type="submit">{t("update")}</Button>
|
|
|
|
</div>
|
|
|
|
</DialogFooter>
|
|
|
|
</Form>
|
|
|
|
</div>
|
2022-05-27 23:27:41 +00:00
|
|
|
</div>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|