Fix/SW-3254 error fromdate cannot be * fix(SW-3254): Implemented date valdaton on select-hotel page * fix(SW-3254): Updated the browser URL to show proper booking dates * fix(SW-3254): Fixed select-rate when invalid dates searched * fix(SW-3254): Forced no availability for past dates * fix(SW-3254) Optimised code * fix(SW-3254): Optimised code Approved-by: Linus Flood
72 lines
1.8 KiB
TypeScript
72 lines
1.8 KiB
TypeScript
"use client"
|
|
|
|
import { TRPCClientError } from "@trpc/client"
|
|
import { useIntl } from "react-intl"
|
|
|
|
import { AlertTypeEnum } from "@scandic-hotels/trpc/types/alert"
|
|
|
|
import Alert from "@/components/TempDesignSystem/Alert"
|
|
import { useSelectRateContext } from "@/contexts/SelectRate/SelectRateContext"
|
|
|
|
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({}: RoomsContainerProps) {
|
|
const intl = useIntl()
|
|
|
|
const {
|
|
availability: { error, isFetching, isError },
|
|
input: { hasError: hasInputError, errorCode },
|
|
} = useSelectRateContext()
|
|
|
|
if (isFetching) {
|
|
return <RoomsContainerSkeleton />
|
|
}
|
|
|
|
if (isError || hasInputError) {
|
|
const errorMessage = getErrorMessage(error ?? errorCode, intl)
|
|
|
|
return (
|
|
<div className={styles.errorContainer}>
|
|
<Alert type={AlertTypeEnum.Alarm} heading={errorMessage} />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<Rooms />
|
|
<RateSummary />
|
|
</>
|
|
)
|
|
}
|
|
|
|
function getErrorMessage(error: unknown, intl: ReturnType<typeof useIntl>) {
|
|
let errorCode = ""
|
|
if (error instanceof TRPCClientError) {
|
|
errorCode = error.data?.zodError?.formErrors?.at(0)
|
|
} else if (typeof error == "string") {
|
|
errorCode = error
|
|
}
|
|
|
|
switch (errorCode) {
|
|
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",
|
|
})
|
|
}
|
|
}
|