feat(WEB-209): revalidate my pages navigation on demand
This commit is contained in:
committed by
Michael Zetterberg
parent
16634abbbf
commit
1bffbc837e
@@ -27,7 +27,6 @@ export const validateBreadcrumbsConstenstackSchema = z.object({
|
||||
}),
|
||||
})
|
||||
),
|
||||
total: z.number(),
|
||||
}),
|
||||
})
|
||||
|
||||
|
||||
@@ -4,10 +4,12 @@ import { accountPageRouter } from "./accountPage"
|
||||
import { breadcrumbsRouter } from "./breadcrumbs"
|
||||
import { contactConfigRouter } from "./contactConfig"
|
||||
import { loyaltyPageRouter } from "./loyaltyPage"
|
||||
import { myPagesRouter } from "./myPages"
|
||||
|
||||
export const contentstackRouter = router({
|
||||
breadcrumbs: breadcrumbsRouter,
|
||||
loyaltyPage: loyaltyPageRouter,
|
||||
accountPage: accountPageRouter,
|
||||
contactConfig: contactConfigRouter,
|
||||
loyaltyPage: loyaltyPageRouter,
|
||||
myPages: myPagesRouter,
|
||||
})
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
SidebarTypenameEnum,
|
||||
} from "@/types/components/loyalty/enums"
|
||||
import { Embeds } from "@/types/requests/embeds"
|
||||
import { Edges } from "@/types/requests/utils/edges"
|
||||
import { EdgesWithTotalCount } from "@/types/requests/utils/edges"
|
||||
import { RTEDocument } from "@/types/rte/node"
|
||||
|
||||
const loyaltyPageBlockCardGrid = z.object({
|
||||
@@ -216,7 +216,7 @@ export interface RteBlockContent extends BlockContentRaw {
|
||||
content: {
|
||||
content: {
|
||||
json: RTEDocument
|
||||
embedded_itemsConnection: Edges<Embeds>
|
||||
embedded_itemsConnection: EdgesWithTotalCount<Embeds>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -243,7 +243,7 @@ export type RteSidebarContent = Omit<SidebarContentRaw, "content"> & {
|
||||
content: {
|
||||
content: {
|
||||
json: RTEDocument
|
||||
embedded_itemsConnection: Edges<Embeds>
|
||||
embedded_itemsConnection: EdgesWithTotalCount<Embeds>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ export const loyaltyPageQueryRouter = router({
|
||||
)
|
||||
|
||||
if (!validatedLoyaltyPage.success) {
|
||||
console.error("Bad validation for `validatedLoyaltyPage`")
|
||||
console.error(validatedLoyaltyPage.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
6
server/routers/contentstack/myPages/index.ts
Normal file
6
server/routers/contentstack/myPages/index.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { router } from "@/server/trpc";
|
||||
import { navigationRouter } from "./navigation";
|
||||
|
||||
export const myPagesRouter = router({
|
||||
navigation: navigationRouter,
|
||||
})
|
||||
4
server/routers/contentstack/myPages/navigation/index.ts
Normal file
4
server/routers/contentstack/myPages/navigation/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { mergeRouters } from "@/server/trpc";
|
||||
import { navigationQueryRouter } from "./query";
|
||||
|
||||
export const navigationRouter = mergeRouters(navigationQueryRouter)
|
||||
5
server/routers/contentstack/myPages/navigation/input.ts
Normal file
5
server/routers/contentstack/myPages/navigation/input.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { Lang } from "@/constants/languages";
|
||||
|
||||
export const getNavigationInputSchema = z.nativeEnum(Lang)
|
||||
127
server/routers/contentstack/myPages/navigation/output.ts
Normal file
127
server/routers/contentstack/myPages/navigation/output.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
import { PageLinkEnum } from "@/types/requests/myPages/navigation"
|
||||
|
||||
const pageConnection = z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
__typename: z.nativeEnum(PageLinkEnum),
|
||||
system: z.object({
|
||||
locale: z.nativeEnum(Lang),
|
||||
uid: z.string(),
|
||||
}),
|
||||
title: z.string(),
|
||||
url: z.string(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
})
|
||||
|
||||
const pageConnectionRefs = z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
__typename: z.nativeEnum(PageLinkEnum),
|
||||
system: z.object({
|
||||
content_type_uid: z.string(),
|
||||
uid: z.string(),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
),
|
||||
})
|
||||
|
||||
export const navigationRefsPayloadSchema = z.object({
|
||||
all_navigation_my_pages: z.object({
|
||||
items: z
|
||||
.array(
|
||||
z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
__typename: z.string(),
|
||||
item: z.object({
|
||||
sub_items: z.array(
|
||||
z.object({
|
||||
__typename: z.string(),
|
||||
item: z.object({
|
||||
pageConnection: pageConnectionRefs,
|
||||
}),
|
||||
})
|
||||
),
|
||||
pageConnection: pageConnectionRefs,
|
||||
}),
|
||||
})
|
||||
),
|
||||
system: z.object({
|
||||
content_type_uid: z.string(),
|
||||
uid: z.string(),
|
||||
}),
|
||||
})
|
||||
)
|
||||
.refine(
|
||||
(input) => {
|
||||
return input.length === 1
|
||||
},
|
||||
{
|
||||
message: `Expected navigationRefsPayloadSchema 1 all_navigation_my_pages item`,
|
||||
}
|
||||
),
|
||||
}),
|
||||
})
|
||||
|
||||
export const navigationPayloadSchema = z.object({
|
||||
all_navigation_my_pages: z.object({
|
||||
items: z
|
||||
.array(
|
||||
z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
item: z.object({
|
||||
link_text: z.string().default(""),
|
||||
pageConnection,
|
||||
sub_items: z.array(
|
||||
z.object({
|
||||
item: z.object({
|
||||
link_text: z.string().default(""),
|
||||
pageConnection,
|
||||
}),
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
),
|
||||
title: z.string(),
|
||||
})
|
||||
)
|
||||
.refine(
|
||||
(input) => {
|
||||
return input.length === 1
|
||||
},
|
||||
{
|
||||
message: `Expected navigationPayloadSchema to containt 1 all_navigation_my_pages item`,
|
||||
}
|
||||
),
|
||||
}),
|
||||
})
|
||||
|
||||
const baseMenuItem = z.object({
|
||||
lang: z.nativeEnum(Lang),
|
||||
linkText: z.string(),
|
||||
uid: z.string(),
|
||||
url: z.string(),
|
||||
originalUrl: z.string().optional(),
|
||||
})
|
||||
|
||||
export const getNavigationSchema = z.object({
|
||||
items: z.array(
|
||||
z
|
||||
.object({
|
||||
subItems: z.array(baseMenuItem),
|
||||
})
|
||||
.merge(baseMenuItem)
|
||||
),
|
||||
title: z.string(),
|
||||
})
|
||||
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()
|
||||
}
|
||||
}),
|
||||
})
|
||||
18
server/routers/contentstack/myPages/navigation/utils.ts
Normal file
18
server/routers/contentstack/myPages/navigation/utils.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import type { GetNavigationMyPagesRefsData } from "@/types/requests/myPages/navigation"
|
||||
import type { Edges } from "@/types/requests/utils/edges"
|
||||
import type { NodeRefs } from "@/types/requests/utils/refs"
|
||||
|
||||
export function getConnections(refs: GetNavigationMyPagesRefsData) {
|
||||
const connections: Edges<NodeRefs>[] = []
|
||||
refs.all_navigation_my_pages.items.forEach((ref) => {
|
||||
ref.items.forEach(({ item }) => {
|
||||
connections.push(item.pageConnection)
|
||||
|
||||
item.sub_items.forEach(({ item: subItem }) => {
|
||||
connections.push(subItem.pageConnection)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
return connections
|
||||
}
|
||||
Reference in New Issue
Block a user