fix: break out content type uid fetch to utils function
This commit is contained in:
@@ -2,11 +2,9 @@ import { firaMono, firaSans } from "@/app/[lang]/(live)/fonts"
|
|||||||
|
|
||||||
import styles from "./layout.module.css"
|
import styles from "./layout.module.css"
|
||||||
|
|
||||||
import type { MyPagesLayoutProps } from "@/types/components/myPages/layout"
|
export default function LoyaltyPagesLayout({
|
||||||
|
|
||||||
export default async function LoyaltyPagesLayout({
|
|
||||||
children,
|
children,
|
||||||
}: React.PropsWithChildren<MyPagesLayoutProps>) {
|
}: React.PropsWithChildren) {
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={`${firaMono.variable} ${firaSans.variable} ${styles.layout}`}
|
className={`${firaMono.variable} ${firaSans.variable} ${styles.layout}`}
|
||||||
|
|||||||
@@ -1,17 +1,11 @@
|
|||||||
query GetContentTypeUid($locale: String!, $url: String!) {
|
query GetContentTypeUid($locale: String!, $url: String!) {
|
||||||
all_content_page(where: { url: $url, locale: $locale }) {
|
all_content_page(where: { url: $url, locale: $locale }) {
|
||||||
items {
|
total
|
||||||
__typename
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
all_current_blocks_page(where: { url: $url, locale: $locale }) {
|
all_current_blocks_page(where: { url: $url, locale: $locale }) {
|
||||||
items {
|
total
|
||||||
__typename
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
all_loyalty_page(where: { url: $url, locale: $locale }) {
|
all_loyalty_page(where: { url: $url, locale: $locale }) {
|
||||||
items {
|
total
|
||||||
__typename
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,40 +1,13 @@
|
|||||||
import { DocumentNode } from "graphql"
|
|
||||||
import { NextResponse } from "next/server"
|
import { NextResponse } from "next/server"
|
||||||
|
|
||||||
import { findLang } from "@/constants/languages"
|
import { findLang } from "@/constants/languages"
|
||||||
import { env } from "@/env/server"
|
|
||||||
import GetContentTypeUid from "@/lib/graphql/Query/ContentTypeUid.graphql"
|
import { getContentTypeByPathName, PageTypeEnum } from "@/utils/contentType"
|
||||||
|
|
||||||
import type { NextMiddleware } from "next/server"
|
import type { NextMiddleware } from "next/server"
|
||||||
|
|
||||||
import { MiddlewareMatcher } from "@/types/middleware"
|
import { MiddlewareMatcher } from "@/types/middleware"
|
||||||
|
|
||||||
enum PageTypeEnum {
|
|
||||||
CurrentBlocksPage = "CurrentBlocksPage",
|
|
||||||
LoyaltyPage = "LoyaltyPage",
|
|
||||||
ContentPage = "contentPage",
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetContentTypeUidType = {
|
|
||||||
all_content_page: {
|
|
||||||
items: {
|
|
||||||
__typename: PageTypeEnum.ContentPage
|
|
||||||
}[]
|
|
||||||
}
|
|
||||||
all_loyalty_page: {
|
|
||||||
items: {
|
|
||||||
__typename: PageTypeEnum.LoyaltyPage
|
|
||||||
}[]
|
|
||||||
}
|
|
||||||
all_current_blocks_page: {
|
|
||||||
items: {
|
|
||||||
__typename?: PageTypeEnum.CurrentBlocksPage
|
|
||||||
}[]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type PageType = keyof typeof PageTypeEnum
|
|
||||||
|
|
||||||
export const middleware: NextMiddleware = async (request) => {
|
export const middleware: NextMiddleware = async (request) => {
|
||||||
const { nextUrl } = request
|
const { nextUrl } = request
|
||||||
const lang = findLang(nextUrl.pathname)
|
const lang = findLang(nextUrl.pathname)
|
||||||
@@ -42,28 +15,7 @@ export const middleware: NextMiddleware = async (request) => {
|
|||||||
const pathNameWithoutLang = nextUrl.pathname.replace(`/${lang}`, "")
|
const pathNameWithoutLang = nextUrl.pathname.replace(`/${lang}`, "")
|
||||||
const searchParams = new URLSearchParams(request.nextUrl.searchParams)
|
const searchParams = new URLSearchParams(request.nextUrl.searchParams)
|
||||||
|
|
||||||
const print = (await import("graphql/language/printer")).print
|
const contentType = await getContentTypeByPathName(pathNameWithoutLang, lang)
|
||||||
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
|
|
||||||
|
|
||||||
if (request.nextUrl.pathname.includes("preview")) {
|
if (request.nextUrl.pathname.includes("preview")) {
|
||||||
searchParams.set("uri", pathNameWithoutLang.replace("/preview", ""))
|
searchParams.set("uri", pathNameWithoutLang.replace("/preview", ""))
|
||||||
@@ -85,6 +37,10 @@ export const middleware: NextMiddleware = async (request) => {
|
|||||||
return NextResponse.rewrite(
|
return NextResponse.rewrite(
|
||||||
new URL(`/${lang}/loyalty-page?${searchParams.toString()}`, nextUrl)
|
new URL(`/${lang}/loyalty-page?${searchParams.toString()}`, nextUrl)
|
||||||
)
|
)
|
||||||
|
// case PageTypeEnum.ContentPage:
|
||||||
|
// return NextResponse.rewrite(
|
||||||
|
// new URL(`/${lang}/content-page?${searchParams.toString()}`, nextUrl)
|
||||||
|
// )
|
||||||
default:
|
default:
|
||||||
return NextResponse.next()
|
return NextResponse.next()
|
||||||
}
|
}
|
||||||
|
|||||||
11
types/requests/contentTypeUid.ts
Normal file
11
types/requests/contentTypeUid.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
export type GetContentTypeUidType = {
|
||||||
|
all_content_page: {
|
||||||
|
total: number
|
||||||
|
}
|
||||||
|
all_loyalty_page: {
|
||||||
|
total: number
|
||||||
|
}
|
||||||
|
all_current_blocks_page: {
|
||||||
|
total: number
|
||||||
|
}
|
||||||
|
}
|
||||||
45
utils/contentType.ts
Normal file
45
utils/contentType.ts
Normal file
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user