feat(SW-2466): Sorting destinations by country depending on language

Approved-by: Christian Andolf
Approved-by: Matilda Landström
This commit is contained in:
Erik Tiekstra
2025-06-02 09:38:20 +00:00
parent 9aa5c294a3
commit 9a868e6fe5
2 changed files with 64 additions and 9 deletions

View File

@@ -32,6 +32,7 @@ import {
destinationOverviewPageRefsSchema, destinationOverviewPageRefsSchema,
destinationOverviewPageSchema, destinationOverviewPageSchema,
} from "./output" } from "./output"
import { getSortedDestinationsByLanguage } from "./utils"
import type { import type {
City, City,
@@ -170,17 +171,17 @@ export const destinationOverviewPageQueryRouter = router({
if (useStaticData) { if (useStaticData) {
switch (ctx.lang) { switch (ctx.lang) {
case Lang.da: case Lang.da:
return destinationsDataDa return getSortedDestinationsByLanguage(destinationsDataDa, ctx.lang)
case Lang.de: case Lang.de:
return destinationsDataDe return getSortedDestinationsByLanguage(destinationsDataDe, ctx.lang)
case Lang.fi: case Lang.fi:
return destinationsDataFi return getSortedDestinationsByLanguage(destinationsDataFi, ctx.lang)
case Lang.en: case Lang.en:
return destinationsDataEn return getSortedDestinationsByLanguage(destinationsDataEn, ctx.lang)
case Lang.no: case Lang.no:
return destinationsDataNo return getSortedDestinationsByLanguage(destinationsDataNo, ctx.lang)
case Lang.sv: case Lang.sv:
return destinationsDataSv return getSortedDestinationsByLanguage(destinationsDataSv, ctx.lang)
default: default:
return [] return []
} }
@@ -258,9 +259,7 @@ export const destinationOverviewPageQueryRouter = router({
}) })
) )
const data = destinations.sort((a, b) => const data = getSortedDestinationsByLanguage(destinations, lang)
a.country.localeCompare(b.country)
)
const fs = await import("node:fs") const fs = await import("node:fs")
fs.writeFileSync( fs.writeFileSync(
`./server/routers/contentstack/destinationOverviewPage/destinations-${lang}.json`, `./server/routers/contentstack/destinationOverviewPage/destinations-${lang}.json`,

View File

@@ -0,0 +1,56 @@
import { Lang } from "@/constants/languages"
import type { DestinationsData } from "@/types/components/destinationOverviewPage/destinationsList/destinationsData"
import { ApiCountry, Country } from "@/types/enums/country"
/**
* Sorts destination data based on language preference:
* - en: alphabetical order
* - de: Germany first, then alphabetical
* - da: Denmark first, then alphabetical
* - no: Norway first, then alphabetical
* - sv: Sweden first, then alphabetical
* - fi: Finland first, then alphabetical
*
* @param destinations DestinationsData
* @param language Lang
* @returns Sorted array of destinations
*/
export function getSortedDestinationsByLanguage(
destinations: DestinationsData,
language: Lang
) {
const destinationsToSort = [...destinations]
const firstCountryByLanguage: Record<Lang, Country | null> = {
[Lang.de]: Country.Germany,
[Lang.da]: Country.Denmark,
[Lang.no]: Country.Norway,
[Lang.sv]: Country.Sweden,
[Lang.fi]: Country.Finland,
[Lang.en]: null,
}
const firstCountry = firstCountryByLanguage[language]
// If no country is defined for this language, sort alphabetically
if (!firstCountry) {
return destinationsToSort.sort((a, b) =>
a.country.localeCompare(b.country, language)
)
}
// Get the localized name of the first country
const localizedCountryName = ApiCountry[language][firstCountry]
return destinationsToSort.sort((a, b) => {
if (a.country === localizedCountryName) {
return -1
}
if (b.country === localizedCountryName) {
return 1
}
return a.country.localeCompare(b.country, language)
})
}