import { useState } from "react"; import { useForm } from "react-hook-form"; import type { Dayjs } from "@calcom/dayjs"; import dayjs from "@calcom/dayjs"; import { classNames } from "@calcom/lib"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import useMediaQuery from "@calcom/lib/hooks/useMediaQuery"; import type { WorkingHours } from "@calcom/types/schedule"; import { Dialog, DialogContent, DialogTrigger, DialogHeader, DialogClose, Switch, Form, Button, } from "@calcom/ui"; import DatePicker from "../../calendars/DatePicker"; import type { TimeRange } from "./Schedule"; import { DayRanges } from "./Schedule"; // 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(); const { t, i18n, isLocaleReady } = useLocale(); const [datesUnavailable, setDatesUnavailable] = useState( value && value[0].start.getUTCHours() === 0 && value[0].start.getUTCMinutes() === 0 && value[0].end.getUTCHours() === 0 && value[0].end.getUTCMinutes() === 0 ); const [date, setDate] = useState(value ? dayjs.utc(value[0].start) : null); const defaultRanges = (workingHours || []).reduce((dayRanges: TimeRange[], 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; }, []); // 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(), }); } 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() ), })) : defaultRanges, }, }); return (
{ if (!date) return; onChange( (datesUnavailable ? [ { start: date.utc(true).startOf("day").toDate(), end: date.utc(true).startOf("day").add(1, "day").toDate(), }, ] : values.range ).map((item) => ({ 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(), })) ); onClose(); }} className="p-6 sm:flex sm:p-0">
setDate(day)} onMonthChange={(newMonth) => { setBrowsingDate(newMonth); }} browsingDate={browsingDate} locale={isLocaleReady ? i18n.language : "en"} />
{date && (

{t("date_overrides_dialog_which_hours")}

{datesUnavailable ? (

{t("date_overrides_unavailable")}

) : ( )}
)}
); }; const DateOverrideInputDialog = ({ Trigger, excludedDates = [], ...passThroughProps }: { workingHours: WorkingHours[]; excludedDates?: string[]; Trigger: React.ReactNode; onChange: (newValue: TimeRange[]) => void; value?: TimeRange[]; }) => { const isMobile = useMediaQuery("(max-width: 768px)"); const [open, setOpen] = useState(false); { /* 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; return ( {Trigger} setOpen(false)} /> ); }; export default DateOverrideInputDialog;