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

@@ -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
}