Fix/SW-902 update response city availability * fix(SW-902): update response for API changes * fix(SW-902): add total row for pricePerStay * fix(SW-902): fix optional requestedPrice * fix(SW-902): fix bookingCode output * feat(SW-903): fix sorting Approved-by: Pontus Dreij Approved-by: Niclas Edenvin
101 lines
2.9 KiB
TypeScript
101 lines
2.9 KiB
TypeScript
"use client"
|
|
import { useSearchParams } from "next/navigation"
|
|
import { useMemo } from "react"
|
|
|
|
import { useHotelFilterStore } from "@/stores/hotel-filters"
|
|
|
|
import HotelCard from "../HotelCard"
|
|
import { DEFAULT_SORT } from "../SelectHotel/HotelSorter"
|
|
|
|
import styles from "./hotelCardListing.module.css"
|
|
|
|
import {
|
|
type HotelCardListingProps,
|
|
HotelCardListingTypeEnum,
|
|
type HotelData,
|
|
} from "@/types/components/hotelReservation/selectHotel/hotelCardListingProps"
|
|
import { SortOrder } from "@/types/components/hotelReservation/selectHotel/hotelSorter"
|
|
|
|
export default function HotelCardListing({
|
|
hotelData,
|
|
type = HotelCardListingTypeEnum.PageListing,
|
|
activeCard,
|
|
onHotelCardHover,
|
|
}: HotelCardListingProps) {
|
|
const searchParams = useSearchParams()
|
|
const activeFilters = useHotelFilterStore((state) => state.activeFilters)
|
|
const setResultCount = useHotelFilterStore((state) => state.setResultCount)
|
|
|
|
const sortBy = useMemo(
|
|
() => searchParams.get("sort") ?? DEFAULT_SORT,
|
|
[searchParams]
|
|
)
|
|
|
|
const sortedHotels = useMemo(() => {
|
|
switch (sortBy) {
|
|
case SortOrder.Name:
|
|
return [...hotelData].sort((a, b) =>
|
|
a.hotelData.name.localeCompare(b.hotelData.name)
|
|
)
|
|
case SortOrder.TripAdvisorRating:
|
|
return [...hotelData].sort(
|
|
(a, b) =>
|
|
(b.hotelData.ratings?.tripAdvisor.rating ?? 0) -
|
|
(a.hotelData.ratings?.tripAdvisor.rating ?? 0)
|
|
)
|
|
case SortOrder.Price:
|
|
const getPricePerNight = (hotel: HotelData): number => {
|
|
return (
|
|
hotel.price?.member?.localPrice?.pricePerNight ??
|
|
hotel.price?.public?.localPrice?.pricePerNight ??
|
|
0
|
|
)
|
|
}
|
|
return [...hotelData].sort(
|
|
(a, b) => getPricePerNight(a) - getPricePerNight(b)
|
|
)
|
|
case SortOrder.Distance:
|
|
default:
|
|
return [...hotelData].sort(
|
|
(a, b) =>
|
|
a.hotelData.location.distanceToCentre -
|
|
b.hotelData.location.distanceToCentre
|
|
)
|
|
}
|
|
}, [hotelData, sortBy])
|
|
|
|
const hotels = useMemo(() => {
|
|
if (activeFilters.length === 0) {
|
|
setResultCount(sortedHotels.length)
|
|
return sortedHotels
|
|
}
|
|
|
|
const filteredHotels = sortedHotels.filter((hotel) =>
|
|
activeFilters.every((appliedFilterId) =>
|
|
hotel.hotelData.detailedFacilities.some(
|
|
(facility) => facility.id.toString() === appliedFilterId
|
|
)
|
|
)
|
|
)
|
|
|
|
setResultCount(filteredHotels.length)
|
|
return filteredHotels
|
|
}, [activeFilters, sortedHotels, setResultCount])
|
|
|
|
return (
|
|
<section className={styles.hotelCards}>
|
|
{hotels?.length
|
|
? hotels.map((hotel) => (
|
|
<HotelCard
|
|
key={hotel.hotelData.operaId}
|
|
hotel={hotel}
|
|
type={type}
|
|
state={hotel.hotelData.name === activeCard ? "active" : "default"}
|
|
onHotelCardHover={onHotelCardHover}
|
|
/>
|
|
))
|
|
: null}
|
|
</section>
|
|
)
|
|
}
|