2021-08-08 15:13:31 +00:00
|
|
|
import { ChevronLeftIcon, ChevronRightIcon } from "@heroicons/react/solid";
|
2021-12-16 15:20:38 +00:00
|
|
|
import { EventType, PeriodType } from "@prisma/client";
|
2021-06-24 22:15:18 +00:00
|
|
|
import dayjs, { Dayjs } from "dayjs";
|
2021-10-18 21:07:06 +00:00
|
|
|
import dayjsBusinessTime from "dayjs-business-time";
|
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";
|
2021-12-16 15:20:38 +00:00
|
|
|
import { useEffect, useMemo, useState } from "react";
|
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";
|
2021-10-08 11:43:48 +00:00
|
|
|
import { useLocale } from "@lib/hooks/useLocale";
|
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-12-16 15:20:38 +00:00
|
|
|
function isOutOfBounds(
|
|
|
|
time: dayjs.ConfigType,
|
|
|
|
{
|
|
|
|
periodType,
|
|
|
|
periodDays,
|
|
|
|
periodCountCalendarDays,
|
|
|
|
periodStartDate,
|
|
|
|
periodEndDate,
|
|
|
|
}: Pick<
|
|
|
|
EventType,
|
|
|
|
"periodType" | "periodDays" | "periodCountCalendarDays" | "periodStartDate" | "periodEndDate"
|
|
|
|
>
|
|
|
|
) {
|
|
|
|
const date = dayjs(time);
|
|
|
|
|
|
|
|
switch (periodType) {
|
|
|
|
case PeriodType.ROLLING: {
|
|
|
|
const periodRollingEndDay = periodCountCalendarDays
|
|
|
|
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
dayjs().utcOffset(date.utcOffset()).add(periodDays!, "days").endOf("day")
|
|
|
|
: // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
dayjs().utcOffset(date.utcOffset()).addBusinessTime(periodDays!, "days").endOf("day");
|
|
|
|
return date.endOf("day").isAfter(periodRollingEndDay);
|
|
|
|
}
|
|
|
|
|
|
|
|
case PeriodType.RANGE: {
|
|
|
|
const periodRangeStartDay = dayjs(periodStartDate).utcOffset(date.utcOffset()).endOf("day");
|
|
|
|
const periodRangeEndDay = dayjs(periodEndDate).utcOffset(date.utcOffset()).endOf("day");
|
|
|
|
return date.endOf("day").isBefore(periodRangeStartDay) || date.endOf("day").isAfter(periodRangeEndDay);
|
|
|
|
}
|
|
|
|
|
|
|
|
case PeriodType.UNLIMITED:
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2021-07-13 16:10:22 +00:00
|
|
|
|
2022-01-04 10:06:05 +00:00
|
|
|
const [month, setMonth] = useState<string>("");
|
|
|
|
const [year, setYear] = useState<string>("");
|
|
|
|
const [isFirstMonth, setIsFirstMonth] = useState<boolean>(false);
|
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
}, [browsingDate, i18n.language]);
|
|
|
|
|
2021-12-16 15:20:38 +00:00
|
|
|
const days = useMemo(() => {
|
|
|
|
if (!browsingDate) {
|
|
|
|
return [];
|
|
|
|
}
|
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
|
|
|
|
2021-06-30 01:35:08 +00:00
|
|
|
const isDisabled = (day: number) => {
|
2021-12-16 15:20:38 +00:00
|
|
|
const date = browsingDate.startOf("day").date(day);
|
|
|
|
return (
|
|
|
|
isOutOfBounds(date, {
|
|
|
|
periodType,
|
|
|
|
periodStartDate,
|
|
|
|
periodEndDate,
|
|
|
|
periodCountCalendarDays,
|
|
|
|
periodDays,
|
|
|
|
}) ||
|
|
|
|
!getSlots({
|
|
|
|
inviteeDate: date,
|
|
|
|
frequency: eventLength,
|
|
|
|
minimumBookingNotice,
|
|
|
|
workingHours,
|
|
|
|
}).length
|
|
|
|
);
|
2021-06-30 01:35:08 +00:00
|
|
|
};
|
|
|
|
|
2021-12-16 15:20:38 +00:00
|
|
|
const daysInMonth = browsingDate.daysInMonth();
|
2021-06-30 01:35:08 +00:00
|
|
|
for (let i = 1; i <= daysInMonth; i++) {
|
2021-09-14 08:45:28 +00:00
|
|
|
days.push({ disabled: isDisabled(i), date: i });
|
2021-06-30 01:35:08 +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
|
2021-12-16 15:20:38 +00:00
|
|
|
}, [browsingDate]);
|
|
|
|
|
|
|
|
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-02-09 00:05:13 +00:00
|
|
|
? "w-full sm:w-1/2 sm:border-r sm:pl-4 sm:pr-6 sm:dark:border-gray-800 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-02-09 00:05:13 +00:00
|
|
|
<div className="mb-4 flex text-xl font-light text-gray-600">
|
2021-08-08 15:13:31 +00:00
|
|
|
<span className="w-1/2 text-gray-600 dark:text-white">
|
2022-01-04 10:06:05 +00:00
|
|
|
<strong className="text-gray-900 dark:text-white">{month}</strong>{" "}
|
|
|
|
<span className="text-gray-500">{year}</span>
|
2021-08-08 15:13:31 +00:00
|
|
|
</span>
|
2021-07-30 12:48:51 +00:00
|
|
|
<div className="w-1/2 text-right text-gray-600 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-02-01 22:17:37 +00:00
|
|
|
isFirstMonth && "text-gray-400 dark:text-gray-600"
|
|
|
|
)}
|
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-02-09 00:05:13 +00:00
|
|
|
<div className="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-02-09 00:05:13 +00:00
|
|
|
<div key={weekDay} className="my-4 text-xs uppercase tracking-widest text-gray-500">
|
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}
|
|
|
|
className={classNames(
|
2022-02-09 00:05:13 +00:00
|
|
|
"absolute top-0 left-0 right-0 bottom-0 mx-auto w-full rounded-sm text-center",
|
2022-02-09 22:32:31 +00:00
|
|
|
"hover:border-brand hover:border dark:hover:border-white",
|
2022-02-09 00:05:13 +00:00
|
|
|
day.disabled ? "cursor-default font-light text-gray-400 hover:border-0" : "font-medium",
|
2021-12-16 15:20:38 +00:00
|
|
|
date && date.isSame(browsingDate.date(day.date), "day")
|
2021-12-14 10:39:32 +00:00
|
|
|
? "bg-brand text-brandcontrast"
|
2021-09-14 08:45:28 +00:00
|
|
|
: !day.disabled
|
2021-12-14 10:39:32 +00:00
|
|
|
? " bg-gray-100 dark:bg-gray-600 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;
|