Files
web/apps/scandic-web/components/HotelReservation/HotelCardListing/utils.ts
Hrishikesh Vaipurkar e6a3e5dbd8 Merged in feat/SW-2398-ui-update-for-booking-codes (pull request #1862)
feat: SW-2398 UI updates booking codes

* feat: SW-2398 UI updates booking codes

* feat: SW-2398 Rate cards UI changes

* feat: SW-2398 Optimized css with vars and chip code

* feat: SW-2398 Optimized code as review comments

* feat: SW-2398 Optimized code

* feat: SW-2398 Optimized code and mobile UX

* feat: SW-2398 Optimized code

* feat: SW-2398 Fixed UI

* feat: SW-2398 Updated animation


Approved-by: Erik Tiekstra
2025-05-02 12:36:22 +00:00

71 lines
2.3 KiB
TypeScript

import { SortOrder } from "@/types/components/hotelReservation/selectHotel/hotelSorter"
import type { HotelResponse } from "@/components/HotelReservation/SelectHotel/helpers"
function getPricePerNight(hotel: HotelResponse): number {
return (
hotel.availability.productType?.member?.localPrice?.pricePerNight ??
hotel.availability.productType?.public?.localPrice?.pricePerNight ??
hotel.availability.productType?.redemptions?.find(
(r) => r?.localPrice.pointsPerStay
)?.localPrice?.pointsPerStay ??
Infinity
)
}
export function getSortedHotels({
hotels,
sortBy,
bookingCode,
}: {
hotels: HotelResponse[]
sortBy: string
bookingCode: string | null
}) {
const availableHotels = hotels.filter(
(hotel) => !!hotel.availability.productType
)
const unavailableHotels = hotels.filter(
(hotel) => !hotel.availability.productType
)
const sortingStrategies: Record<
string,
(a: HotelResponse, b: HotelResponse) => number
> = {
[SortOrder.Name]: (a: HotelResponse, b: HotelResponse) =>
a.hotel.name.localeCompare(b.hotel.name),
[SortOrder.TripAdvisorRating]: (a: HotelResponse, b: HotelResponse) =>
(b.hotel.ratings?.tripAdvisor.rating ?? 0) -
(a.hotel.ratings?.tripAdvisor.rating ?? 0),
[SortOrder.Price]: (a: HotelResponse, b: HotelResponse) =>
getPricePerNight(a) - getPricePerNight(b),
[SortOrder.Distance]: (a: HotelResponse, b: HotelResponse) =>
a.hotel.location.distanceToCentre - b.hotel.location.distanceToCentre,
}
const sortStrategy =
sortingStrategies[sortBy] ?? sortingStrategies[SortOrder.Distance]
if (bookingCode) {
const bookingCodeRateHotels = availableHotels.filter(
(hotel) =>
hotel.availability.productType?.public?.bookingCode ||
hotel.availability.productType?.member?.bookingCode
)
const regularRateHotels = availableHotels.filter(
(hotel) =>
!hotel.availability.productType?.public?.bookingCode &&
!hotel?.availability.productType?.member?.bookingCode
)
return bookingCodeRateHotels
.sort(sortStrategy)
.concat(regularRateHotels.sort(sortStrategy))
.concat(unavailableHotels.sort(sortStrategy))
}
return availableHotels
.sort(sortStrategy)
.concat(unavailableHotels.sort(sortStrategy))
}