From 9f0b044daa8e601b7608c2d07aa115ce606c64ac Mon Sep 17 00:00:00 2001 From: Christel Westerberg Date: Mon, 29 Apr 2024 10:49:19 +0200 Subject: [PATCH] fix: break out content type uid fetch to utils function --- .../(live)/(public)/loyalty-page/layout.tsx | 6 +- lib/graphql/Query/ContentTypeUid.graphql | 12 +--- middlewares/cmsContent.ts | 58 +++---------------- types/requests/contentTypeUid.ts | 11 ++++ utils/contentType.ts | 45 ++++++++++++++ 5 files changed, 68 insertions(+), 64 deletions(-) create mode 100644 types/requests/contentTypeUid.ts create mode 100644 utils/contentType.ts diff --git a/app/[lang]/(live)/(public)/loyalty-page/layout.tsx b/app/[lang]/(live)/(public)/loyalty-page/layout.tsx index 5812eca64..aa011671a 100644 --- a/app/[lang]/(live)/(public)/loyalty-page/layout.tsx +++ b/app/[lang]/(live)/(public)/loyalty-page/layout.tsx @@ -2,11 +2,9 @@ import { firaMono, firaSans } from "@/app/[lang]/(live)/fonts" import styles from "./layout.module.css" -import type { MyPagesLayoutProps } from "@/types/components/myPages/layout" - -export default async function LoyaltyPagesLayout({ +export default function LoyaltyPagesLayout({ children, -}: React.PropsWithChildren) { +}: React.PropsWithChildren) { return (
{ const { nextUrl } = request const lang = findLang(nextUrl.pathname) @@ -42,28 +15,7 @@ export const middleware: NextMiddleware = async (request) => { const pathNameWithoutLang = nextUrl.pathname.replace(`/${lang}`, "") const searchParams = new URLSearchParams(request.nextUrl.searchParams) - const print = (await import("graphql/language/printer")).print - const result = await fetch(env.CMS_URL, { - method: "POST", - headers: { - access_token: env.CMS_ACCESS_TOKEN, - "Content-Type": "application/json", - }, - body: JSON.stringify({ - query: print(GetContentTypeUid as DocumentNode), - variables: { - locale: lang, - url: pathNameWithoutLang, - }, - }), - }) - - const pageTypeData = await result.json() - const pageType = pageTypeData.data as GetContentTypeUidType - - const contentType = Object.values(pageType) - .map((val) => val.items[0]) - .find((item) => item?.__typename)?.__typename + const contentType = await getContentTypeByPathName(pathNameWithoutLang, lang) if (request.nextUrl.pathname.includes("preview")) { searchParams.set("uri", pathNameWithoutLang.replace("/preview", "")) @@ -85,6 +37,10 @@ export const middleware: NextMiddleware = async (request) => { return NextResponse.rewrite( new URL(`/${lang}/loyalty-page?${searchParams.toString()}`, nextUrl) ) + // case PageTypeEnum.ContentPage: + // return NextResponse.rewrite( + // new URL(`/${lang}/content-page?${searchParams.toString()}`, nextUrl) + // ) default: return NextResponse.next() } diff --git a/types/requests/contentTypeUid.ts b/types/requests/contentTypeUid.ts new file mode 100644 index 000000000..e8f358d07 --- /dev/null +++ b/types/requests/contentTypeUid.ts @@ -0,0 +1,11 @@ +export type GetContentTypeUidType = { + all_content_page: { + total: number + } + all_loyalty_page: { + total: number + } + all_current_blocks_page: { + total: number + } +} diff --git a/utils/contentType.ts b/utils/contentType.ts new file mode 100644 index 000000000..2d3668c90 --- /dev/null +++ b/utils/contentType.ts @@ -0,0 +1,45 @@ +import { DocumentNode } from "graphql" + +import { Lang } from "@/constants/languages" +import { env } from "@/env/server" +import GetContentTypeUid from "@/lib/graphql/Query/ContentTypeUid.graphql" + +import type { GetContentTypeUidType } from "@/types/requests/contentTypeUid" + +export enum PageTypeEnum { + CurrentBlocksPage = "CurrentBlocksPage", + LoyaltyPage = "LoyaltyPage", + ContentPage = "ContentPage", +} + +export async function getContentTypeByPathName( + pathNameWithoutLang: string, + lang = Lang.en +) { + const print = (await import("graphql/language/printer")).print + const result = await fetch(env.CMS_URL, { + method: "POST", + headers: { + access_token: env.CMS_ACCESS_TOKEN, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + query: print(GetContentTypeUid as DocumentNode), + variables: { + locale: lang, + url: pathNameWithoutLang, + }, + }), + }) + + const pageTypeData = await result.json() + const pageType = pageTypeData.data as GetContentTypeUidType + + if (pageType.all_content_page.total) { + return PageTypeEnum.ContentPage + } else if (pageType.all_loyalty_page.total) { + return PageTypeEnum.LoyaltyPage + } else if (pageType.all_current_blocks_page.total) { + return PageTypeEnum.CurrentBlocksPage + } +}