Files
cal.diy/packages/platform/atoms/hooks/useAvailableSlots.ts
T
048e56806c feat: useApiV2AvailableSlots (#21138)
* feat: useApiV2AvailableSlots

* feat: useApiV2AvailableSlots

* fix type check

* remove log

* fix unit tests

* simplify

* feat: add missing params to the slots endpoint

* updated documentation

* _shouldServeCache is correct

* fix types

* fix enabled prop

* fix type

* add embedConnectVersion to query key

* add teamId to slot type

* fix

* add fallback to trpc query if apiv2 fails

* add comment

* remove logs

* fix

* refactor: default to isTeamEvent in query params

* fix: only return v2 slots once fetch is success

* fix last e2e failing test

* add feature flag

* add migration sql

* Update packages/prisma/migrations/20250512153630_add_use_api_v2_for_team_slots_feature_flag/migration.sql

---------

Co-authored-by: hbjORbj <[email protected]>
Co-authored-by: Somay Chauhan <[email protected]>
Co-authored-by: supalarry <[email protected]>
Co-authored-by: Keith Williams <[email protected]>
2025-05-12 20:48:29 +00:00

50 lines
1.3 KiB
TypeScript

import { useQuery } from "@tanstack/react-query";
import { SUCCESS_STATUS } from "@calcom/platform-constants";
import type { AvailableSlotsType } from "@calcom/platform-libraries";
import type {
GetAvailableSlotsInput_2024_04_15,
ApiResponse,
ApiSuccessResponse,
} from "@calcom/platform-types";
import http from "../lib/http";
export const QUERY_KEY = "get-available-slots";
export const useAvailableSlots = ({
enabled,
...rest
}: GetAvailableSlotsInput_2024_04_15 & { enabled: boolean }) => {
const availableSlots = useQuery({
queryKey: [
QUERY_KEY,
rest.startTime,
rest.endTime,
rest.eventTypeId,
rest.eventTypeSlug,
rest.isTeamEvent ?? false,
rest.teamId ?? false,
rest.usernameList,
rest.routedTeamMemberIds,
rest.skipContactOwner,
rest._shouldServeCache,
rest.teamMemberEmail,
],
queryFn: () => {
return http
.get<ApiResponse<AvailableSlotsType>>("/slots/available", {
params: rest,
})
.then((res) => {
if (res.data.status === SUCCESS_STATUS) {
return (res.data as ApiSuccessResponse<AvailableSlotsType>).data;
}
throw new Error(res.data.error.message);
});
},
enabled: enabled,
});
return availableSlots;
};