refactor: Handle apiv2 team slots in useSchedule hook (#21309)
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user