Merged in fix/SW-3254-error-fromdate_cannot_be_ (pull request #2676)
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
This commit is contained in:
@@ -18,7 +18,7 @@ export default async function NoAvailabilityAlert({
|
||||
const intl = await getIntl()
|
||||
const lang = await getLang()
|
||||
|
||||
if (bookingCode && isBookingCodeRateNotAvailable) {
|
||||
if (bookingCode && isBookingCodeRateNotAvailable && hotelsLength > 0) {
|
||||
const bookingCodeText = intl.formatMessage(
|
||||
{
|
||||
defaultMessage:
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { dt } from "@scandic-hotels/common/dt"
|
||||
import { AvailabilityEnum } from "@scandic-hotels/trpc/enums/selectHotel"
|
||||
import { generateChildrenString } from "@scandic-hotels/trpc/routers/hotels/helpers"
|
||||
|
||||
@@ -182,6 +183,14 @@ export async function getHotels({
|
||||
redemption,
|
||||
}: GetHotelsInput) {
|
||||
let availableHotelsResponse: SettledResult = []
|
||||
|
||||
// Return empty array (forced No availability) when search dates are invalid
|
||||
if (
|
||||
dt(fromDate).isBefore(dt(), "day") ||
|
||||
dt(toDate).isSameOrBefore(fromDate, "day")
|
||||
) {
|
||||
return []
|
||||
}
|
||||
if (isAlternativeFor) {
|
||||
availableHotelsResponse = await Promise.allSettled(
|
||||
rooms.map(async (room) => {
|
||||
|
||||
@@ -14,8 +14,6 @@ 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({}: RoomsContainerProps) {
|
||||
@@ -23,7 +21,7 @@ export function RoomsContainer({}: RoomsContainerProps) {
|
||||
|
||||
const {
|
||||
availability: { error, isFetching, isError },
|
||||
input: { hasError: hasInputError },
|
||||
input: { hasError: hasInputError, errorCode },
|
||||
} = useSelectRateContext()
|
||||
|
||||
if (isFetching) {
|
||||
@@ -31,7 +29,7 @@ export function RoomsContainer({}: RoomsContainerProps) {
|
||||
}
|
||||
|
||||
if (isError || hasInputError) {
|
||||
const errorMessage = getErrorMessage(error, intl)
|
||||
const errorMessage = getErrorMessage(error ?? errorCode, intl)
|
||||
|
||||
return (
|
||||
<div className={styles.errorContainer}>
|
||||
@@ -49,15 +47,14 @@ export function RoomsContainer({}: RoomsContainerProps) {
|
||||
}
|
||||
|
||||
function getErrorMessage(error: unknown, intl: ReturnType<typeof useIntl>) {
|
||||
if (!isTRPCClientError(error)) {
|
||||
return intl.formatMessage({
|
||||
defaultMessage: "Something went wrong",
|
||||
})
|
||||
let errorCode = ""
|
||||
if (error instanceof TRPCClientError) {
|
||||
errorCode = error.data?.zodError?.formErrors?.at(0)
|
||||
} else if (typeof error == "string") {
|
||||
errorCode = error
|
||||
}
|
||||
|
||||
const firstError = error.data?.zodError?.formErrors?.at(0)
|
||||
|
||||
switch (firstError) {
|
||||
switch (errorCode) {
|
||||
case "FROMDATE_INVALID":
|
||||
case "TODATE_INVALID":
|
||||
case "TODATE_MUST_BE_AFTER_FROMDATE":
|
||||
@@ -72,9 +69,3 @@ function getErrorMessage(error: unknown, intl: ReturnType<typeof useIntl>) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function isTRPCClientError(
|
||||
cause: unknown
|
||||
): cause is TRPCClientError<AppRouter> {
|
||||
return cause instanceof TRPCClientError
|
||||
}
|
||||
|
||||
@@ -85,10 +85,7 @@ export function SelectRateProvider({
|
||||
lang,
|
||||
})
|
||||
|
||||
const hotelId = selectRateInput.data?.booking.hotelId
|
||||
if (hotelId !== hotelData.hotel.id) {
|
||||
throw new Error("Mismatched hotel ID in SelectRateProvider")
|
||||
}
|
||||
const hotelId = selectRateInput.data?.booking.hotelId ?? hotelData.hotel.id
|
||||
|
||||
const hotelQuery = trpc.hotel.get.useQuery(
|
||||
{ hotelId: hotelId!, language: lang, isCardOnlyPayment: false },
|
||||
@@ -394,6 +391,7 @@ export function SelectRateProvider({
|
||||
selectRateInput.data?.booking.fromDate,
|
||||
selectRateInput.data?.booking.toDate
|
||||
),
|
||||
errorCode: selectRateInput.error?.errors[0].message,
|
||||
bookingCode: selectRateInput.data?.booking.bookingCode,
|
||||
roomCount: roomCount,
|
||||
isMultiRoom: roomCount > 1,
|
||||
|
||||
@@ -15,6 +15,7 @@ export type SelectRateContext = {
|
||||
>
|
||||
input: {
|
||||
data: RoomsAvailabilityOutputSchema | undefined
|
||||
errorCode?: string
|
||||
hasError: boolean
|
||||
nights: number
|
||||
isMultiRoom: boolean
|
||||
|
||||
Reference in New Issue
Block a user