feat(WEB-209): revalidate my pages navigation on demand
This commit is contained in:
committed by
Michael Zetterberg
parent
16634abbbf
commit
1bffbc837e
123
server/routers/contentstack/myPages/navigation/query.ts
Normal file
123
server/routers/contentstack/myPages/navigation/query.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
import {
|
||||
GetNavigationMyPages,
|
||||
GetNavigationMyPagesRefs,
|
||||
} from "@/lib/graphql/Query/NavigationMyPages.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { badRequestError, internalServerError } from "@/server/errors/trpc"
|
||||
import { publicProcedure, router } from "@/server/trpc"
|
||||
|
||||
import {
|
||||
generateRefsResponseTag,
|
||||
generateTag,
|
||||
generateTags,
|
||||
} from "@/utils/generateTag"
|
||||
|
||||
import { getNavigationInputSchema } from "./input"
|
||||
import {
|
||||
getNavigationSchema,
|
||||
navigationPayloadSchema,
|
||||
navigationRefsPayloadSchema,
|
||||
} from "./output"
|
||||
import { getConnections } from "./utils"
|
||||
|
||||
import type {
|
||||
GetNavigationMyPagesData,
|
||||
GetNavigationMyPagesRefsData,
|
||||
MenuItem,
|
||||
NavigationItem,
|
||||
} from "@/types/requests/myPages/navigation"
|
||||
|
||||
export function mapMenuItems(navigationItems: NavigationItem[]) {
|
||||
return navigationItems.map(({ item }): MenuItem => {
|
||||
const { node } = item.pageConnection.edges[0]
|
||||
|
||||
const menuItem: MenuItem = {
|
||||
lang: node.system.locale,
|
||||
linkText: item.link_text || node.title,
|
||||
uid: node.system.uid,
|
||||
url: `/${node.system.locale}/${node.url}`.replaceAll(/\/\/+/g, "/"),
|
||||
}
|
||||
|
||||
if ("sub_items" in item) {
|
||||
menuItem.subItems = mapMenuItems(item.sub_items)
|
||||
}
|
||||
|
||||
return menuItem
|
||||
})
|
||||
}
|
||||
|
||||
export const navigationQueryRouter = router({
|
||||
get: publicProcedure.input(getNavigationInputSchema).query(async function ({
|
||||
input: lang,
|
||||
}) {
|
||||
try {
|
||||
const refsResponse = await request<GetNavigationMyPagesRefsData>(
|
||||
GetNavigationMyPagesRefs,
|
||||
{ locale: lang },
|
||||
{ tags: [generateRefsResponseTag(lang, "navigation_my_pages")] }
|
||||
)
|
||||
|
||||
if (!refsResponse.data) {
|
||||
console.error("Bad response for `GetNavigationMyPagesRefs`")
|
||||
console.error(refsResponse)
|
||||
throw internalServerError()
|
||||
}
|
||||
|
||||
const validatedMyPagesNavigationRefs =
|
||||
navigationRefsPayloadSchema.safeParse(refsResponse.data)
|
||||
if (!validatedMyPagesNavigationRefs.success) {
|
||||
console.error("Bad validation for `GetNavigationMyPagesRefs`")
|
||||
console.error(validatedMyPagesNavigationRefs.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
const connections = getConnections(validatedMyPagesNavigationRefs.data)
|
||||
const tags = generateTags(lang, connections)
|
||||
const navigation =
|
||||
validatedMyPagesNavigationRefs.data.all_navigation_my_pages.items[0]
|
||||
tags.push(generateTag(lang, navigation.system.uid))
|
||||
|
||||
const response = await request<GetNavigationMyPagesData>(
|
||||
GetNavigationMyPages,
|
||||
{ locale: lang },
|
||||
{ tags }
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
console.error("Bad response for `GetNavigationMyPages`")
|
||||
console.error(response)
|
||||
throw internalServerError()
|
||||
}
|
||||
|
||||
const validatedMyPagesNavigation = navigationPayloadSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
if (!validatedMyPagesNavigation.success) {
|
||||
console.error("Bad validation for `GetNavigationMyPages`")
|
||||
console.error(validatedMyPagesNavigation.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
const menuItem =
|
||||
validatedMyPagesNavigation.data.all_navigation_my_pages.items[0]
|
||||
|
||||
const nav = {
|
||||
items: mapMenuItems(menuItem.items),
|
||||
title: menuItem.title,
|
||||
}
|
||||
|
||||
const validatedNav = getNavigationSchema.safeParse(nav)
|
||||
if (!validatedNav.success) {
|
||||
console.error("Bad validation for `getNavigationSchema`")
|
||||
console.error(validatedNav.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
return validatedNav.data
|
||||
} catch (error) {
|
||||
console.info(`Get My Pages Navigation Error`)
|
||||
console.error(error)
|
||||
throw internalServerError()
|
||||
}
|
||||
}),
|
||||
})
|
||||
Reference in New Issue
Block a user