From 28efca98b92eae6c948bf8f3ecc767b4b60376a0 Mon Sep 17 00:00:00 2001 From: Christel Westerberg Date: Mon, 27 May 2024 16:46:01 +0200 Subject: [PATCH] feat: fetch urls for language switcher from contentstack --- lib/graphql/Query/AccountPage.graphql | 36 ++++++++++++++++ ...raphql => LanguageSwitcherCurrent.graphql} | 4 +- lib/graphql/Query/LoyaltyPage.graphql | 36 ++++++++++++++++ middlewares/cmsContent.ts | 8 +++- .../contentstack/accountPage/output.ts | 9 ++++ .../routers/contentstack/accountPage/query.ts | 42 +++++++++++++++++++ server/routers/contentstack/config/output.ts | 6 +-- server/routers/contentstack/config/query.ts | 1 - .../contentstack/loyaltyPage/output.ts | 9 ++++ .../routers/contentstack/loyaltyPage/query.ts | 42 +++++++++++++++++++ server/routers/loyalty/temp.ts | 26 ++++++------ types/components/current/header/mainMenu.ts | 1 + types/components/current/header/topMenu.ts | 1 + types/components/current/languageSwitcher.ts | 8 ++-- types/requests/languageSwitcher.ts | 18 +++++++- 15 files changed, 223 insertions(+), 24 deletions(-) rename lib/graphql/Query/{LanguageSwitcher.graphql => LanguageSwitcherCurrent.graphql} (85%) diff --git a/lib/graphql/Query/AccountPage.graphql b/lib/graphql/Query/AccountPage.graphql index 63dd933b9..785627f42 100644 --- a/lib/graphql/Query/AccountPage.graphql +++ b/lib/graphql/Query/AccountPage.graphql @@ -62,3 +62,39 @@ query GetAccountPageRefs($locale: String!, $uid: String!) { } } } + +query GetDaDeEnUrls($uid: String!) { + de: all_account_page(where: { uid: $uid }, locale: "de") { + items { + url + } + } + en: all_account_page(where: { uid: $uid }, locale: "en") { + items { + url + } + } + da: all_account_page(where: { uid: $uid }, locale: "da") { + items { + url + } + } +} + +query GetFiNoSvUrls($uid: String!) { + fi: all_account_page(where: { uid: $uid }, locale: "fi") { + items { + url + } + } + no: all_account_page(where: { uid: $uid }, locale: "no") { + items { + url + } + } + sv: all_account_page(where: { uid: $uid }, locale: "sv") { + items { + url + } + } +} diff --git a/lib/graphql/Query/LanguageSwitcher.graphql b/lib/graphql/Query/LanguageSwitcherCurrent.graphql similarity index 85% rename from lib/graphql/Query/LanguageSwitcher.graphql rename to lib/graphql/Query/LanguageSwitcherCurrent.graphql index 1ca39ed97..821d2c52b 100644 --- a/lib/graphql/Query/LanguageSwitcher.graphql +++ b/lib/graphql/Query/LanguageSwitcherCurrent.graphql @@ -5,6 +5,9 @@ query GetDaDeEnUrls($uid: String!) { en: current_blocks_page(uid: $uid, locale: "en") { url: original_url } + da: current_blocks_page(uid: $uid, locale: "da") { + url: original_url + } } query GetFiNoSvUrls($uid: String!) { @@ -18,4 +21,3 @@ query GetFiNoSvUrls($uid: String!) { url: original_url } } - diff --git a/lib/graphql/Query/LoyaltyPage.graphql b/lib/graphql/Query/LoyaltyPage.graphql index aeed3368f..e8a8b59d1 100644 --- a/lib/graphql/Query/LoyaltyPage.graphql +++ b/lib/graphql/Query/LoyaltyPage.graphql @@ -245,3 +245,39 @@ query GetLoyaltyPageRefs($locale: String!, $uid: String!) { } } } + +query GetDaDeEnUrls($uid: String!) { + de: all_loyalty_page(where: { uid: $uid }, locale: "de") { + items { + url + } + } + en: all_loyalty_page(where: { uid: $uid }, locale: "en") { + items { + url + } + } + da: all_loyalty_page(where: { uid: $uid }, locale: "da") { + items { + url + } + } +} + +query GetFiNoSvUrls($uid: String!) { + sv: all_loyalty_page(where: { uid: $uid }, locale: "sv") { + items { + url + } + } + no: all_loyalty_page(where: { uid: $uid }, locale: "no") { + items { + url + } + } + fi: all_loyalty_page(where: { uid: $uid }, locale: "fi") { + items { + url + } + } +} diff --git a/middlewares/cmsContent.ts b/middlewares/cmsContent.ts index e12fe7435..06219b65a 100644 --- a/middlewares/cmsContent.ts +++ b/middlewares/cmsContent.ts @@ -54,12 +54,18 @@ export const middleware: NextMiddleware = async (request) => { } if (isCurrent) { + searchParams.set("uid", uid) searchParams.set("uri", pathNameWithoutLang) return NextResponse.rewrite( new URL( `/${lang}/current-content-page?${searchParams.toString()}`, nextUrl - ) + ), + { + request: { + headers, + }, + } ) } diff --git a/server/routers/contentstack/accountPage/output.ts b/server/routers/contentstack/accountPage/output.ts index e78b0951e..7bf0c6dd7 100644 --- a/server/routers/contentstack/accountPage/output.ts +++ b/server/routers/contentstack/accountPage/output.ts @@ -188,3 +188,12 @@ export const validateAccountPageRefsSchema = z.object({ export type AccountPageRefsDataRaw = z.infer< typeof validateAccountPageRefsSchema > + +export const validateLanguageSwitcherData = z.object({ + en: z.object({ items: z.array(z.object({ url: z.string() }).nullable()) }), + da: z.object({ items: z.array(z.object({ url: z.string() }).nullable()) }), + de: z.object({ items: z.array(z.object({ url: z.string() }).nullable()) }), + fi: z.object({ items: z.array(z.object({ url: z.string() }).nullable()) }), + sv: z.object({ items: z.array(z.object({ url: z.string() }).nullable()) }), + no: z.object({ items: z.array(z.object({ url: z.string() }).nullable()) }), +}) diff --git a/server/routers/contentstack/accountPage/query.ts b/server/routers/contentstack/accountPage/query.ts index 17375efa6..642671e0d 100644 --- a/server/routers/contentstack/accountPage/query.ts +++ b/server/routers/contentstack/accountPage/query.ts @@ -1,6 +1,10 @@ +import { Lang } from "@/constants/languages" +import { batchRequest } from "@/lib/graphql/batchRequest" import { GetAccountPage, GetAccountPageRefs, + GetDaDeEnUrls, + GetFiNoSvUrls, } from "@/lib/graphql/Query/AccountPage.graphql" import { request } from "@/lib/graphql/request" import { internalServerError, notFound } from "@/server/errors/trpc" @@ -18,11 +22,16 @@ import { AccountPageRefsDataRaw, validateAccountPageRefsSchema, validateAccountPageSchema, + validateLanguageSwitcherData, } from "./output" import { getConnections } from "./utils" import { ContentEntries } from "@/types/components/myPages/myPage/enums" import { Embeds } from "@/types/requests/embeds" +import { + LanguageSwitcherData, + LanguageSwitcherQueryDataRaw, +} from "@/types/requests/languageSwitcher" import { Edges } from "@/types/requests/utils/edges" import { RTEDocument } from "@/types/rte/node" @@ -131,4 +140,37 @@ export const accountPageQueryRouter = router({ return accountPage }), + languageSwitcher: contentstackProcedure.query(async ({ ctx }) => { + const variables = { + uid: ctx.uid, + } + + const res = await batchRequest([ + { + document: GetDaDeEnUrls, + variables, + }, + { + document: GetFiNoSvUrls, + variables, + }, + ]) + + const validatedLanguageSwitcherData = + validateLanguageSwitcherData.safeParse(res.data) + + if (!validatedLanguageSwitcherData.success) { + throw internalServerError(validatedLanguageSwitcherData.error) + } + + const urls = Object.keys( + validatedLanguageSwitcherData.data + ).reduce((acc, key) => { + const items = validatedLanguageSwitcherData.data[key as Lang].items + const url = items.length ? items[0]?.url : undefined + return { ...acc, [key]: { url } } + }, {} as LanguageSwitcherData) + + return { lang: ctx.lang, urls } + }), }) diff --git a/server/routers/contentstack/config/output.ts b/server/routers/contentstack/config/output.ts index d466e5556..388b9a82b 100644 --- a/server/routers/contentstack/config/output.ts +++ b/server/routers/contentstack/config/output.ts @@ -70,7 +70,7 @@ export const validateHeaderConfigSchema = z.object({ edges: z.array( z.object({ node: z.object({ - description: z.string().optional(), + description: z.string().optional().nullable(), dimension: z.object({ height: z.number(), width: z.number(), @@ -79,8 +79,8 @@ export const validateHeaderConfigSchema = z.object({ system: z.object({ uid: z.string(), }), - title: z.string(), - url: z.string(), + title: z.string().nullable(), + url: z.string().nullable(), }), }) ), diff --git a/server/routers/contentstack/config/query.ts b/server/routers/contentstack/config/query.ts index 31ac79394..286d89524 100644 --- a/server/routers/contentstack/config/query.ts +++ b/server/routers/contentstack/config/query.ts @@ -35,7 +35,6 @@ export const configQueryRouter = router({ return validatedContactConfigConfig.data.all_contact_config.items[0] }), header: publicProcedure.query(async ({ ctx }) => { - console.log({ ctx }) const response = await request( GetCurrentHeader, { locale: ctx.lang }, diff --git a/server/routers/contentstack/loyaltyPage/output.ts b/server/routers/contentstack/loyaltyPage/output.ts index 7fd660663..7f0d8a734 100644 --- a/server/routers/contentstack/loyaltyPage/output.ts +++ b/server/routers/contentstack/loyaltyPage/output.ts @@ -306,3 +306,12 @@ export const validateLoyaltyPageRefsSchema = z.object({ export type LoyaltyPageRefsDataRaw = z.infer< typeof validateLoyaltyPageRefsSchema > + +export const validateLanguageSwitcherData = z.object({ + en: z.object({ items: z.array(z.object({ url: z.string() }).nullable()) }), + da: z.object({ items: z.array(z.object({ url: z.string() }).nullable()) }), + de: z.object({ items: z.array(z.object({ url: z.string() }).nullable()) }), + fi: z.object({ items: z.array(z.object({ url: z.string() }).nullable()) }), + sv: z.object({ items: z.array(z.object({ url: z.string() }).nullable()) }), + no: z.object({ items: z.array(z.object({ url: z.string() }).nullable()) }), +}) diff --git a/server/routers/contentstack/loyaltyPage/query.ts b/server/routers/contentstack/loyaltyPage/query.ts index 565b06bc5..1c75c22cc 100644 --- a/server/routers/contentstack/loyaltyPage/query.ts +++ b/server/routers/contentstack/loyaltyPage/query.ts @@ -1,4 +1,8 @@ +import { Lang } from "@/constants/languages" +import { batchRequest } from "@/lib/graphql/batchRequest" import { + GetDaDeEnUrls, + GetFiNoSvUrls, GetLoyaltyPage, GetLoyaltyPageRefs, } from "@/lib/graphql/Query/LoyaltyPage.graphql" @@ -17,12 +21,17 @@ import { removeEmptyObjects } from "../../utils" import { LoyaltyPage, type LoyaltyPageRefsDataRaw, + validateLanguageSwitcherData, validateLoyaltyPageRefsSchema, validateLoyaltyPageSchema, } from "./output" import { getConnections } from "./utils" import { LoyaltyBlocksTypenameEnum } from "@/types/components/loyalty/enums" +import { + LanguageSwitcherData, + LanguageSwitcherQueryDataRaw, +} from "@/types/requests/languageSwitcher" function makeButtonObject(button: any) { return { @@ -172,4 +181,37 @@ export const loyaltyPageQueryRouter = router({ // Assert LoyaltyPage type to get correct typings for RTE fields return validatedLoyaltyPage.data as LoyaltyPage }), + languageSwitcher: contentstackProcedure.query(async ({ ctx }) => { + const variables = { + uid: ctx.uid, + } + + const res = await batchRequest([ + { + document: GetDaDeEnUrls, + variables, + }, + { + document: GetFiNoSvUrls, + variables, + }, + ]) + + const validatedLanguageSwitcherData = + validateLanguageSwitcherData.safeParse(res.data) + + if (!validatedLanguageSwitcherData.success) { + throw internalServerError(validatedLanguageSwitcherData.error) + } + + const urls = Object.keys( + validatedLanguageSwitcherData.data + ).reduce((acc, key) => { + const items = validatedLanguageSwitcherData.data[key as Lang].items + const url = items.length ? items[0]?.url : undefined + return { ...acc, [key]: { url } } + }, {} as LanguageSwitcherData) + + return { lang: ctx.lang, urls } + }), }) diff --git a/server/routers/loyalty/temp.ts b/server/routers/loyalty/temp.ts index a0dc42ef1..2dbfb2c56 100644 --- a/server/routers/loyalty/temp.ts +++ b/server/routers/loyalty/temp.ts @@ -9,11 +9,11 @@ export const allLevels = [ "Always best price", "Book reward nights with points", ], - logo: "/_static/icons/new-friend.png", + logo: "/_static/icons/loyaltyLevels/new-friend.svg", }, { tier: 2, - name: "New Friend", + name: "Good Friend", requiredPoints: 50000, requiredNights: "X", benefits: [ @@ -21,11 +21,11 @@ export const allLevels = [ "Always best price", "Book reward nights with points", ], - logo: "/_static/icons/new-friend.png", + logo: "/_static/icons/loyaltyLevels/good-friend.svg", }, { tier: 3, - name: "New Friend", + name: "Close Friend", requiredPoints: 50000, requiredNights: "X", benefits: [ @@ -33,11 +33,11 @@ export const allLevels = [ "Always best price", "Book reward nights with points", ], - logo: "/_static/icons/new-friend.png", + logo: "/_static/icons/loyaltyLevels/close-friend.svg", }, { tier: 4, - name: "New Friend", + name: "Dear Friend", requiredPoints: 50000, requiredNights: "X", benefits: [ @@ -45,11 +45,11 @@ export const allLevels = [ "Always best price", "Book reward nights with points", ], - logo: "/_static/icons/new-friend.png", + logo: "/_static/icons/loyaltyLevels/dear-friend.svg", }, { tier: 5, - name: "New Friend", + name: "Loyal Friend", requiredPoints: 50000, requiredNights: "X", benefits: [ @@ -57,11 +57,11 @@ export const allLevels = [ "Always best price", "Book reward nights with points", ], - logo: "/_static/icons/new-friend.png", + logo: "/_static/icons/loyaltyLevels/loyal-friend.svg", }, { tier: 6, - name: "New Friend", + name: "True Friend", requiredPoints: 50000, requiredNights: "X", benefits: [ @@ -69,11 +69,11 @@ export const allLevels = [ "Always best price", "Book reward nights with points", ], - logo: "/_static/icons/new-friend.png", + logo: "/_static/icons/loyaltyLevels/true-friend.svg", }, { tier: 7, - name: "New Friend", + name: "Best Friend", requiredPoints: 50000, requiredNights: "X", benefits: [ @@ -81,6 +81,6 @@ export const allLevels = [ "Always best price", "Book reward nights with points", ], - logo: "/_static/icons/new-friend.png", + logo: "/_static/icons/loyaltyLevels/best-friend.svg", }, ] diff --git a/types/components/current/header/mainMenu.ts b/types/components/current/header/mainMenu.ts index 3f0887e87..d0189500a 100644 --- a/types/components/current/header/mainMenu.ts +++ b/types/components/current/header/mainMenu.ts @@ -7,4 +7,5 @@ export type MainMenuProps = { links: HeaderLink[] logo: Image topMenuMobileLinks: TopMenuHeaderLink[] + languageSwitcher: React.ReactNode } diff --git a/types/components/current/header/topMenu.ts b/types/components/current/header/topMenu.ts index bda691693..b0a6e4ec3 100644 --- a/types/components/current/header/topMenu.ts +++ b/types/components/current/header/topMenu.ts @@ -4,4 +4,5 @@ export type TopMenuProps = { frontpageLinkText: string homeHref: string links: TopMenuHeaderLink[] + languageSwitcher: React.ReactNode } diff --git a/types/components/current/languageSwitcher.ts b/types/components/current/languageSwitcher.ts index a4d06428f..e29bb4cff 100644 --- a/types/components/current/languageSwitcher.ts +++ b/types/components/current/languageSwitcher.ts @@ -1,4 +1,6 @@ -import type { LanguageSwitcherQueryData } from "@/types/requests/languageSwitcher" +import { Lang } from "@/constants/languages" + +import type { LanguageSwitcherData } from "@/types/requests/languageSwitcher" export type LanguageSwitcherLink = { href: string @@ -6,6 +8,6 @@ export type LanguageSwitcherLink = { } export type LanguageSwitcherProps = { - currentLanguage: string - urls: LanguageSwitcherQueryData + currentLanguage: Lang + urls: LanguageSwitcherData } diff --git a/types/requests/languageSwitcher.ts b/types/requests/languageSwitcher.ts index f8e56108e..d1de4307b 100644 --- a/types/requests/languageSwitcher.ts +++ b/types/requests/languageSwitcher.ts @@ -1,8 +1,22 @@ -type LanguageResult = { +type CurrentLanguageResult = { url: string } -export type LanguageSwitcherQueryData = { +export type LanguageSwitcherData = { + da: CurrentLanguageResult | undefined + de: CurrentLanguageResult | undefined + en: CurrentLanguageResult | undefined + fi: CurrentLanguageResult | undefined + no: CurrentLanguageResult | undefined + sv: CurrentLanguageResult | undefined +} + +type LanguageResult = { + items: { url: string }[] +} + +export type LanguageSwitcherQueryDataRaw = { + da: LanguageResult | undefined de: LanguageResult | undefined en: LanguageResult | undefined fi: LanguageResult | undefined