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 intl = await getIntl()
|
||||||
const lang = await getLang()
|
const lang = await getLang()
|
||||||
|
|
||||||
if (bookingCode && isBookingCodeRateNotAvailable) {
|
if (bookingCode && isBookingCodeRateNotAvailable && hotelsLength > 0) {
|
||||||
const bookingCodeText = intl.formatMessage(
|
const bookingCodeText = intl.formatMessage(
|
||||||
{
|
{
|
||||||
defaultMessage:
|
defaultMessage:
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { dt } from "@scandic-hotels/common/dt"
|
||||||
import { AvailabilityEnum } from "@scandic-hotels/trpc/enums/selectHotel"
|
import { AvailabilityEnum } from "@scandic-hotels/trpc/enums/selectHotel"
|
||||||
import { generateChildrenString } from "@scandic-hotels/trpc/routers/hotels/helpers"
|
import { generateChildrenString } from "@scandic-hotels/trpc/routers/hotels/helpers"
|
||||||
|
|
||||||
@@ -182,6 +183,14 @@ export async function getHotels({
|
|||||||
redemption,
|
redemption,
|
||||||
}: GetHotelsInput) {
|
}: GetHotelsInput) {
|
||||||
let availableHotelsResponse: SettledResult = []
|
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) {
|
if (isAlternativeFor) {
|
||||||
availableHotelsResponse = await Promise.allSettled(
|
availableHotelsResponse = await Promise.allSettled(
|
||||||
rooms.map(async (room) => {
|
rooms.map(async (room) => {
|
||||||
|
|||||||
@@ -14,8 +14,6 @@ import { RoomsContainerSkeleton } from "./RoomsContainerSkeleton"
|
|||||||
|
|
||||||
import styles from "./index.module.css"
|
import styles from "./index.module.css"
|
||||||
|
|
||||||
import type { AppRouter } from "@scandic-hotels/trpc/routers/appRouter"
|
|
||||||
|
|
||||||
import type { RoomsContainerProps } from "@/types/components/hotelReservation/selectRate/roomsContainer"
|
import type { RoomsContainerProps } from "@/types/components/hotelReservation/selectRate/roomsContainer"
|
||||||
|
|
||||||
export function RoomsContainer({}: RoomsContainerProps) {
|
export function RoomsContainer({}: RoomsContainerProps) {
|
||||||
@@ -23,7 +21,7 @@ export function RoomsContainer({}: RoomsContainerProps) {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
availability: { error, isFetching, isError },
|
availability: { error, isFetching, isError },
|
||||||
input: { hasError: hasInputError },
|
input: { hasError: hasInputError, errorCode },
|
||||||
} = useSelectRateContext()
|
} = useSelectRateContext()
|
||||||
|
|
||||||
if (isFetching) {
|
if (isFetching) {
|
||||||
@@ -31,7 +29,7 @@ export function RoomsContainer({}: RoomsContainerProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isError || hasInputError) {
|
if (isError || hasInputError) {
|
||||||
const errorMessage = getErrorMessage(error, intl)
|
const errorMessage = getErrorMessage(error ?? errorCode, intl)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.errorContainer}>
|
<div className={styles.errorContainer}>
|
||||||
@@ -49,15 +47,14 @@ export function RoomsContainer({}: RoomsContainerProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getErrorMessage(error: unknown, intl: ReturnType<typeof useIntl>) {
|
function getErrorMessage(error: unknown, intl: ReturnType<typeof useIntl>) {
|
||||||
if (!isTRPCClientError(error)) {
|
let errorCode = ""
|
||||||
return intl.formatMessage({
|
if (error instanceof TRPCClientError) {
|
||||||
defaultMessage: "Something went wrong",
|
errorCode = error.data?.zodError?.formErrors?.at(0)
|
||||||
})
|
} else if (typeof error == "string") {
|
||||||
|
errorCode = error
|
||||||
}
|
}
|
||||||
|
|
||||||
const firstError = error.data?.zodError?.formErrors?.at(0)
|
switch (errorCode) {
|
||||||
|
|
||||||
switch (firstError) {
|
|
||||||
case "FROMDATE_INVALID":
|
case "FROMDATE_INVALID":
|
||||||
case "TODATE_INVALID":
|
case "TODATE_INVALID":
|
||||||
case "TODATE_MUST_BE_AFTER_FROMDATE":
|
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,
|
lang,
|
||||||
})
|
})
|
||||||
|
|
||||||
const hotelId = selectRateInput.data?.booking.hotelId
|
const hotelId = selectRateInput.data?.booking.hotelId ?? hotelData.hotel.id
|
||||||
if (hotelId !== hotelData.hotel.id) {
|
|
||||||
throw new Error("Mismatched hotel ID in SelectRateProvider")
|
|
||||||
}
|
|
||||||
|
|
||||||
const hotelQuery = trpc.hotel.get.useQuery(
|
const hotelQuery = trpc.hotel.get.useQuery(
|
||||||
{ hotelId: hotelId!, language: lang, isCardOnlyPayment: false },
|
{ hotelId: hotelId!, language: lang, isCardOnlyPayment: false },
|
||||||
@@ -394,6 +391,7 @@ export function SelectRateProvider({
|
|||||||
selectRateInput.data?.booking.fromDate,
|
selectRateInput.data?.booking.fromDate,
|
||||||
selectRateInput.data?.booking.toDate
|
selectRateInput.data?.booking.toDate
|
||||||
),
|
),
|
||||||
|
errorCode: selectRateInput.error?.errors[0].message,
|
||||||
bookingCode: selectRateInput.data?.booking.bookingCode,
|
bookingCode: selectRateInput.data?.booking.bookingCode,
|
||||||
roomCount: roomCount,
|
roomCount: roomCount,
|
||||||
isMultiRoom: roomCount > 1,
|
isMultiRoom: roomCount > 1,
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ export type SelectRateContext = {
|
|||||||
>
|
>
|
||||||
input: {
|
input: {
|
||||||
data: RoomsAvailabilityOutputSchema | undefined
|
data: RoomsAvailabilityOutputSchema | undefined
|
||||||
|
errorCode?: string
|
||||||
hasError: boolean
|
hasError: boolean
|
||||||
nights: number
|
nights: number
|
||||||
isMultiRoom: boolean
|
isMultiRoom: boolean
|
||||||
|
|||||||
Reference in New Issue
Block a user