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
49 lines
1.6 KiB
TypeScript
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,
|
|
}
|
|
}),
|
|
})
|