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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user