2023-07-13 15:33:27 +00:00
|
|
|
import { useState } from "react";
|
2022-12-14 17:30:55 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
2023-03-03 13:02:02 +00:00
|
|
|
import type { Dayjs } from "@calcom/dayjs";
|
|
|
|
import dayjs from "@calcom/dayjs";
|
2022-12-16 12:27:42 +00:00
|
|
|
import { classNames } from "@calcom/lib";
|
2023-08-12 07:22:44 +00:00
|
|
|
import { yyyymmdd } from "@calcom/lib/date-fns";
|
2022-12-14 17:30:55 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-01-24 14:00:45 +00:00
|
|
|
import useMediaQuery from "@calcom/lib/hooks/useMediaQuery";
|
2023-03-03 13:02:02 +00:00
|
|
|
import type { WorkingHours } from "@calcom/types/schedule";
|
2022-12-14 17:30:55 +00:00
|
|
|
import {
|
|
|
|
Dialog,
|
|
|
|
DialogContent,
|
|
|
|
DialogTrigger,
|
|
|
|
DialogHeader,
|
|
|
|
DialogClose,
|
|
|
|
Switch,
|
|
|
|
Form,
|
|
|
|
Button,
|
|
|
|
} from "@calcom/ui";
|
|
|
|
|
2022-12-22 19:06:26 +00:00
|
|
|
import DatePicker from "../../calendars/DatePicker";
|
2023-03-03 13:02:02 +00:00
|
|
|
import type { TimeRange } from "./Schedule";
|
|
|
|
import { DayRanges } from "./Schedule";
|
2022-12-14 17:30:55 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
|
|
const noop = () => {};
|
|
|
|
|
|
|
|
const DateOverrideForm = ({
|
|
|
|
value,
|
|
|
|
workingHours,
|
|
|
|
excludedDates,
|
|
|
|
onChange,
|
|
|
|
onClose = noop,
|
|
|
|
}: {
|
|
|
|
workingHours?: WorkingHours[];
|
|
|
|
onChange: (newValue: TimeRange[]) => void;
|
|
|
|
excludedDates: string[];
|
|
|
|
value?: TimeRange[];
|
|
|
|
onClose?: () => void;
|
|
|
|
}) => {
|
|
|
|
const [browsingDate, setBrowsingDate] = useState<Dayjs>();
|
|
|
|
const { t, i18n, isLocaleReady } = useLocale();
|
|
|
|
const [datesUnavailable, setDatesUnavailable] = useState(
|
|
|
|
value &&
|
2023-07-13 00:32:11 +00:00
|
|
|
value[0].start.getUTCHours() === 0 &&
|
|
|
|
value[0].start.getUTCMinutes() === 0 &&
|
|
|
|
value[0].end.getUTCHours() === 0 &&
|
|
|
|
value[0].end.getUTCMinutes() === 0
|
2022-12-14 17:30:55 +00:00
|
|
|
);
|
|
|
|
|
2023-08-12 07:22:44 +00:00
|
|
|
const [selectedDates, setSelectedDates] = useState<Dayjs[]>(value ? [dayjs.utc(value[0].start)] : []);
|
|
|
|
|
2023-09-01 16:12:01 +00:00
|
|
|
const onDateChange = (newDate: Dayjs) => {
|
2023-08-12 07:22:44 +00:00
|
|
|
// If clicking on a selected date unselect it
|
|
|
|
if (selectedDates.some((date) => yyyymmdd(date) === yyyymmdd(newDate))) {
|
|
|
|
setSelectedDates(selectedDates.filter((date) => yyyymmdd(date) !== yyyymmdd(newDate)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's not editing we can allow multiple select
|
|
|
|
if (!value) {
|
|
|
|
setSelectedDates((prev) => [...prev, newDate]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setSelectedDates([newDate]);
|
|
|
|
};
|
2023-07-13 15:33:27 +00:00
|
|
|
|
|
|
|
const defaultRanges = (workingHours || []).reduce((dayRanges: TimeRange[], workingHour) => {
|
2023-08-12 07:22:44 +00:00
|
|
|
if (selectedDates[0] && workingHour.days.includes(selectedDates[0].day())) {
|
2023-07-13 15:33:27 +00:00
|
|
|
dayRanges.push({
|
|
|
|
start: dayjs.utc().startOf("day").add(workingHour.startTime, "minute").toDate(),
|
|
|
|
end: dayjs.utc().startOf("day").add(workingHour.endTime, "minute").toDate(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return dayRanges;
|
|
|
|
}, []);
|
|
|
|
// DayRanges does not support empty state, add 9-5 as a default
|
|
|
|
if (!defaultRanges.length) {
|
|
|
|
defaultRanges.push({
|
|
|
|
start: dayjs.utc().startOf("day").add(540, "minute").toDate(),
|
|
|
|
end: dayjs.utc().startOf("day").add(1020, "minute").toDate(),
|
|
|
|
});
|
|
|
|
}
|
2022-12-14 17:30:55 +00:00
|
|
|
|
2023-03-03 13:02:02 +00:00
|
|
|
const form = useForm({
|
|
|
|
values: {
|
|
|
|
range: value
|
|
|
|
? value.map((range) => ({
|
|
|
|
start: new Date(
|
|
|
|
dayjs
|
|
|
|
.utc()
|
|
|
|
.hour(range.start.getUTCHours())
|
|
|
|
.minute(range.start.getUTCMinutes())
|
|
|
|
.second(0)
|
|
|
|
.format()
|
|
|
|
),
|
|
|
|
end: new Date(
|
|
|
|
dayjs.utc().hour(range.end.getUTCHours()).minute(range.end.getUTCMinutes()).second(0).format()
|
|
|
|
),
|
|
|
|
}))
|
2023-07-13 15:33:27 +00:00
|
|
|
: defaultRanges,
|
2023-03-03 13:02:02 +00:00
|
|
|
},
|
|
|
|
});
|
2022-12-14 17:30:55 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
handleSubmit={(values) => {
|
2023-08-12 07:22:44 +00:00
|
|
|
if (selectedDates.length === 0) return;
|
|
|
|
|
|
|
|
const datesInRanges: TimeRange[] = [];
|
|
|
|
|
|
|
|
if (!datesUnavailable) {
|
|
|
|
selectedDates.map((date) => {
|
|
|
|
values.range.map((item) => {
|
|
|
|
datesInRanges.push({
|
|
|
|
start: date
|
|
|
|
.hour(item.start.getUTCHours())
|
|
|
|
.minute(item.start.getUTCMinutes())
|
|
|
|
.utc(true)
|
|
|
|
.toDate(),
|
|
|
|
end: date.hour(item.end.getUTCHours()).minute(item.end.getUTCMinutes()).utc(true).toDate(),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-14 17:30:55 +00:00
|
|
|
onChange(
|
2023-08-12 07:22:44 +00:00
|
|
|
datesUnavailable
|
|
|
|
? selectedDates.map((date) => {
|
|
|
|
return {
|
2023-03-03 13:02:02 +00:00
|
|
|
start: date.utc(true).startOf("day").toDate(),
|
|
|
|
end: date.utc(true).startOf("day").add(1, "day").toDate(),
|
2023-08-12 07:22:44 +00:00
|
|
|
};
|
|
|
|
})
|
|
|
|
: datesInRanges
|
2022-12-14 17:30:55 +00:00
|
|
|
);
|
|
|
|
onClose();
|
|
|
|
}}
|
2023-09-11 10:49:27 +00:00
|
|
|
className="p-6 sm:flex sm:p-0 md:flex-col lg:flex-col xl:flex-row">
|
2023-08-12 07:22:44 +00:00
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
selectedDates[0] && "sm:border-subtle w-full sm:border-r sm:pr-6",
|
|
|
|
"sm:p-4 md:p-8"
|
|
|
|
)}>
|
2022-12-14 17:30:55 +00:00
|
|
|
<DialogHeader title={t("date_overrides_dialog_title")} />
|
|
|
|
<DatePicker
|
|
|
|
excludedDates={excludedDates}
|
|
|
|
weekStart={0}
|
2023-08-12 07:22:44 +00:00
|
|
|
selected={selectedDates}
|
2023-09-01 16:12:01 +00:00
|
|
|
onChange={(day) => {
|
|
|
|
if (day) onDateChange(day);
|
|
|
|
}}
|
2022-12-14 17:30:55 +00:00
|
|
|
onMonthChange={(newMonth) => {
|
|
|
|
setBrowsingDate(newMonth);
|
|
|
|
}}
|
|
|
|
browsingDate={browsingDate}
|
|
|
|
locale={isLocaleReady ? i18n.language : "en"}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-08-12 07:22:44 +00:00
|
|
|
{selectedDates[0] && (
|
2023-03-20 17:55:40 +00:00
|
|
|
<div className="relative mt-8 flex w-full flex-col sm:mt-0 sm:p-4 md:p-8">
|
2022-12-14 17:30:55 +00:00
|
|
|
<div className="mb-4 flex-grow space-y-4">
|
2023-04-05 18:14:46 +00:00
|
|
|
<p className="text-medium text-emphasis text-sm">{t("date_overrides_dialog_which_hours")}</p>
|
2022-12-14 17:30:55 +00:00
|
|
|
<div>
|
|
|
|
{datesUnavailable ? (
|
2023-07-13 15:33:27 +00:00
|
|
|
<p className="text-subtle border-default rounded border p-2 text-sm">
|
|
|
|
{t("date_overrides_unavailable")}
|
|
|
|
</p>
|
2022-12-14 17:30:55 +00:00
|
|
|
) : (
|
|
|
|
<DayRanges name="range" />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<Switch
|
|
|
|
label={t("date_overrides_mark_all_day_unavailable_one")}
|
|
|
|
checked={datesUnavailable}
|
|
|
|
onCheckedChange={setDatesUnavailable}
|
2022-12-15 20:19:35 +00:00
|
|
|
data-testid="date-override-mark-unavailable"
|
2022-12-14 17:30:55 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2023-03-20 17:55:40 +00:00
|
|
|
<div className="mt-4 flex flex-row-reverse sm:mt-0">
|
2022-12-15 20:19:35 +00:00
|
|
|
<Button
|
|
|
|
className="ml-2"
|
|
|
|
color="primary"
|
|
|
|
type="submit"
|
2023-08-12 07:22:44 +00:00
|
|
|
disabled={selectedDates.length === 0}
|
2022-12-15 20:19:35 +00:00
|
|
|
data-testid="add-override-submit-btn">
|
2022-12-14 17:30:55 +00:00
|
|
|
{value ? t("date_overrides_update_btn") : t("date_overrides_add_btn")}
|
|
|
|
</Button>
|
|
|
|
<DialogClose onClick={onClose} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const DateOverrideInputDialog = ({
|
|
|
|
Trigger,
|
|
|
|
excludedDates = [],
|
|
|
|
...passThroughProps
|
|
|
|
}: {
|
|
|
|
workingHours: WorkingHours[];
|
|
|
|
excludedDates?: string[];
|
|
|
|
Trigger: React.ReactNode;
|
|
|
|
onChange: (newValue: TimeRange[]) => void;
|
|
|
|
value?: TimeRange[];
|
|
|
|
}) => {
|
2023-01-24 14:00:45 +00:00
|
|
|
const isMobile = useMediaQuery("(max-width: 768px)");
|
2022-12-14 17:30:55 +00:00
|
|
|
const [open, setOpen] = useState(false);
|
2023-01-24 14:00:45 +00:00
|
|
|
{
|
|
|
|
/* enableOverflow is used to allow overflow when there are too many overrides to show on mobile.
|
|
|
|
ref:- https://github.com/calcom/cal.com/pull/6215
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
const enableOverflow = isMobile;
|
2022-12-14 17:30:55 +00:00
|
|
|
return (
|
|
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
|
|
<DialogTrigger asChild>{Trigger}</DialogTrigger>
|
2023-01-24 14:00:45 +00:00
|
|
|
|
2023-09-11 10:49:27 +00:00
|
|
|
<DialogContent enableOverflow={enableOverflow} size="md" className="p-0 md:w-auto">
|
2022-12-14 17:30:55 +00:00
|
|
|
<DateOverrideForm
|
|
|
|
excludedDates={excludedDates}
|
|
|
|
{...passThroughProps}
|
|
|
|
onClose={() => setOpen(false)}
|
|
|
|
/>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DateOverrideInputDialog;
|