2023-03-03 13:02:02 +00:00
|
|
|
import { useState, useMemo } 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";
|
2022-12-14 17:30:55 +00:00
|
|
|
import { daysInMonth, yyyymmdd } from "@calcom/lib/date-fns";
|
|
|
|
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 &&
|
|
|
|
value[0].start.getHours() === 0 &&
|
|
|
|
value[0].start.getMinutes() === 0 &&
|
|
|
|
value[0].end.getHours() === 0 &&
|
|
|
|
value[0].end.getMinutes() === 0
|
|
|
|
);
|
|
|
|
|
|
|
|
const [date, setDate] = useState<Dayjs | null>(value ? dayjs(value[0].start) : null);
|
|
|
|
const includedDates = useMemo(
|
|
|
|
() =>
|
|
|
|
workingHours
|
|
|
|
? workingHours.reduce((dates, workingHour) => {
|
|
|
|
for (let dNum = 1; dNum <= daysInMonth(browsingDate || dayjs()); dNum++) {
|
|
|
|
const d = browsingDate ? browsingDate.date(dNum) : dayjs.utc().date(dNum);
|
|
|
|
if (workingHour.days.includes(d.day())) {
|
|
|
|
dates.push(yyyymmdd(d));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dates;
|
|
|
|
}, [] as string[])
|
|
|
|
: [],
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
[browsingDate]
|
|
|
|
);
|
|
|
|
|
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()
|
|
|
|
),
|
|
|
|
}))
|
|
|
|
: (workingHours || []).reduce((dayRanges, workingHour) => {
|
|
|
|
if (date && workingHour.days.includes(date.day())) {
|
|
|
|
dayRanges.push({
|
|
|
|
start: dayjs.utc().startOf("day").add(workingHour.startTime, "minute").toDate(),
|
|
|
|
end: dayjs.utc().startOf("day").add(workingHour.endTime, "minute").toDate(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return dayRanges;
|
|
|
|
}, [] as TimeRange[]),
|
|
|
|
},
|
|
|
|
});
|
2022-12-14 17:30:55 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
handleSubmit={(values) => {
|
|
|
|
if (!date) return;
|
|
|
|
onChange(
|
2023-03-03 13:02:02 +00:00
|
|
|
(datesUnavailable
|
|
|
|
? [
|
|
|
|
{
|
|
|
|
start: date.utc(true).startOf("day").toDate(),
|
|
|
|
end: date.utc(true).startOf("day").add(1, "day").toDate(),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
: values.range
|
|
|
|
).map((item) => ({
|
2022-12-14 17:30:55 +00:00
|
|
|
start: date.hour(item.start.getHours()).minute(item.start.getMinutes()).toDate(),
|
|
|
|
end: date.hour(item.end.getHours()).minute(item.end.getMinutes()).toDate(),
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
onClose();
|
|
|
|
}}
|
2022-12-16 12:27:42 +00:00
|
|
|
className="space-y-4 sm:flex sm:space-x-4">
|
|
|
|
<div className={classNames(date && "w-full sm:border-r sm:pr-6")}>
|
2022-12-14 17:30:55 +00:00
|
|
|
<DialogHeader title={t("date_overrides_dialog_title")} />
|
|
|
|
<DatePicker
|
|
|
|
includedDates={includedDates}
|
|
|
|
excludedDates={excludedDates}
|
|
|
|
weekStart={0}
|
|
|
|
selected={date}
|
|
|
|
onChange={(day) => setDate(day)}
|
|
|
|
onMonthChange={(newMonth) => {
|
|
|
|
setBrowsingDate(newMonth);
|
|
|
|
}}
|
|
|
|
browsingDate={browsingDate}
|
|
|
|
locale={isLocaleReady ? i18n.language : "en"}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{date && (
|
2022-12-16 12:27:42 +00:00
|
|
|
<div className="relative flex w-full flex-col sm:pl-2">
|
2022-12-14 17:30:55 +00:00
|
|
|
<div className="mb-4 flex-grow space-y-4">
|
|
|
|
<p className="text-medium text-sm">{t("date_overrides_dialog_which_hours")}</p>
|
|
|
|
<div>
|
|
|
|
{datesUnavailable ? (
|
2023-01-12 16:57:43 +00:00
|
|
|
<p className="rounded border p-2 text-sm text-gray-500">{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>
|
|
|
|
<div className="flex flex-row-reverse">
|
2022-12-15 20:19:35 +00:00
|
|
|
<Button
|
|
|
|
className="ml-2"
|
|
|
|
color="primary"
|
|
|
|
type="submit"
|
|
|
|
disabled={!date}
|
|
|
|
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
|
|
|
|
|
|
|
<DialogContent enableOverflow={enableOverflow} size="md">
|
2022-12-14 17:30:55 +00:00
|
|
|
<DateOverrideForm
|
|
|
|
excludedDates={excludedDates}
|
|
|
|
{...passThroughProps}
|
|
|
|
onClose={() => setOpen(false)}
|
|
|
|
/>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DateOverrideInputDialog;
|