Merged in chore/move-enter-details (pull request #2778)
Chore/move enter details Approved-by: Anton Gunnarsson
This commit is contained in:
62
packages/booking-flow/lib/hooks/useHandleBookingStatus.ts
Normal file
62
packages/booking-flow/lib/hooks/useHandleBookingStatus.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
"use client"
|
||||
|
||||
import { useRef } from "react"
|
||||
|
||||
import { trpc } from "@scandic-hotels/trpc/client"
|
||||
|
||||
import useLang from "./useLang"
|
||||
|
||||
import type { BookingStatusEnum } from "@scandic-hotels/trpc/enums/bookingStatus"
|
||||
|
||||
export function useHandleBookingStatus({
|
||||
refId,
|
||||
expectedStatuses,
|
||||
maxRetries,
|
||||
retryInterval,
|
||||
enabled,
|
||||
}: {
|
||||
refId: string | null
|
||||
expectedStatuses: BookingStatusEnum[]
|
||||
maxRetries: number
|
||||
retryInterval: number
|
||||
enabled: boolean
|
||||
}) {
|
||||
const lang = useLang()
|
||||
const retries = useRef(0)
|
||||
|
||||
const query = trpc.booking.status.useQuery(
|
||||
{
|
||||
lang,
|
||||
refId: refId ?? "",
|
||||
},
|
||||
{
|
||||
enabled,
|
||||
refetchInterval: (query) => {
|
||||
retries.current = query.state.dataUpdateCount
|
||||
|
||||
if (query.state.error || query.state.dataUpdateCount >= maxRetries) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (
|
||||
expectedStatuses.includes(
|
||||
query.state.data?.booking.reservationStatus as BookingStatusEnum
|
||||
)
|
||||
) {
|
||||
return false
|
||||
}
|
||||
|
||||
return retryInterval
|
||||
},
|
||||
refetchIntervalInBackground: true,
|
||||
refetchOnWindowFocus: false,
|
||||
refetchOnMount: false,
|
||||
retry: false,
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
...query,
|
||||
isTimeout: retries.current >= maxRetries,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user