* feat(SW-2271): Changes to hotel data types in preperation for filtering * feat(SW-2271): Added filter and sort functionality Approved-by: Matilda Landström
35 lines
903 B
TypeScript
35 lines
903 B
TypeScript
import { HotelSortOption } from "../types/hotel"
|
|
|
|
import type { DestinationCityListItem } from "../types/destinationCityPage"
|
|
|
|
const CITY_SORTING_STRATEGIES: Partial<
|
|
Record<
|
|
HotelSortOption,
|
|
(a: DestinationCityListItem, b: DestinationCityListItem) => number
|
|
>
|
|
> = {
|
|
[HotelSortOption.Name]: function (a, b) {
|
|
return a.cityName.localeCompare(b.cityName)
|
|
},
|
|
[HotelSortOption.Recommended]: function (a, b) {
|
|
if (a.sort_order === null && b.sort_order === null) {
|
|
return a.cityName.localeCompare(b.cityName)
|
|
}
|
|
if (a.sort_order === null) {
|
|
return 1
|
|
}
|
|
if (b.sort_order === null) {
|
|
return -1
|
|
}
|
|
return b.sort_order - a.sort_order
|
|
},
|
|
}
|
|
|
|
export function getSortedCities(
|
|
cities: DestinationCityListItem[],
|
|
sortOption: HotelSortOption
|
|
) {
|
|
const sortFn = CITY_SORTING_STRATEGIES[sortOption]
|
|
return sortFn ? cities.sort(sortFn) : cities
|
|
}
|