Files
cal.diy/packages/features/bookings/Booker/components/hooks/useSlots.ts
T
313bb89789 refactor: Booker Component (#13176)
* refactor: booker component

* refactor: booker component

* refactor: useBookings, useOverlayCalendar

* fix: imports and types

* fix: don't allow uid update in bookings update api (#13071)

* don't allow uid update in bookings update api

* omit uid from schemaBookingEditBodyParams instead of updating schemaBookingBaseBodyParams

---------

Co-authored-by: Alex van Andel <[email protected]>
Co-authored-by: Peer Richelsen <[email protected]>

* New Crowdin translations by Github Action

* fix: use DateRangePicker component in my-account/out-of-office (#13207)

* fix: remove duplicate useBrandColors

* fix: add missing localstorage package

* fix: booking form initial values

* fix: redirect to payment form

* refactor: handle fetching schedule only in Booker

* fixup! refactor: handle fetching schedule only in Booker

* fix: remove empty file

* useBookerLayout.ts useEffect dependencies

isMobile and isEmbed were missing and validateLayout was redundant

* useInitialFormValues useEffect dependencies

* fix: remove unused hook

* refactor: split logic from data fetch overlayCalendar

* fix: small changes

* fix: inifinite loop

* fix: toggle calendar

---------

Co-authored-by: Rajiv Sahal <[email protected]>
Co-authored-by: Riddhesh Mahajan <[email protected]>
Co-authored-by: Alex van Andel <[email protected]>
Co-authored-by: Peer Richelsen <[email protected]>
Co-authored-by: Crowdin Bot <[email protected]>
Co-authored-by: Carina Wollendorfer <[email protected]>
Co-authored-by: Lauris Skraucis <[email protected]>
2024-01-22 11:57:00 +02:00

75 lines
2.3 KiB
TypeScript

import { useEffect } from "react";
import { shallow } from "zustand/shallow";
import dayjs from "@calcom/dayjs";
import { useBookerStore } from "@calcom/features/bookings/Booker/store";
import { useSlotReservationId } from "@calcom/features/bookings/Booker/useSlotReservationId";
import type { useEventReturnType } from "@calcom/features/bookings/Booker/utils/event";
import { MINUTES_TO_BOOK } from "@calcom/lib/constants";
import { trpc } from "@calcom/trpc";
export const useSlots = (event: useEventReturnType) => {
const selectedDuration = useBookerStore((state) => state.selectedDuration);
const [selectedTimeslot, setSelectedTimeslot] = useBookerStore(
(state) => [state.selectedTimeslot, state.setSelectedTimeslot],
shallow
);
const [slotReservationId, setSlotReservationId] = useSlotReservationId();
const reserveSlotMutation = trpc.viewer.public.slots.reserveSlot.useMutation({
trpc: {
context: {
skipBatch: true,
},
},
onSuccess: (data) => {
setSlotReservationId(data.uid);
},
});
const removeSelectedSlot = trpc.viewer.public.slots.removeSelectedSlotMark.useMutation({
trpc: { context: { skipBatch: true } },
});
const handleRemoveSlot = () => {
if (event?.data) {
removeSelectedSlot.mutate({ uid: slotReservationId });
}
};
const handleReserveSlot = () => {
if (event?.data?.id && selectedTimeslot && (selectedDuration || event?.data?.length)) {
reserveSlotMutation.mutate({
slotUtcStartDate: dayjs(selectedTimeslot).utc().format(),
eventTypeId: event.data.id,
slotUtcEndDate: dayjs(selectedTimeslot)
.utc()
.add(selectedDuration || event.data.length, "minutes")
.format(),
});
}
};
const timeslot = useBookerStore((state) => state.selectedTimeslot);
useEffect(() => {
handleReserveSlot();
const interval = setInterval(() => {
handleReserveSlot();
}, parseInt(MINUTES_TO_BOOK) * 60 * 1000 - 2000);
return () => {
handleRemoveSlot();
clearInterval(interval);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [event?.data?.id, timeslot]);
return {
selectedTimeslot,
setSelectedTimeslot,
setSlotReservationId,
slotReservationId,
handleReserveSlot,
handleRemoveSlot,
};
};