Skip to content

Scheduling infrastructure for absolutely everyone.

refactor: Handle apiv2 team slots in useSchedule hook (#21309)

Benny Jooe082cc7d3d18
diff --git a/packages/features/bookings/Booker/utils/event.ts b/packages/features/bookings/Booker/utils/event.ts
index 919fe3b5b53..5a69e6926e0 100644
--- a/packages/features/bookings/Booker/utils/event.ts
+++ b/packages/features/bookings/Booker/utils/event.ts
@@ -97,7 +97,7 @@ export const useScheduleForEvent = ({
const searchParams = useCompatSearchParams();
const rescheduleUid = searchParams?.get("rescheduleUid");
- const scheduleUsingApiV2 = useSchedule({
+ const schedule = useSchedule({
username: usernameFromStore ?? username,
eventSlug: eventSlugFromStore ?? eventSlug,
eventId,
@@ -115,29 +115,6 @@ export const useScheduleForEvent = ({
useApiV2: useApiV2,
});
- const scheduleNotUsingApiV2 = useSchedule({
- username: usernameFromStore ?? username,
- eventSlug: eventSlugFromStore ?? eventSlug,
- eventId,
- timezone,
- selectedDate,
- prefetchNextMonth,
- monthCount,
- dayCount,
- rescheduleUid,
- month: monthFromStore ?? month,
- duration: durationFromStore ?? duration,
- isTeamEvent,
- orgSlug,
- teamMemberEmail,
- useApiV2: false,
- // only run this query if the one using Api v2 fails
- // Network error does not trigger `isError` flag, so we are instead using `failureReason` here
- enabled: isTeamEvent && !!scheduleUsingApiV2?.failureReason,
- });
-
- const schedule = scheduleUsingApiV2?.isSuccess ? scheduleUsingApiV2 : scheduleNotUsingApiV2;
-
return {
data: schedule?.data,
isPending: schedule?.isPending,
diff --git a/packages/features/schedules/lib/use-schedule/useSchedule.ts b/packages/features/schedules/lib/use-schedule/useSchedule.ts
index 3fc02f95f0e..6dd1afad25e 100644
--- a/packages/features/schedules/lib/use-schedule/useSchedule.ts
+++ b/packages/features/schedules/lib/use-schedule/useSchedule.ts
@@ -122,7 +122,9 @@ export const useSchedule = ({
};
const isCallingApiV2Slots = useApiV2 && Boolean(isTeamEvent) && options.enabled;
- const teamSchedule = useApiV2AvailableSlots({
+
+ // API V2 query for team events
+ const teamScheduleV2 = useApiV2AvailableSlots({
...input,
enabled: isCallingApiV2Slots,
duration: input.duration ? Number(input.duration) : undefined,
@@ -131,26 +133,31 @@ export const useSchedule = ({
eventTypeId: eventId ?? undefined,
});
- if (isCallingApiV2Slots) {
+ const schedule = isTeamEvent
+ ? trpc.viewer.highPerf.getTeamSchedule.useQuery(input, {
+ ...options,
+ // Only enable if we're not using API V2 or if API V2 failed
+ enabled: options.enabled && (!isCallingApiV2Slots || !!teamScheduleV2.failureReason),
+ })
+ : trpc.viewer.slots.getSchedule.useQuery(input, options);
+
+ if (isCallingApiV2Slots && !teamScheduleV2.failureReason) {
updateEmbedBookerState({
bookerState,
- slotsQuery: teamSchedule,
+ slotsQuery: teamScheduleV2,
});
+
return {
- ...teamSchedule,
+ ...teamScheduleV2,
/**
* Invalidates the request and resends it regardless of any other configuration including staleTime
*/
invalidate: () => {
- return teamSchedule.refetch();
+ return teamScheduleV2.refetch();
},
};
}
- const schedule = isTeamEvent
- ? trpc.viewer.highPerf.getTeamSchedule.useQuery(input, options)
- : trpc.viewer.slots.getSchedule.useQuery(input, options);
-
updateEmbedBookerState({
bookerState,
slotsQuery: schedule,