* fix manage test error * fix manage test error * update useBookerContext * wrap store provider around booker * replace useStore with useBookerStoreContext * replace useBookerStore with useBookerStoreContext * add initializer function for booker store provider * update props * fixup: pass more props * export StoreInitializeType type * replace useBookerStore with useBookerStoreContext * fixup: dont wrap BookerStoreProvider around booker directly * wrap BookerStoreProvider around booker web wrapper and fix local storage import * fix: wrap test components with BookerStoreProvider to fix failing unit tests - Create reusable test utility in test-utils.tsx with comprehensive mock store - Update Booker.test.tsx to use context-based testing approach - Fix DatePicker tests in both bookings and calendars packages - Simulate auto-advance behavior for month navigation tests - All 14 previously failing tests now pass Co-Authored-By: rajiv@cal.com <sahalrajiv6900@gmail.com> * add changesets * fix: complete migration from useBookerStore to useBookerStoreContext - Updated all remaining files to use useBookerStoreContext instead of useBookerStore - Fixed BookingFields.test.tsx by wrapping test component with BookerStoreProvider - Ensures all Booker components work within the new context-based store pattern - Resolves failing unit tests in CI by completing the store migration Co-Authored-By: rajiv@cal.com <sahalrajiv6900@gmail.com> * fix: add missing useBookerStoreContext import in BookerWebWrapper.tsx - Fixes TypeScript errors in CI where useBookerStoreContext was used but not imported - Completes the migration from useBookerStore to useBookerStoreContext pattern - All tests pass locally after this fix Co-Authored-By: rajiv@cal.com <sahalrajiv6900@gmail.com> * Revert "fix: add missing useBookerStoreContext import in BookerWebWrapper.tsx" This reverts commit 12947ca63f46c42e5aee74c78176ef230e2bf2cf. * Revert "fix: complete migration from useBookerStore to useBookerStoreContext" This reverts commit 87d837f2d30e4f9259d2be786049d21856f4eef0. * fixup: useBookerStoreContext and useInitializeBookerStoreContext for booker web wrapper * fixup: failing E2E tests * fixup: email embed breaking * fixup: troubleshooter crashing * replace useBookerstore with useBookerStoreContext --------- Co-authored-by: Rajiv Sahal <sahalrajiv-extc@atharvacoe.ac.in> Co-authored-by: Ryukemeister <sahalrajiv6900@gmail.com> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Benny Joo <sldisek783@gmail.com>
249 lines
9.4 KiB
TypeScript
249 lines
9.4 KiB
TypeScript
import { useCallback, useMemo, useRef } from "react";
|
|
|
|
import dayjs from "@calcom/dayjs";
|
|
import { AvailableTimes, AvailableTimesSkeleton } from "@calcom/features/bookings";
|
|
import { useBookerStoreContext } from "@calcom/features/bookings/Booker/BookerStoreProvider";
|
|
import type { IUseBookingLoadingStates } from "@calcom/features/bookings/Booker/components/hooks/useBookings";
|
|
import type { BookerEvent } from "@calcom/features/bookings/types";
|
|
import type { Slot } from "@calcom/features/schedules/lib/use-schedule/types";
|
|
import { useNonEmptyScheduleDays } from "@calcom/features/schedules/lib/use-schedule/useNonEmptyScheduleDays";
|
|
import { useSlotsForAvailableDates } from "@calcom/features/schedules/lib/use-schedule/useSlotsForDate";
|
|
import { PUBLIC_INVALIDATE_AVAILABLE_SLOTS_ON_BOOKING_FORM } from "@calcom/lib/constants";
|
|
import { localStorage } from "@calcom/lib/webstorage";
|
|
import { BookerLayouts } from "@calcom/prisma/zod-utils";
|
|
import classNames from "@calcom/ui/classNames";
|
|
|
|
import { AvailableTimesHeader } from "../../components/AvailableTimesHeader";
|
|
import type { useScheduleForEventReturnType } from "../utils/event";
|
|
import { getQueryParam } from "../utils/query-param";
|
|
|
|
type AvailableTimeSlotsProps = {
|
|
extraDays?: number;
|
|
limitHeight?: boolean;
|
|
schedule?: useScheduleForEventReturnType;
|
|
isLoading: boolean;
|
|
seatsPerTimeSlot?: number | null;
|
|
showAvailableSeatsCount?: boolean | null;
|
|
event: {
|
|
data?: Pick<BookerEvent, "length" | "bookingFields" | "price" | "currency" | "metadata"> | null;
|
|
};
|
|
customClassNames?: {
|
|
availableTimeSlotsContainer?: string;
|
|
availableTimeSlotsTitle?: string;
|
|
availableTimeSlotsHeaderContainer?: string;
|
|
availableTimeSlotsTimeFormatToggle?: string;
|
|
availableTimes?: string;
|
|
};
|
|
confirmStepClassNames?: {
|
|
confirmButton?: string;
|
|
};
|
|
loadingStates: IUseBookingLoadingStates;
|
|
isVerificationCodeSending: boolean;
|
|
renderConfirmNotVerifyEmailButtonCond: boolean;
|
|
onSubmit: (timeSlot?: string) => void;
|
|
skipConfirmStep: boolean;
|
|
shouldRenderCaptcha?: boolean;
|
|
watchedCfToken?: string;
|
|
/**
|
|
* This is the list of time slots that are unavailable to book
|
|
*/
|
|
unavailableTimeSlots: string[];
|
|
confirmButtonDisabled?: boolean;
|
|
onAvailableTimeSlotSelect: (time: string) => void;
|
|
};
|
|
|
|
/**
|
|
* Renders available time slots for a given date.
|
|
* It will extract the date from the booker store.
|
|
* Next to that you can also pass in the `extraDays` prop, this
|
|
* will also fetch the next `extraDays` days and show multiple days
|
|
* in columns next to each other.
|
|
*/
|
|
|
|
export const AvailableTimeSlots = ({
|
|
extraDays,
|
|
limitHeight,
|
|
showAvailableSeatsCount,
|
|
schedule,
|
|
isLoading,
|
|
customClassNames,
|
|
skipConfirmStep,
|
|
seatsPerTimeSlot,
|
|
onSubmit,
|
|
unavailableTimeSlots,
|
|
confirmButtonDisabled,
|
|
confirmStepClassNames,
|
|
onAvailableTimeSlotSelect,
|
|
...props
|
|
}: AvailableTimeSlotsProps) => {
|
|
const selectedDate = useBookerStoreContext((state) => state.selectedDate);
|
|
|
|
const setSeatedEventData = useBookerStoreContext((state) => state.setSeatedEventData);
|
|
const date = selectedDate || dayjs().format("YYYY-MM-DD");
|
|
const [layout] = useBookerStoreContext((state) => [state.layout]);
|
|
const isColumnView = layout === BookerLayouts.COLUMN_VIEW;
|
|
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
const { setTentativeSelectedTimeslots, tentativeSelectedTimeslots } = useBookerStoreContext((state) => ({
|
|
setTentativeSelectedTimeslots: state.setTentativeSelectedTimeslots,
|
|
tentativeSelectedTimeslots: state.tentativeSelectedTimeslots,
|
|
}));
|
|
|
|
const onTentativeTimeSelect = ({
|
|
time,
|
|
attendees: _attendees,
|
|
seatsPerTimeSlot: _seatsPerTimeSlot,
|
|
bookingUid: _bookingUid,
|
|
}: {
|
|
time: string;
|
|
attendees: number;
|
|
seatsPerTimeSlot?: number | null;
|
|
bookingUid?: string;
|
|
}) => {
|
|
// We don't intentionally invalidate schedule here because that could remove the slot itself that was clicked, causing a bad UX.
|
|
// We could start doing that after we fix this behaviour.
|
|
// schedule?.invalidate();
|
|
|
|
// Earlier we had multiple tentative slots, but now we can only have one tentative slot.
|
|
setTentativeSelectedTimeslots([time]);
|
|
};
|
|
|
|
const scheduleData = schedule?.data;
|
|
|
|
const nonEmptyScheduleDays = useNonEmptyScheduleDays(scheduleData?.slots);
|
|
const nonEmptyScheduleDaysFromSelectedDate = nonEmptyScheduleDays.filter(
|
|
(slot) => dayjs(selectedDate).diff(slot, "day") <= 0
|
|
);
|
|
|
|
// Creates an array of dates to fetch slots for.
|
|
// If `extraDays` is passed in, we will extend the array with the next `extraDays` days.
|
|
const dates = useMemo(() => {
|
|
if (!extraDays) return [date];
|
|
if (nonEmptyScheduleDaysFromSelectedDate.length > 0) {
|
|
return nonEmptyScheduleDaysFromSelectedDate.slice(0, extraDays);
|
|
}
|
|
return [];
|
|
}, [date, extraDays, nonEmptyScheduleDaysFromSelectedDate]);
|
|
|
|
const { slotsPerDay, toggleConfirmButton } = useSlotsForAvailableDates(dates, scheduleData?.slots);
|
|
|
|
const overlayCalendarToggled =
|
|
getQueryParam("overlayCalendar") === "true" || localStorage.getItem("overlayCalendarSwitchDefault");
|
|
|
|
const onTimeSelect = useCallback(
|
|
(time: string, attendees: number, seatsPerTimeSlot?: number | null, bookingUid?: string) => {
|
|
// Temporarily allow disabling it, till we are sure that it doesn't cause any significant load on the system
|
|
if (PUBLIC_INVALIDATE_AVAILABLE_SLOTS_ON_BOOKING_FORM) {
|
|
// Ensures that user has latest available slots when they are about to confirm the booking by filling up the details
|
|
schedule?.invalidate();
|
|
}
|
|
setTentativeSelectedTimeslots([]);
|
|
// note(Lauris): setting setSeatedEventData before setSelectedTimeslot so that in useSlots we have seated event data available
|
|
// and only then we invoke handleReserveSlot that is triggered by the changes in setSelectedTimeslot.
|
|
if (seatsPerTimeSlot) {
|
|
setSeatedEventData({
|
|
seatsPerTimeSlot,
|
|
attendees,
|
|
bookingUid,
|
|
showAvailableSeatsCount,
|
|
});
|
|
}
|
|
|
|
onAvailableTimeSlotSelect(time);
|
|
|
|
const isTimeSlotAvailable = !unavailableTimeSlots.includes(time);
|
|
if (skipConfirmStep && isTimeSlotAvailable) {
|
|
onSubmit(time);
|
|
}
|
|
return;
|
|
},
|
|
[
|
|
onSubmit,
|
|
setSeatedEventData,
|
|
skipConfirmStep,
|
|
showAvailableSeatsCount,
|
|
unavailableTimeSlots,
|
|
schedule,
|
|
setTentativeSelectedTimeslots,
|
|
onAvailableTimeSlotSelect,
|
|
]
|
|
);
|
|
|
|
const handleSlotClick = useCallback(
|
|
(selectedSlot: Slot, isOverlapping: boolean) => {
|
|
if ((overlayCalendarToggled && isOverlapping) || skipConfirmStep) {
|
|
toggleConfirmButton(selectedSlot);
|
|
} else {
|
|
onTimeSelect(
|
|
selectedSlot.time,
|
|
selectedSlot?.attendees || 0,
|
|
seatsPerTimeSlot,
|
|
selectedSlot.bookingUid
|
|
);
|
|
}
|
|
},
|
|
[overlayCalendarToggled, onTimeSelect, seatsPerTimeSlot, skipConfirmStep, toggleConfirmButton]
|
|
);
|
|
|
|
return (
|
|
<>
|
|
<div className={classNames(`flex`, `${customClassNames?.availableTimeSlotsContainer}`)}>
|
|
{isLoading ? (
|
|
<div className="mb-3 h-8" />
|
|
) : (
|
|
slotsPerDay.length > 0 &&
|
|
slotsPerDay.map((slots) => (
|
|
<AvailableTimesHeader
|
|
customClassNames={{
|
|
availableTimeSlotsHeaderContainer: customClassNames?.availableTimeSlotsHeaderContainer,
|
|
availableTimeSlotsTitle: customClassNames?.availableTimeSlotsTitle,
|
|
availableTimeSlotsTimeFormatToggle: customClassNames?.availableTimeSlotsTimeFormatToggle,
|
|
}}
|
|
key={slots.date}
|
|
date={dayjs(slots.date)}
|
|
showTimeFormatToggle={!isColumnView}
|
|
availableMonth={
|
|
dayjs(selectedDate).format("MM") !== dayjs(slots.date).format("MM")
|
|
? dayjs(slots.date).format("MMM")
|
|
: undefined
|
|
}
|
|
/>
|
|
))
|
|
)}
|
|
</div>
|
|
|
|
<div
|
|
ref={containerRef}
|
|
className={classNames(
|
|
limitHeight && "scroll-bar flex-grow overflow-auto md:h-[400px]",
|
|
!limitHeight && "flex h-full w-full flex-row gap-4",
|
|
`${customClassNames?.availableTimeSlotsContainer}`
|
|
)}>
|
|
{isLoading && // Shows exact amount of days as skeleton.
|
|
Array.from({ length: 1 + (extraDays ?? 0) }).map((_, i) => <AvailableTimesSkeleton key={i} />)}
|
|
{!isLoading &&
|
|
slotsPerDay.length > 0 &&
|
|
slotsPerDay.map((slots) => (
|
|
<div key={slots.date} className="scroll-bar h-full w-full overflow-y-auto overflow-x-hidden">
|
|
<AvailableTimes
|
|
className={customClassNames?.availableTimeSlotsContainer}
|
|
customClassNames={customClassNames?.availableTimes}
|
|
showTimeFormatToggle={!isColumnView}
|
|
onTimeSelect={onTimeSelect}
|
|
onTentativeTimeSelect={onTentativeTimeSelect}
|
|
unavailableTimeSlots={unavailableTimeSlots}
|
|
slots={slots.slots}
|
|
showAvailableSeatsCount={showAvailableSeatsCount}
|
|
skipConfirmStep={skipConfirmStep}
|
|
seatsPerTimeSlot={seatsPerTimeSlot}
|
|
handleSlotClick={handleSlotClick}
|
|
confirmButtonDisabled={confirmButtonDisabled}
|
|
confirmStepClassNames={confirmStepClassNames}
|
|
{...props}
|
|
/>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</>
|
|
);
|
|
};
|