Merged in fix/remove-old-select-rate (pull request #2647)
Fix/remove old select rate * remove old select-rate * Fix imports * renamed SelectRate2 -> SelectRate
This commit is contained in:
@@ -1,69 +1,37 @@
|
||||
"use client"
|
||||
|
||||
import { notFound, useSearchParams } from "next/navigation"
|
||||
import { TRPCClientError } from "@trpc/client"
|
||||
import { useIntl } from "react-intl"
|
||||
|
||||
import {
|
||||
parseSelectRateSearchParams,
|
||||
searchParamsToRecord,
|
||||
} from "@scandic-hotels/booking-flow/utils/url"
|
||||
import { trpc } from "@scandic-hotels/trpc/client"
|
||||
import { selectRateRoomsAvailabilityInputSchema } from "@scandic-hotels/trpc/routers/hotels/input"
|
||||
import { AlertTypeEnum } from "@scandic-hotels/trpc/types/alertType"
|
||||
|
||||
import Alert from "@/components/TempDesignSystem/Alert"
|
||||
import useLang from "@/hooks/useLang"
|
||||
import RatesProvider from "@/providers/RatesProvider"
|
||||
import { useSelectRateContext } from "@/contexts/SelectRate/SelectRateContext"
|
||||
|
||||
import RateSummary from "./RateSummary"
|
||||
import { RateSummary } from "./RateSummary"
|
||||
import Rooms from "./Rooms"
|
||||
import { RoomsContainerSkeleton } from "./RoomsContainerSkeleton"
|
||||
|
||||
import styles from "./index.module.css"
|
||||
|
||||
import type { AppRouter } from "@scandic-hotels/trpc/routers/appRouter"
|
||||
|
||||
import type { RoomsContainerProps } from "@/types/components/hotelReservation/selectRate/roomsContainer"
|
||||
|
||||
export function RoomsContainer({
|
||||
hotelType,
|
||||
roomCategories,
|
||||
vat,
|
||||
}: RoomsContainerProps) {
|
||||
const lang = useLang()
|
||||
export function RoomsContainer({}: RoomsContainerProps) {
|
||||
const intl = useIntl()
|
||||
const searchParams = useSearchParams()
|
||||
|
||||
const booking = parseSelectRateSearchParams(
|
||||
searchParamsToRecord(searchParams)
|
||||
)
|
||||
|
||||
if (!booking) return notFound()
|
||||
|
||||
const bookingInput = selectRateRoomsAvailabilityInputSchema.safeParse({
|
||||
booking,
|
||||
lang,
|
||||
})
|
||||
|
||||
const { data, isFetching, isError, error } =
|
||||
trpc.hotel.availability.selectRate.rooms.useQuery(bookingInput.data!, {
|
||||
retry(failureCount, error) {
|
||||
if (error.data?.code === "BAD_REQUEST") {
|
||||
return false
|
||||
}
|
||||
|
||||
return failureCount <= 3
|
||||
},
|
||||
enabled: bookingInput.success,
|
||||
})
|
||||
const {
|
||||
availability: { error, isFetching, isError },
|
||||
input: { hasError: hasInputError },
|
||||
} = useSelectRateContext()
|
||||
|
||||
if (isFetching) {
|
||||
return <RoomsContainerSkeleton />
|
||||
}
|
||||
|
||||
if (isError || !bookingInput.success) {
|
||||
const errorMessage = getErrorMessage(
|
||||
error?.data?.zodError?.formErrors,
|
||||
intl
|
||||
)
|
||||
if (isError || hasInputError) {
|
||||
const errorMessage = getErrorMessage(error, intl)
|
||||
|
||||
return (
|
||||
<div className={styles.errorContainer}>
|
||||
@@ -73,24 +41,21 @@ export function RoomsContainer({
|
||||
}
|
||||
|
||||
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)
|
||||
function getErrorMessage(error: unknown, intl: ReturnType<typeof useIntl>) {
|
||||
if (!isTRPCClientError(error)) {
|
||||
return intl.formatMessage({
|
||||
defaultMessage: "Something went wrong",
|
||||
})
|
||||
}
|
||||
|
||||
const firstError = error.data?.zodError?.formErrors?.at(0)
|
||||
|
||||
switch (firstError) {
|
||||
case "FROMDATE_INVALID":
|
||||
@@ -107,3 +72,9 @@ function getErrorMessage(
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function isTRPCClientError(
|
||||
cause: unknown
|
||||
): cause is TRPCClientError<AppRouter> {
|
||||
return cause instanceof TRPCClientError
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user