feat(SW-285): Add support for sidebar

This commit is contained in:
Chuma McPhoy
2024-09-02 22:43:08 +02:00
parent 60636d8cbe
commit a444746ccb
14 changed files with 594 additions and 1 deletions

View File

@@ -8,6 +8,9 @@ import {
CardsGridEnum,
ContentBlocksTypenameEnum,
DynamicContentComponentEnum,
JoinLoyaltyContactTypenameEnum,
SidebarDynamicComponentEnum,
SidebarTypenameEnum,
} from "@/types/components/content/enums"
import { ImageVaultAsset } from "@/types/components/imageVault"
import { Embeds } from "@/types/requests/embeds"
@@ -133,6 +136,62 @@ const contentPageBlockItem = z.discriminatedUnion("__typename", [
contentPageShortcuts,
])
const contentPageSidebarTextContent = z.object({
__typename: z.literal(SidebarTypenameEnum.ContentPageSidebarContent),
content: z.object({
content: z.object({
embedded_itemsConnection: z.object({
edges: z.array(z.any()),
totalCount: z.number(),
}),
json: z.any(),
}),
}),
})
const contentPageJoinLoyaltyContact = z.object({
__typename: z.literal(
SidebarTypenameEnum.ContentPageSidebarJoinLoyaltyContact
),
join_loyalty_contact: z.object({
title: z.string().nullable(),
preamble: z.string().nullable(),
button: z
.object({
openInNewTab: z.boolean(),
title: z.string(),
href: z.string(),
isExternal: z.boolean(),
})
.nullable(),
contact: z.array(
z.object({
__typename: z.literal(
JoinLoyaltyContactTypenameEnum.ContentPageSidebarJoinLoyaltyContactBlockContactContact
),
contact: z.object({
display_text: z.string().nullable(),
contact_field: z.string(),
footnote: z.string().nullable(),
}),
})
),
}),
})
const contentPageSidebarDynamicContent = z.object({
__typename: z.literal(SidebarTypenameEnum.ContentPageSidebarDynamicContent),
dynamic_content: z.object({
component: z.nativeEnum(SidebarDynamicComponentEnum),
}),
})
const contentPageSidebarItem = z.discriminatedUnion("__typename", [
contentPageSidebarTextContent,
contentPageSidebarDynamicContent,
contentPageJoinLoyaltyContact,
])
export type DynamicContent = z.infer<typeof contentPageDynamicContent>
type BlockContentRaw = z.infer<typeof contentPageBlockTextContent>
@@ -163,6 +222,25 @@ export type CardsRaw = CardsGrid["cards_grid"]["cards"][number]
export type Block = RteBlockContent | Shortcuts | CardsGrid | DynamicContent
type SidebarContentRaw = z.infer<typeof contentPageSidebarTextContent>
export type RteSidebarContent = Omit<SidebarContentRaw, "content"> & {
content: {
content: {
json: RTEDocument
embedded_itemsConnection: EdgesWithTotalCount<Embeds>
}
}
}
type SideBarDynamicContent = z.infer<typeof contentPageSidebarDynamicContent>
export type JoinLoyaltyContact = z.infer<typeof contentPageJoinLoyaltyContact>
export type Sidebar =
| JoinLoyaltyContact
| RteSidebarContent
| SideBarDynamicContent
// Content Page Schema and types
export const validateContentPageSchema = z.object({
content_page: z.object({
@@ -173,6 +251,7 @@ export const validateContentPageSchema = z.object({
}),
hero_image: imageVaultAssetSchema.nullable().optional(),
blocks: z.array(contentPageBlockItem).nullable(),
sidebar: z.array(contentPageSidebarItem).nullable(),
system: z.object({
uid: z.string(),
locale: z.nativeEnum(Lang),
@@ -185,9 +264,13 @@ export const validateContentPageSchema = z.object({
export type ContentPageDataRaw = z.infer<typeof validateContentPageSchema>
type ContentPageRaw = ContentPageDataRaw["content_page"]
export type ContentPage = Omit<ContentPageRaw, "blocks" | "hero_image"> & {
export type ContentPage = Omit<
ContentPageRaw,
"blocks" | "hero_image" | "sidebar"
> & {
heroImage?: ImageVaultAsset
blocks: Block[]
sidebar: Sidebar[]
}
const pageConnectionRefs = z.object({
@@ -305,9 +388,37 @@ const contentPageBlockRefsItem = z.discriminatedUnion("__typename", [
contentPageDynamicContentRefs,
])
const contentPageSidebarTextContentRef = z.object({
__typename: z.literal(SidebarTypenameEnum.ContentPageSidebarContent),
content: z.object({
content: z.object({
embedded_itemsConnection: rteConnectionRefs,
}),
}),
})
const contentPageSidebarJoinLoyaltyContactRef = z.object({
__typename: z.literal(
SidebarTypenameEnum.ContentPageSidebarJoinLoyaltyContact
),
join_loyalty_contact: z.object({
button: z
.object({
linkConnection: pageConnectionRefs,
})
.nullable(),
}),
})
const contentPageSidebarRefsItem = z.discriminatedUnion("__typename", [
contentPageSidebarTextContentRef,
contentPageSidebarJoinLoyaltyContactRef,
])
export const validateContentPageRefsSchema = z.object({
content_page: z.object({
blocks: z.array(contentPageBlockRefsItem).nullable(),
sidebar: z.array(contentPageSidebarRefsItem).nullable(),
system: z.object({
content_type_uid: z.string(),
uid: z.string(),

View File

@@ -12,6 +12,7 @@ import {
Block,
ContentPage,
ContentPageDataRaw,
Sidebar,
validateContentPageSchema,
} from "./output"
import {
@@ -25,6 +26,7 @@ import {
import {
CardsGridEnum,
ContentBlocksTypenameEnum,
SidebarTypenameEnum,
} from "@/types/components/content/enums"
import {
TrackingChannelEnum,
@@ -145,11 +147,29 @@ export const contentPageQueryRouter = router({
})
: null
const sidebar = response.data.content_page.sidebar
? response.data.content_page.sidebar.map((item: any) => {
switch (item.__typename) {
case SidebarTypenameEnum.ContentPageSidebarJoinLoyaltyContact:
return {
...item,
join_loyalty_contact: {
...item.join_loyalty_contact,
button: makeButtonObject(item.join_loyalty_contact.button),
},
}
default:
return item
}
})
: null
const heroImage = makeImageVaultImage(content_page.hero_image)
const validatedContentPage = validateContentPageSchema.safeParse({
content_page: {
...content_page,
blocks: processedBlocks,
sidebar,
hero_image: heroImage,
},
})
@@ -169,6 +189,7 @@ export const contentPageQueryRouter = router({
...restContentPage,
heroImage,
blocks: blocks as Block[],
sidebar: sidebar as Sidebar[],
}
const tracking: TrackingSDKPageData = {