feat(SW-285): add support for shortcuts in content pages

This commit is contained in:
Chuma McPhoy
2024-08-30 11:16:36 +02:00
parent 9a51cc6cb5
commit 57cd8c72da
5 changed files with 71 additions and 3 deletions

View File

@@ -1,7 +1,7 @@
import JsonToHtml from "@/components/JsonToHtml"
// import DynamicContentBlock from "@/components/Loyalty/Blocks/DynamicContent"
// import Shortcuts from "@/components/MyPages/Blocks/Shortcuts"
import Shortcuts from "@/components/MyPages/Blocks/Shortcuts"
// import CardsGrid from "./CardsGrid"
import type { BlocksProps } from "@/types/components/content/blocks"
import { ContentBlocksTypenameEnum } from "@/types/components/content/enums"
@@ -19,6 +19,16 @@ export function Blocks({ blocks }: BlocksProps) {
/>
</section>
)
case ContentBlocksTypenameEnum.ContentPageBlocksShortcuts:
return (
<Shortcuts
firstItem={firstItem}
key={`${block.shortcuts.title}-${idx}`}
shortcuts={block.shortcuts.shortcuts}
subtitle={block.shortcuts.preamble}
title={block.shortcuts.title}
/>
)
default:
return null
}

View File

@@ -34,6 +34,27 @@ query GetContentPage($locale: String!, $uid: String!) {
}
}
}
... on ContentPageBlocksShortcuts {
__typename
shortcuts {
title
preamble
shortcuts {
open_in_new_tab
text
linkConnection {
totalCount
edges {
node {
...LoyaltyPageLink
...ContentPageLink
...AccountPageLink
}
}
}
}
}
}
}
title
header {

View File

@@ -24,8 +24,25 @@ const contentPageBlockTextContent = z.object({
}),
})
const contentPageShortcuts = z.object({
__typename: z.literal(ContentBlocksTypenameEnum.ContentPageBlocksShortcuts),
shortcuts: z.object({
title: z.string().nullable(),
preamble: z.string().nullable(),
shortcuts: z.array(
z.object({
text: z.string().optional(),
openInNewTab: z.boolean(),
url: z.string(),
title: z.string(),
})
),
}),
})
const contentPageBlockItem = z.discriminatedUnion("__typename", [
contentPageBlockTextContent,
contentPageShortcuts,
])
type BlockContentRaw = z.infer<typeof contentPageBlockTextContent>
@@ -38,7 +55,8 @@ export interface RteBlockContent extends BlockContentRaw {
}
}
export type Block = RteBlockContent
export type Shortcuts = z.infer<typeof contentPageShortcuts>
export type Block = RteBlockContent | Shortcuts
// Content Page Schema and types
export const validateContentPageSchema = z.object({

View File

@@ -6,6 +6,7 @@ import { contentstackExtendedProcedureUID, router } from "@/server/trpc"
import { generateTag } from "@/utils/generateTag"
import { makeImageVaultImage } from "@/utils/imageVault"
import { removeMultipleSlashes } from "@/utils/url"
import { removeEmptyObjects } from "../../utils"
import {
@@ -44,6 +45,23 @@ export const contentPageQueryRouter = router({
switch (block.__typename) {
case ContentBlocksTypenameEnum.ContentPageBlocksContent:
return block
case ContentBlocksTypenameEnum.ContentPageBlocksShortcuts:
return {
...block,
shortcuts: {
...block.shortcuts,
shortcuts: block.shortcuts.shortcuts.map((shortcut: any) => ({
text: shortcut.text,
openInNewTab: shortcut.open_in_new_tab,
...shortcut.linkConnection.edges[0].node,
url:
shortcut.linkConnection.edges[0].node.web?.original_url ||
removeMultipleSlashes(
`/${shortcut.linkConnection.edges[0].node.system.locale}/${shortcut.linkConnection.edges[0].node.url}`
),
})),
},
}
default:
return block
}

View File

@@ -1,3 +1,4 @@
export enum ContentBlocksTypenameEnum {
ContentPageBlocksContent = "ContentPageBlocksContent",
ContentPageBlocksShortcuts = "ContentPageBlocksShortcuts",
}