fix: add error handling for hotel room availability * fix: add error handling for hotel room availability * fix: update error codes * fix: have one error message to rule them all. them as in permutations of invalid dates Approved-by: Linus Flood
95 lines
2.1 KiB
TypeScript
95 lines
2.1 KiB
TypeScript
"use client"
|
|
|
|
import { useIntl } from "react-intl"
|
|
|
|
import { trpc } from "@/lib/trpc/client"
|
|
|
|
import Alert from "@/components/TempDesignSystem/Alert"
|
|
import useLang from "@/hooks/useLang"
|
|
import RatesProvider from "@/providers/RatesProvider"
|
|
|
|
import RateSummary from "./RateSummary"
|
|
import Rooms from "./Rooms"
|
|
import { RoomsContainerSkeleton } from "./RoomsContainerSkeleton"
|
|
|
|
import styles from "./index.module.css"
|
|
|
|
import type { RoomsContainerProps } from "@/types/components/hotelReservation/selectRate/roomsContainer"
|
|
import { AlertTypeEnum } from "@/types/enums/alert"
|
|
|
|
export function RoomsContainer({
|
|
booking,
|
|
hotelType,
|
|
roomCategories,
|
|
vat,
|
|
}: RoomsContainerProps) {
|
|
const lang = useLang()
|
|
const intl = useIntl()
|
|
|
|
const { data, isFetching, isError, error } =
|
|
trpc.hotel.availability.selectRate.rooms.useQuery(
|
|
{
|
|
booking,
|
|
lang,
|
|
},
|
|
{
|
|
retry(failureCount, error) {
|
|
if (error.data?.code === "BAD_REQUEST") {
|
|
return false
|
|
}
|
|
|
|
return failureCount <= 3
|
|
},
|
|
}
|
|
)
|
|
|
|
if (isFetching) {
|
|
return <RoomsContainerSkeleton />
|
|
}
|
|
|
|
if (isError) {
|
|
const errorMessage = getErrorMessage(error.data?.zodError?.formErrors, intl)
|
|
|
|
return (
|
|
<div className={styles.errorContainer}>
|
|
<Alert type={AlertTypeEnum.Alarm} heading={errorMessage} />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<RatesProvider
|
|
booking={booking}
|
|
hotelType={hotelType}
|
|
roomCategories={roomCategories}
|
|
roomsAvailability={data}
|
|
vat={vat}
|
|
>
|
|
<Rooms />
|
|
<RateSummary />
|
|
</RatesProvider>
|
|
)
|
|
}
|
|
|
|
function getErrorMessage(
|
|
formErrors: string[] | undefined,
|
|
intl: ReturnType<typeof useIntl>
|
|
) {
|
|
const firstError = formErrors?.at(0)
|
|
|
|
switch (firstError) {
|
|
case "FROMDATE_INVALID":
|
|
case "TODATE_INVALID":
|
|
case "TODATE_MUST_BE_AFTER_FROMDATE":
|
|
case "FROMDATE_CANNOT_BE_IN_THE_PAST": {
|
|
return intl.formatMessage({
|
|
defaultMessage: "Invalid dates",
|
|
})
|
|
}
|
|
default:
|
|
return intl.formatMessage({
|
|
defaultMessage: "Something went wrong",
|
|
})
|
|
}
|
|
}
|