fix: refactor language switcher
This commit is contained in:
@@ -132,6 +132,8 @@ const validateHeaderRefConfigSchema = z.object({
|
||||
}),
|
||||
})
|
||||
|
||||
export type HeaderRefDataRaw = z.infer<typeof validateHeaderRefConfigSchema>
|
||||
|
||||
const validateAppDownload = z.object({
|
||||
href: z.string(),
|
||||
imageConnection: z.object({
|
||||
@@ -155,9 +157,6 @@ const validateAppDownload = z.object({
|
||||
}),
|
||||
})
|
||||
|
||||
export type HeaderRefDataRaw = z.infer<typeof validateHeaderRefConfigSchema>
|
||||
|
||||
|
||||
const validateNavigationItem = z.object({
|
||||
links: z.array(z.object({ href: z.string(), title: z.string() })),
|
||||
title: z.string(),
|
||||
@@ -240,3 +239,27 @@ export type FooterData = Omit<
|
||||
> & {
|
||||
logo: Image
|
||||
}
|
||||
|
||||
const validateFooterRefConfigSchema = z.object({
|
||||
all_current_footer: z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
system: z.object({
|
||||
content_type_uid: z.string(),
|
||||
uid: z.string(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
|
||||
export type FooterRefDataRaw = z.infer<typeof validateFooterRefConfigSchema>
|
||||
|
||||
export const validateLanguageSwitcherData = z.object({
|
||||
en: z.object({ url: z.string(), isExternal: z.boolean() }).optional(),
|
||||
da: z.object({ url: z.string(), isExternal: z.boolean() }).optional(),
|
||||
de: z.object({ url: z.string(), isExternal: z.boolean() }).optional(),
|
||||
fi: z.object({ url: z.string(), isExternal: z.boolean() }).optional(),
|
||||
sv: z.object({ url: z.string(), isExternal: z.boolean() }).optional(),
|
||||
no: z.object({ url: z.string(), isExternal: z.boolean() }).optional(),
|
||||
})
|
||||
|
||||
@@ -1,9 +1,26 @@
|
||||
import { Lang } from "@/constants/languages"
|
||||
import { batchRequest } from "@/lib/graphql/batchRequest"
|
||||
import {
|
||||
GetDaDeEnUrlsAccountPage,
|
||||
GetFiNoSvUrlsAccountPage,
|
||||
} from "@/lib/graphql/Query/AccountPage.graphql"
|
||||
import { GetContactConfig } from "@/lib/graphql/Query/ContactConfig.graphql"
|
||||
import { GetCurrentFooter } from "@/lib/graphql/Query/CurrentFooter.graphql"
|
||||
import {
|
||||
GetCurrentFooter,
|
||||
GetCurrentFooterRef,
|
||||
} from "@/lib/graphql/Query/CurrentFooter.graphql"
|
||||
import {
|
||||
GetCurrentHeader,
|
||||
GetCurrentHeaderRef,
|
||||
} from "@/lib/graphql/Query/CurrentHeader.graphql"
|
||||
import {
|
||||
GetDaDeEnUrlsCurrentBlocksPage,
|
||||
GetFiNoSvUrlsCurrentBlocksPage,
|
||||
} from "@/lib/graphql/Query/LanguageSwitcherCurrent.graphql"
|
||||
import {
|
||||
GetDaDeEnUrlsLoyaltyPage,
|
||||
GetFiNoSvUrlsLoyaltyPage,
|
||||
} from "@/lib/graphql/Query/LoyaltyPage.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { internalServerError, notFound } from "@/server/errors/trpc"
|
||||
import { contentstackProcedure, publicProcedure, router } from "@/server/trpc"
|
||||
@@ -13,14 +30,22 @@ import { generateTag } from "@/utils/generateTag"
|
||||
import {
|
||||
type ContactConfigData,
|
||||
FooterDataRaw,
|
||||
FooterRefDataRaw,
|
||||
HeaderData,
|
||||
HeaderDataRaw,
|
||||
HeaderRefDataRaw,
|
||||
validateContactConfigSchema,
|
||||
validateFooterConfigSchema,
|
||||
validateHeaderConfigSchema,
|
||||
validateLanguageSwitcherData,
|
||||
} from "./output"
|
||||
|
||||
import {
|
||||
LanguageSwitcherData,
|
||||
LanguageSwitcherQueryDataRaw,
|
||||
} from "@/types/requests/languageSwitcher"
|
||||
import { PageTypeEnum } from "@/types/requests/pageType"
|
||||
|
||||
export const configQueryRouter = router({
|
||||
contact: contentstackProcedure.query(async ({ ctx }) => {
|
||||
const { lang } = ctx
|
||||
@@ -85,13 +110,24 @@ export const configQueryRouter = router({
|
||||
} as HeaderData
|
||||
}),
|
||||
footer: contentstackProcedure.query(async ({ ctx }) => {
|
||||
const responseRef = await request<FooterRefDataRaw>(GetCurrentFooterRef, {
|
||||
locale: ctx.lang,
|
||||
})
|
||||
|
||||
const response = await request<FooterDataRaw>(
|
||||
GetCurrentFooter,
|
||||
{
|
||||
locale: ctx.lang,
|
||||
},
|
||||
{
|
||||
next: { tags: [`footer-${ctx.lang}`] },
|
||||
next: {
|
||||
tags: [
|
||||
generateTag(
|
||||
ctx.lang,
|
||||
responseRef.data.all_current_footer.items[0].system.uid
|
||||
),
|
||||
],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -105,4 +141,99 @@ export const configQueryRouter = router({
|
||||
|
||||
return validatedFooterConfig.data.all_current_footer.items[0]
|
||||
}),
|
||||
languageSwitcher: contentstackProcedure.query(async ({ ctx }) => {
|
||||
const variables = { uid: ctx.uid, locale: ctx.lang }
|
||||
let urls: LanguageSwitcherData
|
||||
switch (ctx.contentType) {
|
||||
case PageTypeEnum.accountPage:
|
||||
const accountPageRes = await batchRequest<LanguageSwitcherQueryDataRaw>(
|
||||
[
|
||||
{
|
||||
document: GetDaDeEnUrlsAccountPage,
|
||||
variables,
|
||||
},
|
||||
{
|
||||
document: GetFiNoSvUrlsAccountPage,
|
||||
variables,
|
||||
},
|
||||
]
|
||||
)
|
||||
|
||||
const accountPageUrls = Object.keys(
|
||||
accountPageRes.data
|
||||
).reduce<LanguageSwitcherData>((acc, key) => {
|
||||
const item = accountPageRes.data[key as Lang]?.items?.[0]
|
||||
const url = item
|
||||
? { url: `/${key}${item.url}`, isExternal: false }
|
||||
: undefined
|
||||
return { ...acc, [key]: url }
|
||||
}, {} as LanguageSwitcherData)
|
||||
|
||||
const validatedAccountLanguageSwitcherData =
|
||||
validateLanguageSwitcherData.safeParse(accountPageUrls)
|
||||
|
||||
if (!validatedAccountLanguageSwitcherData.success) {
|
||||
throw internalServerError(validatedAccountLanguageSwitcherData.error)
|
||||
}
|
||||
|
||||
urls = validatedAccountLanguageSwitcherData.data
|
||||
break
|
||||
case PageTypeEnum.loyaltyPage:
|
||||
const loyaltyPageRes = await batchRequest<LanguageSwitcherQueryDataRaw>(
|
||||
[
|
||||
{
|
||||
document: GetDaDeEnUrlsLoyaltyPage,
|
||||
variables,
|
||||
},
|
||||
{
|
||||
document: GetFiNoSvUrlsLoyaltyPage,
|
||||
variables,
|
||||
},
|
||||
]
|
||||
)
|
||||
const loyaltyPageUrls = Object.keys(
|
||||
loyaltyPageRes.data
|
||||
).reduce<LanguageSwitcherData>((acc, key) => {
|
||||
const item = loyaltyPageRes.data[key as Lang]?.items?.[0]
|
||||
const url = item
|
||||
? {
|
||||
url: item.web?.original_url || `/${key}${item.url}`,
|
||||
isExternal: !!item?.web?.original_url,
|
||||
}
|
||||
: undefined
|
||||
|
||||
return {
|
||||
...acc,
|
||||
[key]: url,
|
||||
}
|
||||
}, {} as LanguageSwitcherData)
|
||||
|
||||
const validatedLoyaltyLanguageSwitcherData =
|
||||
validateLanguageSwitcherData.safeParse(loyaltyPageUrls)
|
||||
|
||||
if (!validatedLoyaltyLanguageSwitcherData.success) {
|
||||
throw internalServerError(validatedLoyaltyLanguageSwitcherData.error)
|
||||
}
|
||||
|
||||
urls = validatedLoyaltyLanguageSwitcherData.data
|
||||
break
|
||||
case PageTypeEnum.currentBlocksPage:
|
||||
const { data } = await batchRequest<LanguageSwitcherData>([
|
||||
{
|
||||
document: GetDaDeEnUrlsCurrentBlocksPage,
|
||||
variables,
|
||||
},
|
||||
{
|
||||
document: GetFiNoSvUrlsCurrentBlocksPage,
|
||||
variables,
|
||||
},
|
||||
])
|
||||
|
||||
urls = data
|
||||
break
|
||||
default:
|
||||
urls = [] as unknown as LanguageSwitcherData
|
||||
}
|
||||
return { urls, lang: ctx.lang }
|
||||
}),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user