2021-08-08 15:13:31 +00:00
|
|
|
import { ChevronLeftIcon, ChevronRightIcon } from "@heroicons/react/solid";
|
2022-05-18 21:05:49 +00:00
|
|
|
import { PeriodType } from "@prisma/client";
|
2021-06-24 22:15:18 +00:00
|
|
|
import dayjs, { Dayjs } from "dayjs";
|
2022-05-17 19:31:49 +00:00
|
|
|
import dayjsBusinessTime from "dayjs-business-days2";
|
2021-12-16 15:20:38 +00:00
|
|
|
import timezone from "dayjs/plugin/timezone";
|
2021-09-22 19:52:38 +00:00
|
|
|
import utc from "dayjs/plugin/utc";
|
2022-02-27 00:19:50 +00:00
|
|
|
import { memoize } from "lodash";
|
2022-03-31 08:45:47 +00:00
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
|
|
|
2022-05-27 15:37:02 +00:00
|
|
|
import { useEmbedStyles } from "@calcom/embed-core/embed-iframe";
|
2022-03-31 08:45:47 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
import classNames from "@lib/classNames";
|
2021-12-16 15:20:38 +00:00
|
|
|
import { timeZone } from "@lib/clock";
|
2021-12-20 11:55:49 +00:00
|
|
|
import { weekdayNames } from "@lib/core/i18n/weekday";
|
2022-02-27 00:19:50 +00:00
|
|
|
import { doWorkAsync } from "@lib/doWorkAsync";
|
2022-05-18 21:05:49 +00:00
|
|
|
import isOutOfBounds from "@lib/isOutOfBounds";
|
2021-09-22 19:52:38 +00:00
|
|
|
import getSlots from "@lib/slots";
|
2021-11-18 01:03:19 +00:00
|
|
|
import { WorkingHours } from "@lib/types/schedule";
|
2021-07-07 10:43:13 +00:00
|
|
|
|
2021-12-16 15:20:38 +00:00
|
|
|
import Loader from "@components/Loader";
|
|
|
|
|
2021-10-18 21:07:06 +00:00
|
|
|
dayjs.extend(dayjsBusinessTime);
|
2021-06-30 01:35:08 +00:00
|
|
|
dayjs.extend(utc);
|
2021-12-16 15:20:38 +00:00
|
|
|
dayjs.extend(timezone);
|
2021-06-21 20:26:04 +00:00
|
|
|
|
2021-11-18 01:03:19 +00:00
|
|
|
type DatePickerProps = {
|
|
|
|
weekStart: string;
|
|
|
|
onDatePicked: (pickedDate: Dayjs) => void;
|
|
|
|
workingHours: WorkingHours[];
|
|
|
|
eventLength: number;
|
|
|
|
date: Dayjs | null;
|
2021-12-16 15:20:38 +00:00
|
|
|
periodType: PeriodType;
|
2021-11-18 01:03:19 +00:00
|
|
|
periodStartDate: Date | null;
|
|
|
|
periodEndDate: Date | null;
|
|
|
|
periodDays: number | null;
|
|
|
|
periodCountCalendarDays: boolean | null;
|
|
|
|
minimumBookingNotice: number;
|
|
|
|
};
|
|
|
|
|
2021-10-18 21:07:06 +00:00
|
|
|
function DatePicker({
|
2021-06-30 15:41:38 +00:00
|
|
|
weekStart,
|
|
|
|
onDatePicked,
|
|
|
|
workingHours,
|
|
|
|
eventLength,
|
2021-07-13 16:10:22 +00:00
|
|
|
date,
|
2021-11-18 01:03:19 +00:00
|
|
|
periodType = PeriodType.UNLIMITED,
|
2021-07-15 14:10:26 +00:00
|
|
|
periodStartDate,
|
|
|
|
periodEndDate,
|
|
|
|
periodDays,
|
|
|
|
periodCountCalendarDays,
|
2021-07-22 22:52:27 +00:00
|
|
|
minimumBookingNotice,
|
2021-11-18 01:03:19 +00:00
|
|
|
}: DatePickerProps): JSX.Element {
|
2021-12-20 11:55:49 +00:00
|
|
|
const { i18n } = useLocale();
|
2021-12-16 15:20:38 +00:00
|
|
|
const [browsingDate, setBrowsingDate] = useState<Dayjs | null>(date);
|
2022-03-31 08:45:47 +00:00
|
|
|
const enabledDateButtonEmbedStyles = useEmbedStyles("enabledDateButton");
|
|
|
|
const disabledDateButtonEmbedStyles = useEmbedStyles("disabledDateButton");
|
2022-01-04 10:06:05 +00:00
|
|
|
const [month, setMonth] = useState<string>("");
|
|
|
|
const [year, setYear] = useState<string>("");
|
|
|
|
const [isFirstMonth, setIsFirstMonth] = useState<boolean>(false);
|
2022-02-27 00:19:50 +00:00
|
|
|
const [daysFromState, setDays] = useState<
|
|
|
|
| {
|
2022-05-17 20:43:27 +00:00
|
|
|
disabled: boolean;
|
2022-02-27 00:19:50 +00:00
|
|
|
date: number;
|
|
|
|
}[]
|
|
|
|
| null
|
|
|
|
>(null);
|
2021-09-14 08:45:28 +00:00
|
|
|
useEffect(() => {
|
2021-12-16 15:20:38 +00:00
|
|
|
if (!browsingDate || (date && browsingDate.utcOffset() !== date?.utcOffset())) {
|
|
|
|
setBrowsingDate(date || dayjs().tz(timeZone()));
|
2021-07-15 14:10:26 +00:00
|
|
|
}
|
2021-12-16 15:20:38 +00:00
|
|
|
}, [date, browsingDate]);
|
2021-06-21 20:26:04 +00:00
|
|
|
|
2022-01-04 10:06:05 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (browsingDate) {
|
|
|
|
setMonth(browsingDate.toDate().toLocaleString(i18n.language, { month: "long" }));
|
|
|
|
setYear(browsingDate.format("YYYY"));
|
|
|
|
setIsFirstMonth(browsingDate.startOf("month").isBefore(dayjs()));
|
2022-02-27 00:19:50 +00:00
|
|
|
setDays(null);
|
2022-01-04 10:06:05 +00:00
|
|
|
}
|
|
|
|
}, [browsingDate, i18n.language]);
|
|
|
|
|
2022-02-27 00:19:50 +00:00
|
|
|
const isDisabled = (
|
|
|
|
day: number,
|
|
|
|
{
|
|
|
|
browsingDate,
|
|
|
|
periodType,
|
|
|
|
periodStartDate,
|
|
|
|
periodEndDate,
|
|
|
|
periodCountCalendarDays,
|
|
|
|
periodDays,
|
|
|
|
eventLength,
|
|
|
|
minimumBookingNotice,
|
|
|
|
workingHours,
|
2022-04-04 20:26:14 +00:00
|
|
|
}: Omit<DatePickerProps, "weekStart" | "onDatePicked" | "date"> & {
|
|
|
|
browsingDate: Dayjs;
|
2022-02-27 00:19:50 +00:00
|
|
|
}
|
|
|
|
) => {
|
|
|
|
const date = browsingDate.startOf("day").date(day);
|
|
|
|
return (
|
|
|
|
isOutOfBounds(date, {
|
|
|
|
periodType,
|
|
|
|
periodStartDate,
|
|
|
|
periodEndDate,
|
|
|
|
periodCountCalendarDays,
|
|
|
|
periodDays,
|
|
|
|
}) ||
|
|
|
|
!getSlots({
|
|
|
|
inviteeDate: date,
|
|
|
|
frequency: eventLength,
|
|
|
|
minimumBookingNotice,
|
|
|
|
workingHours,
|
2022-03-12 06:52:27 +00:00
|
|
|
eventLength,
|
2022-02-27 00:19:50 +00:00
|
|
|
}).length
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const isDisabledRef = useRef(
|
|
|
|
memoize(isDisabled, (day, { browsingDate }) => {
|
|
|
|
// Make a composite cache key
|
|
|
|
return day + "_" + browsingDate.toString();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const days = (() => {
|
2021-12-16 15:20:38 +00:00
|
|
|
if (!browsingDate) {
|
|
|
|
return [];
|
|
|
|
}
|
2022-02-27 00:19:50 +00:00
|
|
|
if (daysFromState) {
|
|
|
|
return daysFromState;
|
|
|
|
}
|
2021-09-14 08:45:28 +00:00
|
|
|
// Create placeholder elements for empty days in first week
|
2021-12-29 08:27:49 +00:00
|
|
|
let weekdayOfFirst = browsingDate.date(1).day();
|
2021-09-14 08:45:28 +00:00
|
|
|
if (weekStart === "Monday") {
|
|
|
|
weekdayOfFirst -= 1;
|
|
|
|
if (weekdayOfFirst < 0) weekdayOfFirst = 6;
|
2021-06-30 01:35:08 +00:00
|
|
|
}
|
2021-06-21 20:26:04 +00:00
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
const days = Array(weekdayOfFirst).fill(null);
|
2021-06-24 22:15:18 +00:00
|
|
|
|
2022-02-27 00:19:50 +00:00
|
|
|
const isDisabledMemoized = isDisabledRef.current;
|
2021-06-30 01:35:08 +00:00
|
|
|
|
2021-12-16 15:20:38 +00:00
|
|
|
const daysInMonth = browsingDate.daysInMonth();
|
2022-02-27 00:19:50 +00:00
|
|
|
const daysInitialOffset = days.length;
|
|
|
|
|
|
|
|
// Build UI with All dates disabled
|
2021-06-30 01:35:08 +00:00
|
|
|
for (let i = 1; i <= daysInMonth; i++) {
|
2022-02-27 00:19:50 +00:00
|
|
|
days.push({
|
|
|
|
disabled: true,
|
|
|
|
date: i,
|
|
|
|
});
|
2021-06-30 01:35:08 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 00:19:50 +00:00
|
|
|
// Update dates with their availability
|
|
|
|
doWorkAsync({
|
2022-02-28 09:16:43 +00:00
|
|
|
batch: 1,
|
2022-02-27 00:19:50 +00:00
|
|
|
name: "DatePicker",
|
|
|
|
length: daysInMonth,
|
2022-04-04 20:26:14 +00:00
|
|
|
callback: (i: number) => {
|
2022-05-17 20:43:27 +00:00
|
|
|
const day = i + 1;
|
2022-02-27 00:19:50 +00:00
|
|
|
days[daysInitialOffset + i] = {
|
|
|
|
disabled: isDisabledMemoized(day, {
|
|
|
|
browsingDate,
|
|
|
|
periodType,
|
|
|
|
periodStartDate,
|
|
|
|
periodEndDate,
|
|
|
|
periodCountCalendarDays,
|
|
|
|
periodDays,
|
|
|
|
eventLength,
|
|
|
|
minimumBookingNotice,
|
|
|
|
workingHours,
|
|
|
|
}),
|
|
|
|
date: day,
|
|
|
|
};
|
|
|
|
},
|
2022-02-28 09:16:43 +00:00
|
|
|
batchDone: () => {
|
|
|
|
setDays([...days]);
|
2022-02-27 00:19:50 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-12-16 15:20:38 +00:00
|
|
|
return days;
|
2021-09-14 08:45:28 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2022-02-27 00:19:50 +00:00
|
|
|
})();
|
2021-12-16 15:20:38 +00:00
|
|
|
|
|
|
|
if (!browsingDate) {
|
|
|
|
return <Loader />;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle month changes
|
|
|
|
const incrementMonth = () => {
|
|
|
|
setBrowsingDate(browsingDate?.add(1, "month"));
|
|
|
|
};
|
|
|
|
|
|
|
|
const decrementMonth = () => {
|
|
|
|
setBrowsingDate(browsingDate?.subtract(1, "month"));
|
|
|
|
};
|
2021-06-21 20:26:04 +00:00
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
return (
|
2021-08-08 15:13:31 +00:00
|
|
|
<div
|
|
|
|
className={
|
2021-08-12 17:05:46 +00:00
|
|
|
"mt-8 sm:mt-0 sm:min-w-[455px] " +
|
2021-09-14 08:45:28 +00:00
|
|
|
(date
|
2022-03-24 13:15:24 +00:00
|
|
|
? "w-full sm:w-1/2 sm:border-r sm:pl-4 sm:pr-6 sm:dark:border-gray-700 md:w-1/3 "
|
2021-08-14 12:26:33 +00:00
|
|
|
: "w-full sm:pl-4")
|
2021-08-08 15:13:31 +00:00
|
|
|
}>
|
2022-04-08 05:33:24 +00:00
|
|
|
<div className="mb-4 flex text-xl font-light">
|
|
|
|
<span className="w-1/2 dark:text-white">
|
|
|
|
<strong className="text-bookingdarker dark:text-white">{month}</strong>{" "}
|
|
|
|
<span className="text-bookinglight">{year}</span>
|
2021-08-08 15:13:31 +00:00
|
|
|
</span>
|
2022-04-08 05:33:24 +00:00
|
|
|
<div className="w-1/2 text-right dark:text-gray-400">
|
2021-06-21 20:26:04 +00:00
|
|
|
<button
|
|
|
|
onClick={decrementMonth}
|
2022-02-01 22:17:37 +00:00
|
|
|
className={classNames(
|
2022-02-09 00:05:13 +00:00
|
|
|
"group p-1 ltr:mr-2 rtl:ml-2",
|
2022-04-08 05:33:24 +00:00
|
|
|
isFirstMonth && "text-bookinglighter dark:text-gray-600"
|
2022-02-01 22:17:37 +00:00
|
|
|
)}
|
2022-01-04 10:06:05 +00:00
|
|
|
disabled={isFirstMonth}
|
2021-11-15 15:03:04 +00:00
|
|
|
data-testid="decrementMonth">
|
2022-02-09 00:05:13 +00:00
|
|
|
<ChevronLeftIcon className="h-5 w-5 group-hover:text-black dark:group-hover:text-white" />
|
2021-06-21 20:26:04 +00:00
|
|
|
</button>
|
2022-02-09 00:05:13 +00:00
|
|
|
<button className="group p-1" onClick={incrementMonth} data-testid="incrementMonth">
|
|
|
|
<ChevronRightIcon className="h-5 w-5 group-hover:text-black dark:group-hover:text-white" />
|
2021-06-21 20:26:04 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-04-08 05:33:24 +00:00
|
|
|
<div className="border-bookinglightest grid grid-cols-7 gap-4 border-t border-b text-center dark:border-gray-800 sm:border-0">
|
2021-12-20 11:55:49 +00:00
|
|
|
{weekdayNames(i18n.language, weekStart === "Sunday" ? 0 : 1, "short").map((weekDay) => (
|
2022-04-08 05:33:24 +00:00
|
|
|
<div key={weekDay} className="text-bookinglight my-4 text-xs uppercase tracking-widest">
|
2021-12-20 11:55:49 +00:00
|
|
|
{weekDay}
|
|
|
|
</div>
|
|
|
|
))}
|
2021-06-21 20:26:04 +00:00
|
|
|
</div>
|
2021-09-14 08:45:28 +00:00
|
|
|
<div className="grid grid-cols-7 gap-2 text-center">
|
|
|
|
{days.map((day, idx) => (
|
|
|
|
<div
|
|
|
|
key={day === null ? `e-${idx}` : `day-${day.date}`}
|
|
|
|
style={{
|
|
|
|
paddingTop: "100%",
|
|
|
|
}}
|
2021-11-18 01:03:19 +00:00
|
|
|
className="relative w-full">
|
2021-09-14 08:45:28 +00:00
|
|
|
{day === null ? (
|
|
|
|
<div key={`e-${idx}`} />
|
|
|
|
) : (
|
|
|
|
<button
|
2021-12-16 15:20:38 +00:00
|
|
|
onClick={() => onDatePicked(browsingDate.date(day.date))}
|
2021-09-14 08:45:28 +00:00
|
|
|
disabled={day.disabled}
|
2022-03-31 08:45:47 +00:00
|
|
|
style={
|
|
|
|
day.disabled ? { ...disabledDateButtonEmbedStyles } : { ...enabledDateButtonEmbedStyles }
|
|
|
|
}
|
2021-09-14 08:45:28 +00:00
|
|
|
className={classNames(
|
2022-06-15 20:54:31 +00:00
|
|
|
"hover:border-brand disabled:text-bookinglighter absolute top-0 left-0 right-0 bottom-0 mx-auto w-full rounded-sm text-center font-medium hover:border disabled:cursor-default disabled:font-light disabled:hover:border-0 dark:hover:border-white",
|
2021-12-16 15:20:38 +00:00
|
|
|
date && date.isSame(browsingDate.date(day.date), "day")
|
2022-06-15 20:54:31 +00:00
|
|
|
? "bg-brand text-brandcontrast dark:bg-darkmodebrand dark:text-darkmodebrandcontrast disabled:bg-gray-100 disabled:dark:bg-gray-600 disabled:dark:text-white"
|
2021-09-14 08:45:28 +00:00
|
|
|
: ""
|
2021-10-18 21:07:06 +00:00
|
|
|
)}
|
|
|
|
data-testid="day"
|
|
|
|
data-disabled={day.disabled}>
|
2021-09-14 08:45:28 +00:00
|
|
|
{day.date}
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
2021-06-21 20:26:04 +00:00
|
|
|
</div>
|
2021-09-14 08:45:28 +00:00
|
|
|
);
|
2021-10-18 21:07:06 +00:00
|
|
|
}
|
2021-06-21 20:26:04 +00:00
|
|
|
|
2021-06-24 22:15:18 +00:00
|
|
|
export default DatePicker;
|