Fix: created hook for useScrollToTop to reuse functionallity and util for getSortedHotels
This commit is contained in:
35
components/HotelReservation/HotelCardListing/utils.ts
Normal file
35
components/HotelReservation/HotelCardListing/utils.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import type { HotelData } from "@/types/components/hotelReservation/selectHotel/hotelCardListingProps"
|
||||
import { SortOrder } from "@/types/components/hotelReservation/selectHotel/hotelSorter"
|
||||
|
||||
export function getSortedHotels({
|
||||
hotels,
|
||||
sortBy,
|
||||
}: {
|
||||
hotels: HotelData[]
|
||||
sortBy: string
|
||||
}) {
|
||||
const getPricePerNight = (hotel: HotelData): number =>
|
||||
hotel.price?.member?.localPrice?.pricePerNight ??
|
||||
hotel.price?.public?.localPrice?.pricePerNight ??
|
||||
Infinity
|
||||
|
||||
const sortingStrategies: Record<
|
||||
string,
|
||||
(a: HotelData, b: HotelData) => number
|
||||
> = {
|
||||
[SortOrder.Name]: (a: HotelData, b: HotelData) =>
|
||||
a.hotelData.name.localeCompare(b.hotelData.name),
|
||||
[SortOrder.TripAdvisorRating]: (a: HotelData, b: HotelData) =>
|
||||
(b.hotelData.ratings?.tripAdvisor.rating ?? 0) -
|
||||
(a.hotelData.ratings?.tripAdvisor.rating ?? 0),
|
||||
[SortOrder.Price]: (a: HotelData, b: HotelData) =>
|
||||
getPricePerNight(a) - getPricePerNight(b),
|
||||
[SortOrder.Distance]: (a: HotelData, b: HotelData) =>
|
||||
a.hotelData.location.distanceToCentre -
|
||||
b.hotelData.location.distanceToCentre,
|
||||
}
|
||||
|
||||
return [...hotels].sort(
|
||||
sortingStrategies[sortBy] ?? sortingStrategies[SortOrder.Distance]
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user