Files
web/apps/scandic-web/components/HotelReservation/SelectRate/RoomsContainer/index.tsx
Anton Gunnarsson 03468ad824 Merged in fix/refactor-booking-flow-search-params (pull request #2148)
Fix: refactor booking flow search params

* wip: apply codemod and upgrade swc plugin

* wip: design-system to react 19, fix issues from async (search)params

* Prepare new parse function for booking flow search params

* Prepare serialize function for booking flow search params

* Improve handling of comma separated arrays

* Slightly refactor for readability

* Next abstracts URLSearchParams so handle the abstraction instead

* Refactor booking widget to use new search params parsing

* Rename search param functions

* Refactor select-hotel to use new search param parser

* Use new search params parser in select-rate and details

* Fix hotelId type

* Avoid passing down search params into BookingWidget components

* More updates to use new types instead of SearchParams<T>

* Remove types SelectHotelSearchParams and AlternativeSelectHotelSearchParams

* Fix parseBookingWidgetSearchParams return type

* Add error handling to booking search param parsers

* Fix modifyRateIndex handling in details page

* Clean up

* Refactor booking widget search param serializing to util function

* Move start page booking widget search param parsing to page

* Use new search param serializer in HandleErrorCallback

* Delete convertSearchParamsToObj & convertObjToSearchParams


Approved-by: Michael Zetterberg
2025-06-02 13:38:01 +00:00

107 lines
2.6 KiB
TypeScript

"use client"
import { notFound, useSearchParams } from "next/navigation"
import { useIntl } from "react-intl"
import { trpc } from "@/lib/trpc/client"
import { selectRateRoomsAvailabilityInputSchema } from "@/server/routers/hotels/input"
import Alert from "@/components/TempDesignSystem/Alert"
import useLang from "@/hooks/useLang"
import RatesProvider from "@/providers/RatesProvider"
import { parseSelectRateSearchParams, searchParamsToRecord } from "@/utils/url"
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({
hotelType,
roomCategories,
vat,
}: RoomsContainerProps) {
const lang = useLang()
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,
})
if (isFetching) {
return <RoomsContainerSkeleton />
}
if (isError || !bookingInput.success) {
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",
})
}
}