Files
cal.diy/packages/features/bookings/Booker/components/hooks/useOverlayCalendar.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

89 lines
3.1 KiB
TypeScript

import { useEffect, useState } from "react";
import { shallow } from "zustand/shallow";
import dayjs from "@calcom/dayjs";
import { useTimePreferences } from "@calcom/features/bookings/lib";
import { useOverlayCalendarStore } from "../OverlayCalendar/store";
import type { UseCalendarsReturnType } from "./useCalendars";
import { useLocalSet } from "./useLocalSet";
export const useOverlayCalendar = ({
connectedCalendars,
overlayBusyDates,
onToggleCalendar,
}: Pick<UseCalendarsReturnType, "connectedCalendars" | "overlayBusyDates" | "onToggleCalendar">) => {
const { set, toggleValue, hasItem } = useLocalSet<{
credentialId: number;
externalId: string;
}>("toggledConnectedCalendars", []);
const [initalised, setInitalised] = useState(false);
const [continueWithProvider, setContinueWithProvider] = useOverlayCalendarStore(
(state) => [state.continueWithProviderModal, state.setContinueWithProviderModal],
shallow
);
const [calendarSettingsOverlay, setCalendarSettingsOverlay] = useOverlayCalendarStore(
(state) => [state.calendarSettingsOverlayModal, state.setCalendarSettingsOverlayModal],
shallow
);
const setOverlayBusyDates = useOverlayCalendarStore((state) => state.setOverlayBusyDates);
const { timezone } = useTimePreferences();
useEffect(() => {
if (overlayBusyDates) {
const nowDate = dayjs();
const usersTimezoneDate = nowDate.tz(timezone);
const offset = (usersTimezoneDate.utcOffset() - nowDate.utcOffset()) / 60;
const offsettedArray = overlayBusyDates.map((item) => {
return {
...item,
start: dayjs(item.start).add(offset, "hours").toDate(),
end: dayjs(item.end).add(offset, "hours").toDate(),
};
});
setOverlayBusyDates(offsettedArray);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [overlayBusyDates]);
useEffect(() => {
if (connectedCalendars && set.size === 0 && !initalised) {
connectedCalendars.forEach((item) => {
item.calendars?.forEach((cal) => {
const id = { credentialId: item.credentialId, externalId: cal.externalId };
if (cal.primary) {
toggleValue(id);
}
});
});
setInitalised(true);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [hasItem, set, initalised]);
const handleToggleConnectedCalendar = (externalCalendarId: string, credentialId: number) => {
toggleValue({
credentialId: credentialId,
externalId: externalCalendarId,
});
setOverlayBusyDates([]);
onToggleCalendar();
};
return {
isOpenOverlayContinueModal: continueWithProvider,
isOpenOverlaySettingsModal: calendarSettingsOverlay,
handleCloseContinueModal: (val: boolean) => setContinueWithProvider(val),
handleCloseSettingsModal: (val: boolean) => setCalendarSettingsOverlay(val),
handleToggleConnectedCalendar,
checkIsCalendarToggled: (externalId: string, credentialId: number) => {
return hasItem({
credentialId: credentialId,
externalId: externalId,
});
},
};
};