feat(SW-2879): Move BookingWidget to booking-flow package * Fix lockfile * Fix styling * a tiny little booking widget test * Tiny fixes * Merge branch 'master' into feat/sw-2879-booking-widget-to-booking-flow-package * Remove unused scripts * lint:fix * Merge branch 'master' into feat/sw-2879-booking-widget-to-booking-flow-package * Tiny lint fixes * update test * Update Input in booking-flow * Clean up comments etc * Merge branch 'master' into feat/sw-2879-booking-widget-to-booking-flow-package * Setup tracking context for booking-flow * Add missing use client * Fix temp tracking function * Pass booking to booking-widget * Remove comment * Add use client to booking widget tracking provider * Add use client to tracking functions * Merge branch 'master' into feat/sw-2879-booking-widget-to-booking-flow-package * Move debug page * Merge branch 'master' into feat/sw-2879-booking-widget-to-booking-flow-package * Merge branch 'master' into feat/sw-2879-booking-widget-to-booking-flow-package * Merge branch 'master' into feat/sw-2879-booking-widget-to-booking-flow-package Approved-by: Bianca Widstam
110 lines
2.7 KiB
TypeScript
110 lines
2.7 KiB
TypeScript
"use client"
|
|
|
|
import { notFound, useSearchParams } from "next/navigation"
|
|
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 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"
|
|
|
|
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",
|
|
})
|
|
}
|
|
}
|