feat(WEB-209): revalidate my pages navigation on demand
This commit is contained in:
committed by
Michael Zetterberg
parent
16634abbbf
commit
1bffbc837e
@@ -1,30 +1,44 @@
|
||||
import { revalidateTag } from "next/cache"
|
||||
import { headers } from "next/headers"
|
||||
import { NextRequest } from "next/server"
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
import { env } from "@/env/server"
|
||||
import { internalServerError } from "@/server/errors/next"
|
||||
|
||||
import {
|
||||
generateRefsResponseTag,
|
||||
generateRefTag,
|
||||
generateTag,
|
||||
} from "@/utils/generateTag"
|
||||
|
||||
const validateJsonBody = z.object({
|
||||
api_key: z.string(),
|
||||
module: z.string(),
|
||||
|
||||
data: z.object({
|
||||
content_type: z.object({
|
||||
uid: z.string(),
|
||||
}),
|
||||
entry: z.object({
|
||||
locale: z.nativeEnum(Lang),
|
||||
uid: z.string(),
|
||||
url: z.string().optional(),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const secret = request.nextUrl.searchParams.get("secret") ?? ""
|
||||
const tagsParam = request.nextUrl.searchParams.get("tags") ?? ""
|
||||
const headersList = headers()
|
||||
const secret = headersList.get("x-revalidate-secret")
|
||||
|
||||
if (secret !== env.REVALIDATE_SECRET) {
|
||||
console.error(`Invalid Secret`)
|
||||
console.error({ secret })
|
||||
return Response.json(
|
||||
{
|
||||
message: "Invalid secret",
|
||||
now: Date.now(),
|
||||
revalidated: false,
|
||||
},
|
||||
{
|
||||
status: 401,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
if (!tagsParam) {
|
||||
return Response.json(
|
||||
{
|
||||
message: "Missing tags param",
|
||||
now: Date.now(),
|
||||
revalidated: false,
|
||||
},
|
||||
@@ -34,31 +48,37 @@ export async function POST(request: NextRequest) {
|
||||
)
|
||||
}
|
||||
|
||||
const tags = tagsParam.split(",")
|
||||
if (!tags.length) {
|
||||
return Response.json(
|
||||
{
|
||||
message: "No tags",
|
||||
now: Date.now(),
|
||||
revalidated: false,
|
||||
},
|
||||
{
|
||||
status: 400,
|
||||
}
|
||||
)
|
||||
const data = await request.json()
|
||||
const validatedData = validateJsonBody.safeParse(data)
|
||||
if (!validatedData.success) {
|
||||
console.error("Bad validation for `validatedData`")
|
||||
console.error(validatedData.error)
|
||||
return internalServerError({ revalidated: false, now: Date.now() })
|
||||
}
|
||||
|
||||
tags.forEach((tag) => {
|
||||
revalidateTag(tag)
|
||||
})
|
||||
const {
|
||||
data: {
|
||||
data: { content_type, entry },
|
||||
},
|
||||
} = validatedData
|
||||
const identifier = entry.url ?? content_type.uid
|
||||
const refsTag = generateRefsResponseTag(entry.locale, identifier)
|
||||
const refTag = generateRefTag(entry.locale, content_type.uid, entry.uid)
|
||||
const tag = generateTag(entry.locale, entry.uid)
|
||||
|
||||
console.info(`Revalidating refsTag: ${refsTag}`)
|
||||
revalidateTag(refsTag)
|
||||
|
||||
console.info(`Revalidating refTag: ${refTag}`)
|
||||
revalidateTag(refTag)
|
||||
|
||||
console.info(`Revalidating tag: ${tag}`)
|
||||
revalidateTag(tag)
|
||||
|
||||
return Response.json({ revalidated: true, now: Date.now() })
|
||||
} catch (error) {
|
||||
console.info("Failed to revalidate tag(s)")
|
||||
console.error(error)
|
||||
return Response.json(
|
||||
{ revalidated: false, now: Date.now() },
|
||||
{ status: 500 }
|
||||
)
|
||||
return internalServerError({ revalidated: false, now: Date.now() })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
import type {
|
||||
MenuItem,
|
||||
NavigationItem,
|
||||
} from "@/types/requests/myPages/navigation"
|
||||
|
||||
export function mapMenuItems(navigationItems: NavigationItem[]) {
|
||||
return navigationItems.map(({ item }): MenuItem => {
|
||||
const { node } = item.pageConnection.edges[0]
|
||||
|
||||
return {
|
||||
linkText: item.link_text || node.title,
|
||||
lang: node.system.locale,
|
||||
subItems: item.sub_items ? mapMenuItems(item.sub_items) : null,
|
||||
uid: node.system.uid,
|
||||
url: `/${node.system.locale}/${node.url}`.replaceAll(/\/\/+/g, "/"),
|
||||
originalUrl: node.web?.original_url,
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1,38 +1,25 @@
|
||||
import { Fragment } from "react"
|
||||
import { LogOut } from "react-feather"
|
||||
|
||||
import { GetNavigationMyPages } from "@/lib/graphql/Query/NavigationMyPages.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { serverClient } from "@/lib/trpc/server"
|
||||
|
||||
import Link from "@/components/TempDesignSystem/Link"
|
||||
import Title from "@/components/Title"
|
||||
|
||||
import { mapMenuItems } from "./helpers"
|
||||
|
||||
import styles from "./sidebar.module.css"
|
||||
|
||||
import type {
|
||||
GetNavigationMyPagesData,
|
||||
SidebarProps,
|
||||
} from "@/types/requests/myPages/navigation"
|
||||
import type { SidebarProps } from "@/types/requests/myPages/navigation"
|
||||
|
||||
export default async function Sidebar({ lang }: SidebarProps) {
|
||||
const response = await request<GetNavigationMyPagesData>(
|
||||
GetNavigationMyPages,
|
||||
{
|
||||
locale: lang,
|
||||
}
|
||||
)
|
||||
// navigation_my_pages is of type Single, hence the hard [0]
|
||||
const navigation = response.data.all_navigation_my_pages.items[0]
|
||||
const menuItems = mapMenuItems(navigation.items)
|
||||
const navigation =
|
||||
await serverClient().contentstack.myPages.navigation.get(lang)
|
||||
return (
|
||||
<aside className={styles.sidebar}>
|
||||
<nav className={styles.nav}>
|
||||
<Title level="h5" uppercase>
|
||||
{navigation.title}
|
||||
</Title>
|
||||
{menuItems.map((item) => (
|
||||
{navigation.items.map((item) => (
|
||||
<Fragment key={item.uid}>
|
||||
<Link
|
||||
href={item.originalUrl || item.url}
|
||||
@@ -42,18 +29,16 @@ export default async function Sidebar({ lang }: SidebarProps) {
|
||||
{item.linkText}
|
||||
</Link>
|
||||
{item.subItems
|
||||
? item.subItems.map((subItem) => {
|
||||
return (
|
||||
<Link
|
||||
key={subItem.uid}
|
||||
href={subItem.originalUrl || subItem.url}
|
||||
partialMatch
|
||||
variant="sidebar"
|
||||
>
|
||||
{subItem.linkText}
|
||||
</Link>
|
||||
)
|
||||
})
|
||||
? item.subItems.map((subItem) => (
|
||||
<Link
|
||||
key={subItem.uid}
|
||||
href={subItem.originalUrl || subItem.url}
|
||||
partialMatch
|
||||
variant="sidebar"
|
||||
>
|
||||
{subItem.linkText}
|
||||
</Link>
|
||||
))
|
||||
: null}
|
||||
</Fragment>
|
||||
))}
|
||||
|
||||
@@ -85,7 +85,7 @@ export default function FormContent({ control }: EditFormContentProps) {
|
||||
<HouseIcon />
|
||||
</Field.Icon>
|
||||
<Field.Label htmlFor="address.streetAddress">
|
||||
*{_("Address")}
|
||||
{_("Address")}
|
||||
</Field.Label>
|
||||
<Field.Content>
|
||||
<Input
|
||||
@@ -93,7 +93,6 @@ export default function FormContent({ control }: EditFormContentProps) {
|
||||
control={control}
|
||||
name="address.streetAddress"
|
||||
placeholder={_("Street 123")}
|
||||
registerOptions={{ required: true }}
|
||||
/>
|
||||
</Field.Content>
|
||||
</Field>
|
||||
@@ -102,14 +101,13 @@ export default function FormContent({ control }: EditFormContentProps) {
|
||||
<Field.Icon>
|
||||
<HouseIcon />
|
||||
</Field.Icon>
|
||||
<Field.Label htmlFor="address.city">*{_("City/State")}</Field.Label>
|
||||
<Field.Label htmlFor="address.city">{_("City/State")}</Field.Label>
|
||||
<Field.Content>
|
||||
<Input
|
||||
aria-label={_("City")}
|
||||
control={control}
|
||||
name="address.city"
|
||||
placeholder={_("City")}
|
||||
registerOptions={{ required: true }}
|
||||
/>
|
||||
</Field.Content>
|
||||
</Field>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
fragment Breadcrumbs on AccountPage {
|
||||
fragment MyPagesBreadcrumbs on AccountPage {
|
||||
breadcrumbs {
|
||||
title
|
||||
parents: parentsConnection {
|
||||
|
||||
7
lib/graphql/Fragments/Refs/AccountPage.graphql
Normal file
7
lib/graphql/Fragments/Refs/AccountPage.graphql
Normal file
@@ -0,0 +1,7 @@
|
||||
#import "./System.graphql"
|
||||
|
||||
fragment AccountPageRef on AccountPage {
|
||||
system {
|
||||
...System
|
||||
}
|
||||
}
|
||||
7
lib/graphql/Fragments/Refs/ContentPage.graphql
Normal file
7
lib/graphql/Fragments/Refs/ContentPage.graphql
Normal file
@@ -0,0 +1,7 @@
|
||||
#import "./System.graphql"
|
||||
|
||||
fragment ContentPageRef on ContentPage {
|
||||
system {
|
||||
...System
|
||||
}
|
||||
}
|
||||
7
lib/graphql/Fragments/Refs/LoyaltyPage.graphql
Normal file
7
lib/graphql/Fragments/Refs/LoyaltyPage.graphql
Normal file
@@ -0,0 +1,7 @@
|
||||
#import "./System.graphql"
|
||||
|
||||
fragment LoyaltyPageRef on LoyaltyPage {
|
||||
system {
|
||||
...System
|
||||
}
|
||||
}
|
||||
4
lib/graphql/Fragments/Refs/System.graphql
Normal file
4
lib/graphql/Fragments/Refs/System.graphql
Normal file
@@ -0,0 +1,4 @@
|
||||
fragment System on EntrySystemField {
|
||||
content_type_uid
|
||||
uid
|
||||
}
|
||||
@@ -3,11 +3,10 @@
|
||||
query GetMyPagesBreadcrumbs($locale: String!, $url: String!) {
|
||||
all_account_page(locale: $locale, where: { url: $url }) {
|
||||
items {
|
||||
...Breadcrumbs
|
||||
...MyPagesBreadcrumbs
|
||||
system {
|
||||
uid
|
||||
}
|
||||
}
|
||||
total
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
#import "../Fragments/PageLink/AccountPageLink.graphql"
|
||||
#import "../Fragments/PageLink/ContentPageLink.graphql"
|
||||
#import "../Fragments/PageLink/LoyaltyPageLink.graphql"
|
||||
#import "../Fragments/Refs/AccountPage.graphql"
|
||||
#import "../Fragments/Refs/ContentPage.graphql"
|
||||
#import "../Fragments/Refs/LoyaltyPage.graphql"
|
||||
#import "../Fragments/Refs/System.graphql"
|
||||
|
||||
query GetNavigationMyPages($locale: String!) {
|
||||
all_navigation_my_pages(locale: $locale) {
|
||||
all_navigation_my_pages(locale: $locale, limit: 1) {
|
||||
items {
|
||||
items {
|
||||
... on NavigationMyPagesItemsItem {
|
||||
@@ -20,8 +24,8 @@ query GetNavigationMyPages($locale: String!) {
|
||||
node {
|
||||
__typename
|
||||
...AccountPageLink
|
||||
...LoyaltyPageLink
|
||||
...ContentPageLink
|
||||
...LoyaltyPageLink
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,8 +37,8 @@ query GetNavigationMyPages($locale: String!) {
|
||||
node {
|
||||
__typename
|
||||
...AccountPageLink
|
||||
...LoyaltyPageLink
|
||||
...ContentPageLink
|
||||
...LoyaltyPageLink
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,3 +49,47 @@ query GetNavigationMyPages($locale: String!) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
query GetNavigationMyPagesRefs($locale: String!) {
|
||||
all_navigation_my_pages(locale: $locale, limit: 1) {
|
||||
items {
|
||||
items {
|
||||
... on NavigationMyPagesItemsItem {
|
||||
__typename
|
||||
item {
|
||||
sub_items {
|
||||
... on NavigationMyPagesItemsItemBlockSubItemsItem {
|
||||
__typename
|
||||
item {
|
||||
pageConnection {
|
||||
edges {
|
||||
node {
|
||||
__typename
|
||||
...AccountPageRef
|
||||
...ContentPageRef
|
||||
...LoyaltyPageRef
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pageConnection {
|
||||
edges {
|
||||
node {
|
||||
__typename
|
||||
...AccountPageRef
|
||||
...ContentPageRef
|
||||
...LoyaltyPageRef
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
system {
|
||||
...System
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,6 +66,7 @@ export async function request<T>(
|
||||
|
||||
return { data: response }
|
||||
} catch (error) {
|
||||
console.info(`GraphQL Request Error`)
|
||||
console.error(error)
|
||||
throw new Error("Something went wrong")
|
||||
}
|
||||
|
||||
@@ -1,13 +1,27 @@
|
||||
import { NextResponse } from "next/server"
|
||||
|
||||
export function badRequest() {
|
||||
return new NextResponse("Bad request", {
|
||||
export function badRequest(body: unknown | string = "Bad request") {
|
||||
const resInit = {
|
||||
status: 400,
|
||||
})
|
||||
}
|
||||
|
||||
if (typeof body === "string") {
|
||||
return new NextResponse(body, resInit)
|
||||
}
|
||||
|
||||
return NextResponse.json(body, resInit)
|
||||
}
|
||||
|
||||
export function internalServerError() {
|
||||
return new NextResponse("Internal Server Error", {
|
||||
export function internalServerError(
|
||||
body: unknown | string = "Internal Server Error"
|
||||
) {
|
||||
const resInit = {
|
||||
status: 500,
|
||||
})
|
||||
}
|
||||
|
||||
if (typeof body === "string") {
|
||||
return new NextResponse(body, resInit)
|
||||
}
|
||||
|
||||
return NextResponse.json(body, resInit)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { Edges } from "../utils/edges"
|
||||
import type { Lang } from "@/constants/languages"
|
||||
import type { EdgesWithTotalCount } from "../utils/edges"
|
||||
import type { Typename } from "../utils/typename"
|
||||
|
||||
export enum Section {
|
||||
@@ -75,6 +75,6 @@ export type ContactNode = {
|
||||
|
||||
export type Contact = {
|
||||
contact: {
|
||||
contactConnection: Edges<ContactNode>
|
||||
contactConnection: EdgesWithTotalCount<ContactNode>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import type { Edges } from "../utils/edges"
|
||||
import type { Embeds } from "../embeds"
|
||||
import type { RTEDocument } from "@/types/rte/node"
|
||||
import type { Embeds } from "../embeds"
|
||||
import type { EdgesWithTotalCount } from "../utils/edges"
|
||||
|
||||
export type Text = {
|
||||
text: {
|
||||
content: {
|
||||
embedded_itemsConnection: Edges<Embeds>
|
||||
embedded_itemsConnection: EdgesWithTotalCount<Embeds>
|
||||
json: RTEDocument
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,11 +7,8 @@ import type { List } from "./blocks/list"
|
||||
import type { PuffBlock } from "./blocks/puff"
|
||||
import type { Preamble } from "./preamble"
|
||||
import type { Text } from "./blocks/text"
|
||||
import type { AllRequestResponse } from "./utils/all"
|
||||
import type {
|
||||
AsideTypenameEnum,
|
||||
Typename,
|
||||
} from "./utils/typename"
|
||||
import type { AllRequestResponseWithTotal } from "./utils/all"
|
||||
import type { AsideTypenameEnum, Typename } from "./utils/typename"
|
||||
|
||||
export type Asides =
|
||||
| Typename<Contact, AsideTypenameEnum.CurrentBlocksPageAsideContact>
|
||||
@@ -48,5 +45,5 @@ export type BlockPage = {
|
||||
}
|
||||
|
||||
export type GetCurrentBlockPageData = {
|
||||
all_current_blocks_page: AllRequestResponse<BlockPage>
|
||||
all_current_blocks_page: AllRequestResponseWithTotal<BlockPage>
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import type { AllRequestResponse } from "./utils/all"
|
||||
import type { Edges } from "./utils/edges"
|
||||
import type { Image } from "../image"
|
||||
import type { AllRequestResponse } from "./utils/all"
|
||||
import type { EdgesWithTotalCount } from "./utils/edges"
|
||||
|
||||
type AppDownload = {
|
||||
href: string
|
||||
imageConnection: Edges<Image>
|
||||
imageConnection: EdgesWithTotalCount<Image>
|
||||
}
|
||||
|
||||
export type Link = {
|
||||
@@ -33,7 +33,7 @@ export type Footer = {
|
||||
app_store: AppDownload
|
||||
google_play: AppDownload
|
||||
}
|
||||
logoConnection: Edges<Image>
|
||||
logoConnection: EdgesWithTotalCount<Image>
|
||||
navigation: NavigationItem[]
|
||||
social_media: {
|
||||
title: string
|
||||
@@ -43,7 +43,7 @@ export type Footer = {
|
||||
}
|
||||
trip_advisor: {
|
||||
title: string
|
||||
logoConnection: Edges<Image>
|
||||
logoConnection: EdgesWithTotalCount<Image>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { Edges } from "./utils/edges"
|
||||
import type { Image } from "../image"
|
||||
import type { EdgesWithTotalCount } from "./utils/edges"
|
||||
|
||||
export type HeaderLink = {
|
||||
href: string
|
||||
@@ -27,7 +27,7 @@ export type HeaderQueryData = {
|
||||
all_header: {
|
||||
items: {
|
||||
frontpage_link_text: string
|
||||
logoConnection: Edges<Image>
|
||||
logoConnection: EdgesWithTotalCount<Image>
|
||||
menu: HeaderLinks
|
||||
top_menu: TopMenuHeaderLinks
|
||||
}[]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Image } from "../image"
|
||||
import type { Edges } from "./utils/edges"
|
||||
import type { EdgesWithTotalCount } from "./utils/edges"
|
||||
|
||||
export type Hero = {
|
||||
imagesConnection: Edges<Image>
|
||||
imagesConnection: EdgesWithTotalCount<Image>
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import type { AllRequestResponse } from "../utils/all"
|
||||
import type { Edges } from "../utils/edges"
|
||||
import type { EdgesWithTotalCount } from "../utils/edges"
|
||||
|
||||
interface AccountPageBreadcrumbs {
|
||||
breadcrumbs: {
|
||||
title: string
|
||||
parents: Edges<{
|
||||
parents: EdgesWithTotalCount<{
|
||||
breadcrumbs: {
|
||||
title: string
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import type { Edges } from "../utils/edges"
|
||||
import type { Image } from "../../image"
|
||||
import type { EdgesWithTotalCount } from "../utils/edges"
|
||||
|
||||
export type LogoQueryData = {
|
||||
all_header: {
|
||||
items: {
|
||||
logoConnection: Edges<Image>
|
||||
logoConnection: EdgesWithTotalCount<Image>
|
||||
}[]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { Lang } from "@/constants/languages"
|
||||
import type { System } from "../system"
|
||||
import type { AllRequestResponse } from "../utils/all"
|
||||
import type { Edges } from "../utils/edges"
|
||||
import type { TypenameInterface } from "../utils/typename"
|
||||
@@ -12,10 +13,10 @@ export enum PageLinkEnum {
|
||||
export type MenuItem = {
|
||||
lang: Lang
|
||||
linkText: string
|
||||
subItems: MenuItem[] | null
|
||||
uid: string
|
||||
url: string
|
||||
originalUrl: string | undefined
|
||||
originalUrl?: string
|
||||
subItems?: MenuItem[]
|
||||
}
|
||||
|
||||
export type SidebarProps = {
|
||||
@@ -27,8 +28,8 @@ interface NavigationLink {
|
||||
locale: Lang
|
||||
uid: string
|
||||
}
|
||||
url: string
|
||||
title: string
|
||||
url: string
|
||||
web?: { original_url: string }
|
||||
}
|
||||
|
||||
@@ -46,16 +47,44 @@ export interface ContentPageLink
|
||||
|
||||
export type PageLink = ContentPageLink | AccountPageLink | LoyaltyPageLink
|
||||
|
||||
export type NavigationItem = {
|
||||
item: {
|
||||
pageConnection: Edges<PageLink>
|
||||
link_text: string
|
||||
sub_items: NavigationItem[] | null
|
||||
}
|
||||
interface Item {
|
||||
link_text: string
|
||||
pageConnection: Edges<PageLink>
|
||||
}
|
||||
|
||||
export type NavigationMyPages = { items: NavigationItem[]; title: string }
|
||||
interface ItemWithSubitem extends Item {
|
||||
sub_items: NavigationItem[]
|
||||
}
|
||||
|
||||
export type NavigationItem = {
|
||||
item: Item | ItemWithSubitem
|
||||
}
|
||||
|
||||
export type NavigationMyPages = {
|
||||
items: NavigationItem[]
|
||||
title: string
|
||||
}
|
||||
|
||||
export type GetNavigationMyPagesData = {
|
||||
all_navigation_my_pages: AllRequestResponse<NavigationMyPages>
|
||||
}
|
||||
|
||||
/** Refs Request */
|
||||
type NavigationItemRef = {
|
||||
item: {
|
||||
pageConnection: Edges<System>
|
||||
sub_items: {
|
||||
item: {
|
||||
pageConnection: Edges<System>
|
||||
}
|
||||
}[]
|
||||
}
|
||||
}
|
||||
|
||||
interface NavigationMyPagesRefs extends System {
|
||||
items: NavigationItemRef[]
|
||||
}
|
||||
|
||||
export type GetNavigationMyPagesRefsData = {
|
||||
all_navigation_my_pages: AllRequestResponse<NavigationMyPagesRefs>
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import type { Edges } from "./utils/edges"
|
||||
import type { RTEDocument } from "../rte/node"
|
||||
import type { Embeds } from "./embeds"
|
||||
import type { EdgesWithTotalCount } from "./utils/edges"
|
||||
|
||||
export type Preamble = {
|
||||
text: {
|
||||
embedded_itemsConnection: Edges<Embeds>
|
||||
embedded_itemsConnection: EdgesWithTotalCount<Embeds>
|
||||
json: RTEDocument
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Image } from "../image"
|
||||
import type { Edges } from "./utils/edges"
|
||||
import type { RTEDocument } from "../rte/node"
|
||||
import type { EdgesWithTotalCount } from "./utils/edges"
|
||||
|
||||
export enum PuffStyleEnum {
|
||||
button = "button",
|
||||
@@ -8,7 +8,7 @@ export enum PuffStyleEnum {
|
||||
}
|
||||
|
||||
export type Puff = {
|
||||
imageConnection: Edges<Image>
|
||||
imageConnection: EdgesWithTotalCount<Image>
|
||||
link: {
|
||||
href: string
|
||||
title?: string
|
||||
|
||||
6
types/requests/system.ts
Normal file
6
types/requests/system.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export interface System {
|
||||
system: {
|
||||
content_type_uid: string
|
||||
uid: string
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
export interface AllRequestResponse<T> {
|
||||
items: T[]
|
||||
}
|
||||
|
||||
export interface AllRequestResponseWithTotal<T> {
|
||||
items: T[]
|
||||
total: number
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,5 +4,9 @@ export type Node<T> = {
|
||||
|
||||
export type Edges<T> = {
|
||||
edges: Node<T>[]
|
||||
}
|
||||
|
||||
export type EdgesWithTotalCount<T> = {
|
||||
edges: Node<T>[]
|
||||
totalCount: number
|
||||
}
|
||||
|
||||
3
types/requests/utils/refs.ts
Normal file
3
types/requests/utils/refs.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import type { System } from "../system"
|
||||
|
||||
export interface NodeRefs extends System {}
|
||||
@@ -38,6 +38,7 @@ export async function getContentTypeByPathName(
|
||||
)
|
||||
|
||||
if (!validatedContentTypeUid.success) {
|
||||
console.error("Bad validation for `validatedContentTypeUid`")
|
||||
console.error(validatedContentTypeUid.error)
|
||||
return null
|
||||
}
|
||||
|
||||
56
utils/generateTag.ts
Normal file
56
utils/generateTag.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import type { Edges } from "@/types/requests/utils/edges"
|
||||
import type { Lang } from "@/constants/languages"
|
||||
import type { NodeRefs } from "@/types/requests/utils/refs"
|
||||
|
||||
/**
|
||||
* Function to generate tag for initial refs request
|
||||
*
|
||||
* @param lang
|
||||
* @param identifier Should be uri for all pages and content_type_uid for
|
||||
* everything else
|
||||
* @returns string
|
||||
*/
|
||||
export function generateRefsResponseTag(lang: Lang, identifier: string) {
|
||||
return `${lang}:${identifier}:refs`
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to generate all tags to references on entity
|
||||
*
|
||||
* @param lang Lang
|
||||
* @param contentTypeUid content_type_uid of reference
|
||||
* @param uid system.uid of reference
|
||||
* @returns string
|
||||
*/
|
||||
export function generateRefTag(
|
||||
lang: Lang,
|
||||
contentTypeUid: string,
|
||||
uid: string
|
||||
) {
|
||||
return `${lang}:ref:${contentTypeUid}:${uid}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to generate tag for entity being requested
|
||||
*
|
||||
* @param lang Lang
|
||||
* @param uid system.uid of entity
|
||||
* @returns string
|
||||
*/
|
||||
export function generateTag(lang: Lang, uid: string) {
|
||||
return `${lang}:${uid}`
|
||||
}
|
||||
|
||||
export function generateTags(lang: Lang, connections: Edges<NodeRefs>[]) {
|
||||
return connections
|
||||
.map((connection) => {
|
||||
return connection.edges.map(({ node }) => {
|
||||
return generateRefTag(
|
||||
lang,
|
||||
node.system.content_type_uid,
|
||||
node.system.uid
|
||||
)
|
||||
})
|
||||
})
|
||||
.flat()
|
||||
}
|
||||
Reference in New Issue
Block a user