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; min-height: 100dvh;
} }
.hotelCards { .section {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: var(--Spacing-x4);
} }
.link { .link {

View File

@@ -1,40 +1,75 @@
import { serverClient } from "@/lib/trpc/server" 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 HotelFilter from "@/components/HotelReservation/SelectHotel/HotelFilter"
import { ChevronRightIcon } from "@/components/Icons" import { ChevronRightIcon } from "@/components/Icons"
import StaticMap from "@/components/Maps/StaticMap" import StaticMap from "@/components/Maps/StaticMap"
import Link from "@/components/TempDesignSystem/Link" import Link from "@/components/TempDesignSystem/Link"
import Title from "@/components/TempDesignSystem/Text/Title"
import { getIntl } from "@/i18n" import { getIntl } from "@/i18n"
import { setLang } from "@/i18n/serverContext" import { getLang, setLang } from "@/i18n/serverContext"
import styles from "./page.module.css" 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" import { LangParams, PageArgs } from "@/types/params"
export default async function SelectHotelPage({ async function getAvailableHotels({
params, cityId,
}: PageArgs<LangParams>) { roomStayStartDate,
const intl = await getIntl() roomStayEndDate,
setLang(params.lang) adults,
}: AvailabilityInput): Promise<HotelData[] | null> {
const tempSearchTerm = "Stockholm"
const getAvailableHotels = await serverClient().hotel.availability.get({ const getAvailableHotels = await serverClient().hotel.availability.get({
cityId: "8ec4bba3-1c38-4606-82d1-bbe3f6738e54", cityId: cityId,
roomStayStartDate: "2024-11-02", roomStayStartDate: roomStayStartDate,
roomStayEndDate: "2024-11-03", roomStayEndDate: roomStayEndDate,
adults: 1, adults: adults,
}) })
if (!getAvailableHotels) return null if (!getAvailableHotels) return null
const { availability } = getAvailableHotels 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 ( return (
<main className={styles.main}> <main className={styles.main}>
<section> <section className={styles.section}>
<StaticMap <StaticMap
city={tempSearchTerm} city={tempSearchTerm}
width={340} width={340}
@@ -49,22 +84,7 @@ export default async function SelectHotelPage({
</Link> </Link>
<HotelFilter /> <HotelFilter />
</section> </section>
<section className={styles.hotelCards}> <HotelCardListing hotelData={hotels} />
{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>
</main> </main>
) )
} }

View File

@@ -1,5 +1,4 @@
import { serverClient } from "@/lib/trpc/server" import { useIntl } from "react-intl"
import tempHotelData from "@/server/routers/hotels/tempHotelData.json"
import { mapFacilityToIcon } from "@/components/ContentType/HotelPage/data" import { mapFacilityToIcon } from "@/components/ContentType/HotelPage/data"
import { import {
@@ -15,43 +14,30 @@ import Link from "@/components/TempDesignSystem/Link"
import Caption from "@/components/TempDesignSystem/Text/Caption" import Caption from "@/components/TempDesignSystem/Text/Caption"
import Footnote from "@/components/TempDesignSystem/Text/Footnote" import Footnote from "@/components/TempDesignSystem/Text/Footnote"
import Title from "@/components/TempDesignSystem/Text/Title" import Title from "@/components/TempDesignSystem/Text/Title"
import { getIntl } from "@/i18n"
import { getLang } from "@/i18n/serverContext"
import styles from "./hotelCard.module.css" import styles from "./hotelCard.module.css"
import { HotelCardProps } from "@/types/components/hotelReservation/selectHotel/hotelCardProps" import { HotelCardProps } from "@/types/components/hotelReservation/selectHotel/hotelCardProps"
export default async function HotelCard({ export default function HotelCard({ hotel }: HotelCardProps) {
checkInDate, const intl = useIntl()
checkOutDate,
hotelId,
price,
}: HotelCardProps) {
const intl = await getIntl()
// TODO: Use real endpoint. const { hotelData } = hotel
const hotel = tempHotelData.data.attributes const { price } = hotel
const hotelResponse = await serverClient().hotel.hotel.get({ if (!hotelData) return null
hotelId: hotelId.toString(), if (!price) return null
language: getLang(),
})
if (!hotelResponse) return null const sortedAmenities = hotelData.detailedFacilities
?.sort((a, b) => b.sortOrder - a.sortOrder)
const { data } = hotelResponse
const sortedAmenities = data.attributes.detailedFacilities
.sort((a, b) => b.sortOrder - a.sortOrder)
.slice(0, 5) .slice(0, 5)
return ( return (
<article className={styles.card}> <article className={styles.card}>
<section className={styles.imageContainer}> <section className={styles.imageContainer}>
<Image <Image
src={data.attributes.hotelContent.images.imageSizes.large} src={hotelData.hotelContent.images.imageSizes.medium}
alt={data.attributes.hotelContent.images.metaData.altText} alt={hotelData.hotelContent.images.metaData.altText}
width={300} width={300}
height={200} height={200}
className={styles.image} className={styles.image}
@@ -59,25 +45,25 @@ export default async function HotelCard({
<div className={styles.tripAdvisor}> <div className={styles.tripAdvisor}>
<Chip intent="primary" className={styles.tripAdvisor}> <Chip intent="primary" className={styles.tripAdvisor}>
<TripAdvisorIcon color="white" /> <TripAdvisorIcon color="white" />
{data.attributes.ratings?.tripAdvisor.rating} {hotelData.ratings?.tripAdvisor.rating}
</Chip> </Chip>
</div> </div>
</section> </section>
<section className={styles.hotelInformation}> <section className={styles.hotelInformation}>
<ScandicLogoIcon color="red" /> <ScandicLogoIcon color="red" />
<Title as="h4" textTransform="capitalize"> <Title as="h4" textTransform="capitalize">
{hotel.name} {hotelData.name}
</Title> </Title>
<Footnote color="textMediumContrast" className={styles.adress}> <Footnote color="textMediumContrast" className={styles.adress}>
{`${data.attributes.address.streetAddress}, ${data.attributes.address.city}`} {`${hotelData.address?.streetAddress}, ${hotelData.address?.city}`}
</Footnote> </Footnote>
<Footnote color="textMediumContrast"> <Footnote color="textMediumContrast">
{`${data.attributes.location.distanceToCentre} ${intl.formatMessage({ id: "km to city center" })}`} {`${hotelData.location.distanceToCentre} ${intl.formatMessage({ id: "km to city center" })}`}
</Footnote> </Footnote>
</section> </section>
<section className={styles.hotel}> <section className={styles.hotel}>
<div className={styles.facilities}> <div className={styles.facilities}>
{sortedAmenities.map((facility) => { {sortedAmenities?.map((facility) => {
const IconComponent = mapFacilityToIcon(facility.name) const IconComponent = mapFacilityToIcon(facility.name)
return ( return (
<div className={styles.facilitiesItem} key={facility.id}> <div className={styles.facilitiesItem} key={facility.id}>

View File

@@ -0,0 +1,5 @@
.hotelCards {
display: flex;
flex-direction: column;
gap: var(--Spacing-x4);
}

View File

@@ -0,0 +1,25 @@
"use client"
import Title from "@/components/TempDesignSystem/Text/Title"
import HotelCard from "../HotelCard"
import styles from "./hotelCardListing.module.css"
import { HotelCardListingProps } from "@/types/components/hotelReservation/selectHotel/hotelCardListingProps"
export default function HotelCardListing({ hotelData }: HotelCardListingProps) {
if (!hotelData) return null
return (
<section className={styles.hotelCards}>
{hotelData && hotelData.length ? (
hotelData.map((hotel) => (
<HotelCard key={hotel.hotelData?.name} hotel={hotel} />
))
) : (
<Title>Hallå</Title>
)}
</section>
)
}

View File

@@ -3,15 +3,15 @@ import { getIntl } from "@/i18n"
import styles from "./hotelFilter.module.css" import styles from "./hotelFilter.module.css"
export default async function HotelFilter() { export default async function HotelFilter() {
const { formatMessage } = await getIntl() const intl = await getIntl()
return ( return (
<aside className={styles.container}> <aside className={styles.container}>
<div className={styles.facilities}> <div className={styles.facilities}>
{formatMessage({ id: "Hotel facilities" })} {intl.formatMessage({ id: "Hotel facilities" })}
</div> </div>
<div className={styles.facilities}> <div className={styles.facilities}>
{formatMessage({ id: "Hotel surroundings" })} {intl.formatMessage({ id: "Hotel surroundings" })}
{/* {filters.hotelSurroundings.map((surroundings) => ( {/* {filters.hotelSurroundings.map((surroundings) => (
<div key={surroundings} className={styles.filter}> <div key={surroundings} className={styles.filter}>
<input id={surroundings} name={surroundings} type="checkbox" /> <input id={surroundings} name={surroundings} type="checkbox" />

View File

@@ -0,0 +1,10 @@
export type AvailabilityInput = {
cityId: string
roomStayStartDate: string
roomStayEndDate: string
adults: number
children?: number
promotionCode?: string
reservationProfileType?: string
attachedProfileId?: string
}

View File

@@ -0,0 +1,12 @@
import { AvailabilityPrices } from "@/server/routers/hotels/output"
import { Hotel } from "@/types/hotel"
export type HotelCardListingProps = {
hotelData: HotelData[]
}
export type HotelData = {
hotelData: Hotel | undefined
price: AvailabilityPrices
}

View File

@@ -1,11 +1,5 @@
import { import { HotelData } from "./hotelCardListingProps"
Availability,
AvailabilityPrices,
} from "@/server/routers/hotels/output"
export type HotelCardProps = { export type HotelCardProps = {
checkInDate: Availability["data"][number]["attributes"]["checkInDate"] hotel: HotelData
checkOutDate: Availability["data"][number]["attributes"]["checkOutDate"]
hotelId: Availability["data"][number]["attributes"]["hotelId"]
price: AvailabilityPrices
} }