feat(SW-285): add missing code for dynamic content
This commit is contained in:
@@ -7,6 +7,7 @@ import { imageVaultAssetSchema } from "../schemas/imageVault"
|
||||
import {
|
||||
CardsGridEnum,
|
||||
ContentBlocksTypenameEnum,
|
||||
DynamicContentComponentEnum,
|
||||
} from "@/types/components/content/enums"
|
||||
import { ImageVaultAsset } from "@/types/components/imageVault"
|
||||
import { Embeds } from "@/types/requests/embeds"
|
||||
@@ -45,7 +46,23 @@ const contentPageShortcuts = z.object({
|
||||
}),
|
||||
})
|
||||
|
||||
// TODO: this is a separate entity, should be in a separate file.
|
||||
const contentPageDynamicContent = z.object({
|
||||
__typename: z.literal(
|
||||
ContentBlocksTypenameEnum.ContentPageBlocksDynamicContent
|
||||
),
|
||||
dynamic_content: z.object({
|
||||
title: z.string().nullable(),
|
||||
subtitle: z.string().nullable(),
|
||||
component: z.nativeEnum(DynamicContentComponentEnum),
|
||||
link: z
|
||||
.object({
|
||||
text: z.string(),
|
||||
href: z.string(),
|
||||
})
|
||||
.optional(),
|
||||
}),
|
||||
})
|
||||
|
||||
const cardBlock = z.object({
|
||||
__typename: z.literal(CardsGridEnum.Card),
|
||||
heading: z.string().nullable(),
|
||||
@@ -111,10 +128,13 @@ const contentPageCards = z.object({
|
||||
|
||||
const contentPageBlockItem = z.discriminatedUnion("__typename", [
|
||||
contentPageBlockTextContent,
|
||||
contentPageShortcuts,
|
||||
contentPageCards,
|
||||
contentPageDynamicContent,
|
||||
contentPageShortcuts,
|
||||
])
|
||||
|
||||
export type DynamicContent = z.infer<typeof contentPageDynamicContent>
|
||||
|
||||
type BlockContentRaw = z.infer<typeof contentPageBlockTextContent>
|
||||
export interface RteBlockContent extends BlockContentRaw {
|
||||
content: {
|
||||
@@ -141,7 +161,7 @@ export type CardsGrid = Omit<CardsGridRaw, "cards"> & {
|
||||
}
|
||||
export type CardsRaw = CardsGrid["cards_grid"]["cards"][number]
|
||||
|
||||
export type Block = RteBlockContent | Shortcuts | CardsGrid
|
||||
export type Block = RteBlockContent | Shortcuts | CardsGrid | DynamicContent
|
||||
|
||||
// Content Page Schema and types
|
||||
export const validateContentPageSchema = z.object({
|
||||
@@ -246,7 +266,13 @@ const contentPageBlockTextContentRefs = z.object({
|
||||
const contentPageCardsRefs = z.object({
|
||||
__typename: z.literal(ContentBlocksTypenameEnum.ContentPageBlocksCardsGrid),
|
||||
cards_grid: z.object({
|
||||
cardConnection: cardGridCardsRef,
|
||||
cardConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: cardGridCardsRef,
|
||||
})
|
||||
),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -261,10 +287,22 @@ const contentPageShortcutsRefs = z.object({
|
||||
}),
|
||||
})
|
||||
|
||||
const contentPageDynamicContentRefs = z.object({
|
||||
__typename: z.literal(
|
||||
ContentBlocksTypenameEnum.ContentPageBlocksDynamicContent
|
||||
),
|
||||
dynamic_content: z.object({
|
||||
link: z.object({
|
||||
pageConnection: pageConnectionRefs,
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
const contentPageBlockRefsItem = z.discriminatedUnion("__typename", [
|
||||
contentPageBlockTextContentRefs,
|
||||
contentPageShortcutsRefs,
|
||||
contentPageCardsRefs,
|
||||
contentPageDynamicContentRefs,
|
||||
])
|
||||
|
||||
export const validateContentPageRefsSchema = z.object({
|
||||
|
||||
@@ -121,6 +121,24 @@ export const contentPageQueryRouter = router({
|
||||
),
|
||||
},
|
||||
}
|
||||
case ContentBlocksTypenameEnum.ContentPageBlocksDynamicContent:
|
||||
return {
|
||||
...block,
|
||||
dynamic_content: {
|
||||
...block.dynamic_content,
|
||||
link: block.dynamic_content.link.pageConnection.edges.length
|
||||
? {
|
||||
text: block.dynamic_content.link.text,
|
||||
href: removeMultipleSlashes(
|
||||
`/${block.dynamic_content.link.pageConnection.edges[0].node.system.locale}/${block.dynamic_content.link.pageConnection.edges[0].node.url}`
|
||||
),
|
||||
title:
|
||||
block.dynamic_content.link.pageConnection.edges[0]
|
||||
.node.title,
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
}
|
||||
default:
|
||||
return block
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user