fix(SW-1143): added safeTry

This commit is contained in:
Pontus Dreij
2024-12-09 20:29:31 +01:00
parent d4e4c4a0d0
commit 22a5edc2d7

View File

@@ -15,6 +15,7 @@ import Button from "@/components/TempDesignSystem/Button"
import Link from "@/components/TempDesignSystem/Link" import Link from "@/components/TempDesignSystem/Link"
import Subtitle from "@/components/TempDesignSystem/Text/Subtitle" import Subtitle from "@/components/TempDesignSystem/Text/Subtitle"
import { getIntl } from "@/i18n" import { getIntl } from "@/i18n"
import { safeTry } from "@/utils/safeTry"
import HotelCardListing from "../HotelCardListing" import HotelCardListing from "../HotelCardListing"
import HotelCount from "./HotelCount" import HotelCount from "./HotelCount"
@@ -38,20 +39,23 @@ export default async function SelectHotel({
const intl = await getIntl() const intl = await getIntl()
const hotels = await fetchAvailableHotels({ const hotelsPromise = safeTry(
cityId: city.id, fetchAvailableHotels({
roomStayStartDate: searchParams.fromDate, cityId: city.id,
roomStayEndDate: searchParams.toDate, roomStayStartDate: searchParams.fromDate,
adults: adultsParams, roomStayEndDate: searchParams.toDate,
children: childrenParams?.toString(), adults: adultsParams,
}) children: childrenParams?.toString(),
})
)
const [hotels] = await hotelsPromise
const isCityWithCountry = (city: any): city is { country: string } => const isCityWithCountry = (city: any): city is { country: string } =>
"country" in city "country" in city
const validHotels = hotels.filter( const validHotels =
(hotel): hotel is HotelData => hotel !== null hotels?.filter((hotel): hotel is HotelData => hotel !== null) || []
)
const filterList = getFiltersFromHotels(validHotels) const filterList = getFiltersFromHotels(validHotels)
const breadcrumbs = [ const breadcrumbs = [
@@ -76,7 +80,7 @@ export default async function SelectHotel({
}, },
] ]
const isAllUnavailable = hotels.every((hotel) => hotel.price === undefined) const isAllUnavailable = hotels?.every((hotel) => hotel.price === undefined)
return ( return (
<> <>
@@ -95,7 +99,7 @@ export default async function SelectHotel({
</header> </header>
<main className={styles.main}> <main className={styles.main}>
<div className={styles.sideBar}> <div className={styles.sideBar}>
{hotels.length > 0 ? ( // TODO: Temp fix until API returns hotels that are not available {hotels && hotels.length > 0 ? ( // TODO: Temp fix until API returns hotels that are not available
<Link <Link
className={styles.link} className={styles.link}
color="burgundy" color="burgundy"