Files
web/apps/scandic-web/server/routers/contentstack/languageSwitcher/query.ts
Erik Tiekstra 19bb965298 Merged in feat/SW-1745-language-switcher-non-contentstack-pages (pull request #1501)
feat(SW-1745): Query for language switcher returns just current pathname for non-contentstack pages except for hotelreservation paths

* feat(SW-1745): Query for language switcher returns just current pathname for non-contentstack pages except for hotelreservation paths


Approved-by: Linus Flood
2025-03-10 09:06:19 +00:00

49 lines
1.6 KiB
TypeScript

import { baseUrls } from "@/constants/routes/baseUrls"
import { findMyBooking } from "@/constants/routes/findMyBooking"
import { hotelreservation } from "@/constants/routes/hotelReservation"
import { publicProcedure, router } from "@/server/trpc"
import { getUidAndContentTypeByPath } from "@/services/cms/getUidAndContentTypeByPath"
import { getLanguageSwitcherInput } from "./input"
import { getUrlsOfAllLanguages } from "./utils"
import type { LanguageSwitcherData } from "@/types/requests/languageSwitcher"
import type { Lang } from "@/constants/languages"
export const languageSwitcherQueryRouter = router({
get: publicProcedure
.input(getLanguageSwitcherInput)
.query(async ({ input }) => {
const { pathName, lang } = input
const { uid, contentType } = await getUidAndContentTypeByPath(pathName)
if (!uid || !contentType) {
// we have pages that are not currently routed within contentstack context,
// therefor this fix is needed for some of these pages
if (Object.values(findMyBooking).includes(pathName)) {
const urls: LanguageSwitcherData = {}
return {
lang,
urls: Object.entries(findMyBooking).reduce((acc, [lang, url]) => {
acc[lang as Lang] = { url }
return urls
}, urls),
}
}
if (pathName.startsWith(hotelreservation(lang))) {
return { lang, urls: baseUrls }
}
return { lang, urls: { [lang]: { url: pathName } } }
}
const urls = await getUrlsOfAllLanguages(lang, uid, contentType)
return {
lang,
urls,
}
}),
})