2023-07-24 10:09:03 +00:00
|
|
|
import { shallow } from "zustand/shallow";
|
|
|
|
|
2023-02-21 22:25:43 +00:00
|
|
|
import type { Dayjs } from "@calcom/dayjs";
|
|
|
|
import dayjs from "@calcom/dayjs";
|
2022-08-31 09:47:23 +00:00
|
|
|
import { useEmbedStyles } from "@calcom/embed-core/embed-iframe";
|
2023-07-24 10:09:03 +00:00
|
|
|
import { useBookerStore } from "@calcom/features/bookings/Booker/store";
|
2022-07-23 00:39:50 +00:00
|
|
|
import classNames from "@calcom/lib/classNames";
|
|
|
|
import { daysInMonth, yyyymmdd } from "@calcom/lib/date-fns";
|
2022-11-09 19:14:01 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-07-23 00:39:50 +00:00
|
|
|
import { weekdayNames } from "@calcom/lib/weekday";
|
2023-01-23 23:08:01 +00:00
|
|
|
import { Button, SkeletonText } from "@calcom/ui";
|
2023-05-17 12:47:44 +00:00
|
|
|
import { ChevronLeft, ChevronRight } from "@calcom/ui/components/icon";
|
2023-04-12 15:26:31 +00:00
|
|
|
import { ArrowRight } from "@calcom/ui/components/icon";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
|
|
|
export type DatePickerProps = {
|
|
|
|
/** which day of the week to render the calendar. Usually Sunday (=0) or Monday (=1) - default: Sunday */
|
|
|
|
weekStart?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
|
|
/** Fires whenever a selected date is changed. */
|
|
|
|
onChange: (date: Dayjs) => void;
|
|
|
|
/** Fires when the month is changed. */
|
|
|
|
onMonthChange?: (date: Dayjs) => void;
|
2023-08-12 07:22:44 +00:00
|
|
|
/** which date or dates are currently selected (not tracked from here) */
|
|
|
|
selected?: Dayjs | Dayjs[] | null;
|
2022-07-23 00:39:50 +00:00
|
|
|
/** defaults to current date. */
|
|
|
|
minDate?: Dayjs;
|
|
|
|
/** Furthest date selectable in the future, default = UNLIMITED */
|
|
|
|
maxDate?: Dayjs;
|
|
|
|
/** locale, any IETF language tag, e.g. "hu-HU" - defaults to Browser settings */
|
|
|
|
locale: string;
|
|
|
|
/** Defaults to [], which dates are not bookable. Array of valid dates like: ["2022-04-23", "2022-04-24"] */
|
|
|
|
excludedDates?: string[];
|
|
|
|
/** defaults to all, which dates are bookable (inverse of excludedDates) */
|
|
|
|
includedDates?: string[];
|
|
|
|
/** allows adding classes to the container */
|
|
|
|
className?: string;
|
|
|
|
/** Shows a small loading spinner next to the month name */
|
|
|
|
isLoading?: boolean;
|
2023-07-24 10:09:03 +00:00
|
|
|
/** used to query the multiple selected dates */
|
|
|
|
eventSlug?: string;
|
2022-07-23 00:39:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const Day = ({
|
|
|
|
date,
|
|
|
|
active,
|
2022-12-14 17:30:55 +00:00
|
|
|
disabled,
|
2022-07-23 00:39:50 +00:00
|
|
|
...props
|
2022-12-14 17:30:55 +00:00
|
|
|
}: JSX.IntrinsicElements["button"] & {
|
|
|
|
active: boolean;
|
|
|
|
date: Dayjs;
|
|
|
|
}) => {
|
2023-05-04 10:53:50 +00:00
|
|
|
const { t } = useLocale();
|
2022-08-31 09:47:23 +00:00
|
|
|
const enabledDateButtonEmbedStyles = useEmbedStyles("enabledDateButton");
|
|
|
|
const disabledDateButtonEmbedStyles = useEmbedStyles("disabledDateButton");
|
2022-07-23 00:39:50 +00:00
|
|
|
return (
|
|
|
|
<button
|
2022-12-14 17:30:55 +00:00
|
|
|
type="button"
|
|
|
|
style={disabled ? { ...disabledDateButtonEmbedStyles } : { ...enabledDateButtonEmbedStyles }}
|
2022-07-23 00:39:50 +00:00
|
|
|
className={classNames(
|
2023-06-22 22:25:37 +00:00
|
|
|
"disabled:text-bookinglighter absolute bottom-0 left-0 right-0 top-0 mx-auto w-full rounded-md border-2 border-transparent text-center text-sm font-medium disabled:cursor-default disabled:border-transparent disabled:font-light ",
|
2022-07-23 00:39:50 +00:00
|
|
|
active
|
2023-04-05 18:14:46 +00:00
|
|
|
? "bg-brand-default text-brand"
|
2022-12-14 17:30:55 +00:00
|
|
|
: !disabled
|
2023-04-05 18:14:46 +00:00
|
|
|
? " hover:border-brand-default text-emphasis bg-emphasis"
|
|
|
|
: "text-muted"
|
2022-07-23 00:39:50 +00:00
|
|
|
)}
|
|
|
|
data-testid="day"
|
2022-12-14 17:30:55 +00:00
|
|
|
data-disabled={disabled}
|
|
|
|
disabled={disabled}
|
2022-07-23 00:39:50 +00:00
|
|
|
{...props}>
|
|
|
|
{date.date()}
|
2022-10-14 18:04:54 +00:00
|
|
|
{date.isToday() && (
|
2023-06-22 22:25:37 +00:00
|
|
|
<span className="absolute left-1/2 top-1/2 flex h-[5px] w-[5px] -translate-x-1/2 translate-y-[8px] items-center justify-center rounded-full bg-white align-middle sm:translate-y-[12px]">
|
2023-05-04 10:53:50 +00:00
|
|
|
<span className="sr-only">{t("today")}</span>
|
|
|
|
</span>
|
2022-10-14 18:04:54 +00:00
|
|
|
)}
|
2022-07-23 00:39:50 +00:00
|
|
|
</button>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-12-14 22:43:21 +00:00
|
|
|
const NoAvailabilityOverlay = ({
|
|
|
|
month,
|
|
|
|
nextMonthButton,
|
|
|
|
}: {
|
|
|
|
month: string | null;
|
|
|
|
nextMonthButton: () => void;
|
|
|
|
}) => {
|
|
|
|
const { t } = useLocale();
|
|
|
|
|
|
|
|
return (
|
2023-06-22 22:25:37 +00:00
|
|
|
<div className=" bg-muted border-subtle absolute left-1/2 top-40 -mt-10 w-max -translate-x-1/2 -translate-y-1/2 transform rounded-md border p-8 shadow-sm">
|
2023-04-05 18:14:46 +00:00
|
|
|
<h4 className="text-emphasis mb-4 font-medium">{t("no_availability_in_month", { month: month })}</h4>
|
2023-04-12 15:26:31 +00:00
|
|
|
<Button onClick={nextMonthButton} color="primary" EndIcon={ArrowRight}>
|
2022-12-14 22:43:21 +00:00
|
|
|
{t("view_next_month")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-07-23 00:39:50 +00:00
|
|
|
const Days = ({
|
2022-12-14 17:30:55 +00:00
|
|
|
minDate = dayjs.utc(),
|
2022-07-23 00:39:50 +00:00
|
|
|
excludedDates = [],
|
|
|
|
browsingDate,
|
|
|
|
weekStart,
|
|
|
|
DayComponent = Day,
|
|
|
|
selected,
|
2022-12-14 22:43:21 +00:00
|
|
|
month,
|
|
|
|
nextMonthButton,
|
2023-07-24 10:09:03 +00:00
|
|
|
eventSlug,
|
2022-07-23 00:39:50 +00:00
|
|
|
...props
|
|
|
|
}: Omit<DatePickerProps, "locale" | "className" | "weekStart"> & {
|
|
|
|
DayComponent?: React.FC<React.ComponentProps<typeof Day>>;
|
|
|
|
browsingDate: Dayjs;
|
|
|
|
weekStart: number;
|
2022-12-14 22:43:21 +00:00
|
|
|
month: string | null;
|
|
|
|
nextMonthButton: () => void;
|
2022-07-23 00:39:50 +00:00
|
|
|
}) => {
|
|
|
|
// Create placeholder elements for empty days in first week
|
2023-06-16 16:08:58 +00:00
|
|
|
const weekdayOfFirst = browsingDate.date(1).day();
|
2022-12-14 17:30:55 +00:00
|
|
|
const currentDate = minDate.utcOffset(browsingDate.utcOffset());
|
|
|
|
const availableDates = (includedDates: string[] | undefined) => {
|
|
|
|
const dates = [];
|
|
|
|
const lastDateOfMonth = browsingDate.date(daysInMonth(browsingDate));
|
|
|
|
for (
|
|
|
|
let date = currentDate;
|
|
|
|
date.isBefore(lastDateOfMonth) || date.isSame(lastDateOfMonth, "day");
|
|
|
|
date = date.add(1, "day")
|
|
|
|
) {
|
|
|
|
// even if availableDates is given, filter out the passed included dates
|
|
|
|
if (includedDates && !includedDates.includes(yyyymmdd(date))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dates.push(yyyymmdd(date));
|
|
|
|
}
|
|
|
|
return dates;
|
|
|
|
};
|
|
|
|
|
|
|
|
const includedDates = currentDate.isSame(browsingDate, "month")
|
|
|
|
? availableDates(props.includedDates)
|
|
|
|
: props.includedDates;
|
2022-07-23 00:39:50 +00:00
|
|
|
|
|
|
|
const days: (Dayjs | null)[] = Array((weekdayOfFirst - weekStart + 7) % 7).fill(null);
|
|
|
|
for (let day = 1, dayCount = daysInMonth(browsingDate); day <= dayCount; day++) {
|
|
|
|
const date = browsingDate.set("date", day);
|
|
|
|
days.push(date);
|
|
|
|
}
|
2023-01-13 12:10:02 +00:00
|
|
|
|
2023-07-24 10:09:03 +00:00
|
|
|
const [selectedDatesAndTimes] = useBookerStore((state) => [state.selectedDatesAndTimes], shallow);
|
|
|
|
|
|
|
|
const isActive = (day: dayjs.Dayjs) => {
|
2023-08-12 07:22:44 +00:00
|
|
|
// for selecting a range of dates
|
|
|
|
if (Array.isArray(selected)) {
|
|
|
|
return Array.isArray(selected) && selected?.some((e) => yyyymmdd(e) === yyyymmdd(day));
|
|
|
|
}
|
|
|
|
|
2023-07-24 10:09:03 +00:00
|
|
|
if (selected && yyyymmdd(selected) === yyyymmdd(day)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-12 07:22:44 +00:00
|
|
|
// for selecting multiple dates for an event
|
2023-07-24 10:09:03 +00:00
|
|
|
if (
|
|
|
|
eventSlug &&
|
|
|
|
selectedDatesAndTimes &&
|
|
|
|
selectedDatesAndTimes[eventSlug as string] &&
|
|
|
|
Object.keys(selectedDatesAndTimes[eventSlug as string]).length > 0
|
|
|
|
) {
|
|
|
|
return Object.keys(selectedDatesAndTimes[eventSlug as string]).some((date) => {
|
|
|
|
return yyyymmdd(dayjs(date)) === yyyymmdd(day);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2022-07-23 00:39:50 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{days.map((day, idx) => (
|
|
|
|
<div key={day === null ? `e-${idx}` : `day-${day.format()}`} className="relative w-full pt-[100%]">
|
|
|
|
{day === null ? (
|
|
|
|
<div key={`e-${idx}`} />
|
|
|
|
) : props.isLoading ? (
|
|
|
|
<button
|
2023-06-22 22:25:37 +00:00
|
|
|
className="bg-muted text-muted absolute bottom-0 left-0 right-0 top-0 mx-auto flex w-full items-center justify-center rounded-sm border-transparent text-center font-medium opacity-50"
|
2022-07-23 00:39:50 +00:00
|
|
|
key={`e-${idx}`}
|
|
|
|
disabled>
|
2022-09-09 08:22:39 +00:00
|
|
|
<SkeletonText className="h-4 w-5" />
|
2022-07-23 00:39:50 +00:00
|
|
|
</button>
|
|
|
|
) : (
|
|
|
|
<DayComponent
|
|
|
|
date={day}
|
|
|
|
onClick={() => {
|
|
|
|
props.onChange(day);
|
|
|
|
}}
|
|
|
|
disabled={
|
|
|
|
(includedDates && !includedDates.includes(yyyymmdd(day))) ||
|
|
|
|
excludedDates.includes(yyyymmdd(day))
|
|
|
|
}
|
2023-07-24 10:09:03 +00:00
|
|
|
active={isActive(day)}
|
2022-07-23 00:39:50 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
))}
|
2022-12-14 22:43:21 +00:00
|
|
|
|
|
|
|
{!props.isLoading && includedDates && includedDates?.length === 0 && (
|
|
|
|
<NoAvailabilityOverlay month={month} nextMonthButton={nextMonthButton} />
|
|
|
|
)}
|
2022-07-23 00:39:50 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const DatePicker = ({
|
|
|
|
weekStart = 0,
|
|
|
|
className,
|
|
|
|
locale,
|
|
|
|
selected,
|
|
|
|
onMonthChange,
|
|
|
|
...passThroughProps
|
|
|
|
}: DatePickerProps & Partial<React.ComponentProps<typeof Days>>) => {
|
|
|
|
const browsingDate = passThroughProps.browsingDate || dayjs().startOf("month");
|
2022-11-09 19:14:01 +00:00
|
|
|
const { i18n } = useLocale();
|
2022-07-23 00:39:50 +00:00
|
|
|
|
|
|
|
const changeMonth = (newMonth: number) => {
|
|
|
|
if (onMonthChange) {
|
|
|
|
onMonthChange(browsingDate.add(newMonth, "month"));
|
|
|
|
}
|
|
|
|
};
|
2022-11-09 19:14:01 +00:00
|
|
|
const month = browsingDate
|
2022-11-22 13:09:23 +00:00
|
|
|
? new Intl.DateTimeFormat(i18n.language, { month: "long" }).format(
|
|
|
|
new Date(browsingDate.year(), browsingDate.month())
|
|
|
|
)
|
2022-11-09 19:14:01 +00:00
|
|
|
: null;
|
|
|
|
|
2022-07-23 00:39:50 +00:00
|
|
|
return (
|
|
|
|
<div className={className}>
|
2023-04-25 14:28:32 +00:00
|
|
|
<div className="mb-1 flex items-center justify-between text-xl">
|
2023-04-05 18:14:46 +00:00
|
|
|
<span className="text-default w-1/2 text-base">
|
2022-07-23 00:39:50 +00:00
|
|
|
{browsingDate ? (
|
|
|
|
<>
|
2023-04-05 18:14:46 +00:00
|
|
|
<strong className="text-emphasis font-semibold">{month}</strong>{" "}
|
2023-04-25 14:28:32 +00:00
|
|
|
<span className="text-subtle font-medium">{browsingDate.format("YYYY")}</span>
|
2022-07-23 00:39:50 +00:00
|
|
|
</>
|
|
|
|
) : (
|
2022-09-09 08:22:39 +00:00
|
|
|
<SkeletonText className="h-8 w-24" />
|
2022-07-23 00:39:50 +00:00
|
|
|
)}
|
|
|
|
</span>
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="text-emphasis">
|
2023-03-10 13:02:48 +00:00
|
|
|
<div className="flex">
|
|
|
|
<Button
|
|
|
|
className={classNames(
|
2023-08-11 23:39:25 +00:00
|
|
|
"group p-1 opacity-70 hover:opacity-100 rtl:rotate-180",
|
2023-03-10 13:02:48 +00:00
|
|
|
!browsingDate.isAfter(dayjs()) &&
|
2023-04-05 18:14:46 +00:00
|
|
|
"disabled:text-bookinglighter hover:bg-background hover:opacity-70"
|
2023-03-10 13:02:48 +00:00
|
|
|
)}
|
|
|
|
onClick={() => changeMonth(-1)}
|
|
|
|
disabled={!browsingDate.isAfter(dayjs())}
|
|
|
|
data-testid="decrementMonth"
|
|
|
|
color="minimal"
|
|
|
|
variant="icon"
|
2023-05-17 12:47:44 +00:00
|
|
|
StartIcon={ChevronLeft}
|
2023-03-10 13:02:48 +00:00
|
|
|
/>
|
|
|
|
<Button
|
2023-08-11 23:39:25 +00:00
|
|
|
className="group p-1 opacity-70 hover:opacity-100 rtl:rotate-180"
|
2023-03-10 13:02:48 +00:00
|
|
|
onClick={() => changeMonth(+1)}
|
|
|
|
data-testid="incrementMonth"
|
|
|
|
color="minimal"
|
|
|
|
variant="icon"
|
2023-05-17 12:47:44 +00:00
|
|
|
StartIcon={ChevronRight}
|
2023-03-10 13:02:48 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2022-07-23 00:39:50 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-06-22 22:25:37 +00:00
|
|
|
<div className="border-subtle mb-2 grid grid-cols-7 gap-4 border-b border-t text-center md:mb-0 md:border-0">
|
2022-07-23 00:39:50 +00:00
|
|
|
{weekdayNames(locale, weekStart, "short").map((weekDay) => (
|
2023-04-25 14:28:32 +00:00
|
|
|
<div key={weekDay} className="text-emphasis my-4 text-xs font-medium uppercase tracking-widest">
|
2022-07-23 00:39:50 +00:00
|
|
|
{weekDay}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
2022-12-14 22:43:21 +00:00
|
|
|
<div className="relative grid grid-cols-7 gap-1 text-center">
|
|
|
|
<Days
|
|
|
|
weekStart={weekStart}
|
|
|
|
selected={selected}
|
|
|
|
{...passThroughProps}
|
|
|
|
browsingDate={browsingDate}
|
|
|
|
month={month}
|
|
|
|
nextMonthButton={() => changeMonth(+1)}
|
|
|
|
/>
|
2022-07-23 00:39:50 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DatePicker;
|