Files
calendar/packages/features/schedules/lib/use-schedule/useSlotsForDate.ts
T
829bb63eb9 fix: skip confirm step followup (#19076)
* Fix: Resolved double-click issue on timeSlot in Booker Atom

* fix: show toast on booking error

* fix: type error

* Prevent the automatic change of bookerState when all fields are filled.

* fix: when a user clicks on a new timeslot, any previously displayed "Confirm" button should be hidden

* Update useSkipConfirmStep.ts

* fix: remove 2 states for slots

* Update useSlotsForDate.ts

* fix types

* use common types

* fix: show VerifyCodeDialog and RedirectToInstantMeetingModal when skiping confirm step

* fix for instant bookings

---------

Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com>
2025-02-21 19:07:54 +05:30

53 lines
1.5 KiB
TypeScript

import { useCallback, useEffect, useMemo, useState } from "react";
import type { Slots, Slot } from "./types";
/**
* Gets slots for a specific date from the schedule cache.
* @param date Format YYYY-MM-DD
* @param scheduleCache Instance of useScheduleWithCache
*/
export const useSlotsForDate = (date: string | null, slots?: Slots) => {
const slotsForDate = useMemo(() => {
if (!date || typeof slots === "undefined") return [];
return slots[date] || [];
}, [date, slots]);
return slotsForDate;
};
export const useSlotsForAvailableDates = (dates: (string | null)[], slots?: Slots) => {
const [slotsPerDay, setSlotsPerDay] = useState<{ date: string | null; slots: Slots[string] }[]>([]);
const toggleConfirmButton = useCallback((selectedSlot: Slot) => {
setSlotsPerDay((prevSlotsPerDay) =>
prevSlotsPerDay.map(({ date, slots }) => ({
date,
slots: slots.map((slot) => ({
...slot,
showConfirmButton: slot.time === selectedSlot.time ? !selectedSlot?.showConfirmButton : false,
})),
}))
);
}, []);
useEffect(() => {
if (slots === undefined) {
setSlotsPerDay([]);
return;
}
const updatedSlots = dates
.filter((date) => date !== null)
.map((date) => ({
slots: slots[`${date}`] || [],
date,
}));
setSlotsPerDay(updatedSlots);
}, [JSON.stringify(dates), JSON.stringify(slots)]);
return { slotsPerDay, setSlotsPerDay, toggleConfirmButton } as const;
};