feat(SW-251): use parallel fetch for hotels

This commit is contained in:
Fredrik Thorsson
2024-09-11 10:22:01 +02:00
parent 8c530658c5
commit 43e58dcf01
9 changed files with 126 additions and 75 deletions

View File

@@ -6,10 +6,9 @@
min-height: 100dvh;
}
.hotelCards {
.section {
display: flex;
flex-direction: column;
gap: var(--Spacing-x4);
}
.link {

View File

@@ -1,40 +1,75 @@
import { serverClient } from "@/lib/trpc/server"
import { notFound } from "@/server/errors/next"
import HotelCard from "@/components/HotelReservation/HotelCard"
import HotelCardListing from "@/components/HotelReservation/HotelCardListing"
import HotelFilter from "@/components/HotelReservation/SelectHotel/HotelFilter"
import { ChevronRightIcon } from "@/components/Icons"
import StaticMap from "@/components/Maps/StaticMap"
import Link from "@/components/TempDesignSystem/Link"
import Title from "@/components/TempDesignSystem/Text/Title"
import { getIntl } from "@/i18n"
import { setLang } from "@/i18n/serverContext"
import { getLang, setLang } from "@/i18n/serverContext"
import styles from "./page.module.css"
import { AvailabilityInput } from "@/types/components/hotelReservation/selectHotel/availabilityInput"
import { HotelData } from "@/types/components/hotelReservation/selectHotel/hotelCardListingProps"
import { LangParams, PageArgs } from "@/types/params"
export default async function SelectHotelPage({
params,
}: PageArgs<LangParams>) {
const intl = await getIntl()
setLang(params.lang)
const tempSearchTerm = "Stockholm"
async function getAvailableHotels({
cityId,
roomStayStartDate,
roomStayEndDate,
adults,
}: AvailabilityInput): Promise<HotelData[] | null> {
const getAvailableHotels = await serverClient().hotel.availability.get({
cityId: "8ec4bba3-1c38-4606-82d1-bbe3f6738e54",
roomStayStartDate: "2024-11-02",
roomStayEndDate: "2024-11-03",
adults: 1,
cityId: cityId,
roomStayStartDate: roomStayStartDate,
roomStayEndDate: roomStayEndDate,
adults: adults,
})
if (!getAvailableHotels) return null
const { availability } = getAvailableHotels
const hotels = availability.map(async (hotel) => {
const hotelData = await serverClient().hotel.hotel.get({
hotelId: hotel.hotelId.toString(),
language: getLang(),
})
return {
hotelData: hotelData?.data.attributes,
price: hotel.bestPricePerNight,
}
})
return await Promise.all(hotels)
}
export default async function SelectHotelPage({
params,
}: PageArgs<LangParams>) {
setLang(params.lang)
const tempSearchTerm = "Stockholm"
const intl = await getIntl()
const hotels = await getAvailableHotels({
cityId: "8ec4bba3-1c38-4606-82d1-bbe3f6738e54",
roomStayStartDate: "2024-11-02",
roomStayEndDate: "2024-11-03",
adults: 1,
})
if (!hotels) return null
if (hotels.some((item) => item?.hotelData === undefined)) return notFound()
return (
<main className={styles.main}>
<section>
<section className={styles.section}>
<StaticMap
city={tempSearchTerm}
width={340}
@@ -49,22 +84,7 @@ export default async function SelectHotelPage({
</Link>
<HotelFilter />
</section>
<section className={styles.hotelCards}>
{availability.length ? (
availability.map((hotel) => (
<HotelCard
key={hotel.hotelId}
checkInDate={hotel.checkInDate}
checkOutDate={hotel.checkOutDate}
hotelId={hotel.hotelId}
price={hotel.bestPricePerNight}
/>
))
) : (
// TODO: handle no hotels found
<Title>No hotels found</Title>
)}
</section>
<HotelCardListing hotelData={hotels} />
</main>
)
}