283 lines
7.0 KiB
TypeScript
283 lines
7.0 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { Lang } from "@/constants/languages"
|
|
|
|
import { imageVaultAssetSchema } from "../schemas/imageVault"
|
|
|
|
import {
|
|
CardsGridEnum,
|
|
ContentBlocksTypenameEnum,
|
|
} from "@/types/components/content/enums"
|
|
import { ImageVaultAsset } from "@/types/components/imageVault"
|
|
import { Embeds } from "@/types/requests/embeds"
|
|
import { PageLinkEnum } from "@/types/requests/pageLinks"
|
|
import { RTEEmbedsEnum } from "@/types/requests/rte"
|
|
import { EdgesWithTotalCount } from "@/types/requests/utils/edges"
|
|
import { RTEDocument } from "@/types/rte/node"
|
|
|
|
// Block Schema and types
|
|
const contentPageBlockTextContent = z.object({
|
|
__typename: z.literal(ContentBlocksTypenameEnum.ContentPageBlocksContent),
|
|
content: z.object({
|
|
content: z.object({
|
|
embedded_itemsConnection: z.object({
|
|
edges: z.array(z.any()),
|
|
totalCount: z.number(),
|
|
}),
|
|
json: z.any(),
|
|
}),
|
|
}),
|
|
})
|
|
|
|
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(),
|
|
})
|
|
),
|
|
}),
|
|
})
|
|
|
|
// TODO: this is a separate entity, should be in a separate file.
|
|
const cardBlock = z.object({
|
|
__typename: z.literal(CardsGridEnum.Card),
|
|
heading: z.string().nullable(),
|
|
body_text: z.string().nullable(),
|
|
background_image: z.any(),
|
|
scripted_top_title: z.string().nullable(),
|
|
primaryButton: z
|
|
.object({
|
|
openInNewTab: z.boolean(),
|
|
title: z.string(),
|
|
href: z.string(),
|
|
isExternal: z.boolean(),
|
|
})
|
|
.optional(),
|
|
secondaryButton: z
|
|
.object({
|
|
openInNewTab: z.boolean(),
|
|
title: z.string(),
|
|
href: z.string(),
|
|
isExternal: z.boolean(),
|
|
})
|
|
.optional(),
|
|
system: z.object({
|
|
locale: z.nativeEnum(Lang),
|
|
uid: z.string(),
|
|
}),
|
|
})
|
|
|
|
const loyaltyCardBlock = z.object({
|
|
__typename: z.literal(CardsGridEnum.LoyaltyCard),
|
|
heading: z.string().nullable(),
|
|
body_text: z.string().nullable(),
|
|
image: z.any(),
|
|
link: z
|
|
.object({
|
|
openInNewTab: z.boolean(),
|
|
title: z.string(),
|
|
href: z.string(),
|
|
isExternal: z.boolean(),
|
|
})
|
|
.optional(),
|
|
system: z.object({
|
|
locale: z.nativeEnum(Lang),
|
|
uid: z.string(),
|
|
}),
|
|
})
|
|
|
|
const contentPageCardsItems = z.discriminatedUnion("__typename", [
|
|
loyaltyCardBlock,
|
|
cardBlock,
|
|
])
|
|
|
|
const contentPageCards = z.object({
|
|
__typename: z.literal(ContentBlocksTypenameEnum.ContentPageBlocksCardsGrid),
|
|
cards_grid: z.object({
|
|
title: z.string().nullable(),
|
|
preamble: z.string().nullable(),
|
|
layout: z.enum(["twoColumnGrid", "threeColumnGrid", "twoPlusOne"]),
|
|
theme: z.enum(["one", "two", "three"]).nullable(),
|
|
cards: z.array(contentPageCardsItems),
|
|
}),
|
|
})
|
|
|
|
const contentPageBlockItem = z.discriminatedUnion("__typename", [
|
|
contentPageBlockTextContent,
|
|
contentPageShortcuts,
|
|
contentPageCards,
|
|
])
|
|
|
|
type BlockContentRaw = z.infer<typeof contentPageBlockTextContent>
|
|
export interface RteBlockContent extends BlockContentRaw {
|
|
content: {
|
|
content: {
|
|
json: RTEDocument
|
|
embedded_itemsConnection: EdgesWithTotalCount<Embeds>
|
|
}
|
|
}
|
|
}
|
|
|
|
export type Shortcuts = z.infer<typeof contentPageShortcuts>
|
|
|
|
type LoyaltyCardRaw = z.infer<typeof loyaltyCardBlock>
|
|
type LoyaltyCard = Omit<LoyaltyCardRaw, "image"> & {
|
|
image?: ImageVaultAsset
|
|
}
|
|
type CardRaw = z.infer<typeof cardBlock>
|
|
type Card = Omit<CardRaw, "background_image"> & {
|
|
backgroundImage?: ImageVaultAsset
|
|
}
|
|
type CardsGridRaw = z.infer<typeof contentPageCards>
|
|
export type CardsGrid = Omit<CardsGridRaw, "cards"> & {
|
|
cards: (LoyaltyCard | Card)[]
|
|
}
|
|
export type CardsRaw = CardsGrid["cards_grid"]["cards"][number]
|
|
|
|
export type Block = RteBlockContent | Shortcuts | CardsGrid
|
|
|
|
// Content Page Schema and types
|
|
export const validateContentPageSchema = z.object({
|
|
content_page: z.object({
|
|
title: z.string(),
|
|
header: z.object({
|
|
heading: z.string(),
|
|
preamble: z.string(),
|
|
}),
|
|
hero_image: imageVaultAssetSchema.nullable().optional(),
|
|
blocks: z.array(contentPageBlockItem).nullable(),
|
|
system: z.object({
|
|
uid: z.string(),
|
|
locale: z.nativeEnum(Lang),
|
|
created_at: z.string(),
|
|
updated_at: z.string(),
|
|
}),
|
|
}),
|
|
})
|
|
|
|
export type ContentPageDataRaw = z.infer<typeof validateContentPageSchema>
|
|
type ContentPageRaw = ContentPageDataRaw["content_page"]
|
|
|
|
export type ContentPage = Omit<ContentPageRaw, "blocks" | "hero_image"> & {
|
|
heroImage?: ImageVaultAsset
|
|
blocks: Block[]
|
|
}
|
|
|
|
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(),
|
|
}),
|
|
}),
|
|
})
|
|
),
|
|
})
|
|
|
|
const rteConnectionRefs = z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: z.object({
|
|
__typename: z.nativeEnum(RTEEmbedsEnum),
|
|
system: z.object({
|
|
content_type_uid: z.string(),
|
|
uid: z.string(),
|
|
}),
|
|
}),
|
|
})
|
|
),
|
|
})
|
|
|
|
const cardBlockRefs = z.object({
|
|
__typename: z.literal(CardsGridEnum.Card),
|
|
primary_button: z
|
|
.object({
|
|
linkConnection: pageConnectionRefs,
|
|
})
|
|
.nullable(),
|
|
secondary_button: z
|
|
.object({
|
|
linkConnection: pageConnectionRefs,
|
|
})
|
|
.nullable(),
|
|
system: z.object({
|
|
content_type_uid: z.string(),
|
|
uid: z.string(),
|
|
}),
|
|
})
|
|
|
|
const loyaltyCardBlockRefs = z.object({
|
|
__typename: z.literal(CardsGridEnum.LoyaltyCard),
|
|
link: z
|
|
.object({
|
|
linkConnection: pageConnectionRefs,
|
|
})
|
|
.nullable(),
|
|
system: z.object({
|
|
content_type_uid: z.string(),
|
|
uid: z.string(),
|
|
}),
|
|
})
|
|
|
|
const cardGridCardsRef = z.discriminatedUnion("__typename", [
|
|
loyaltyCardBlockRefs,
|
|
cardBlockRefs,
|
|
])
|
|
|
|
const contentPageBlockTextContentRefs = z.object({
|
|
__typename: z.literal(ContentBlocksTypenameEnum.ContentPageBlocksContent),
|
|
content: z.object({
|
|
content: z.object({
|
|
embedded_itemsConnection: rteConnectionRefs,
|
|
}),
|
|
}),
|
|
})
|
|
|
|
const contentPageCardsRefs = z.object({
|
|
__typename: z.literal(ContentBlocksTypenameEnum.ContentPageBlocksCardsGrid),
|
|
cards_grid: z.object({
|
|
cardConnection: cardGridCardsRef,
|
|
}),
|
|
})
|
|
|
|
const contentPageShortcutsRefs = z.object({
|
|
__typename: z.literal(ContentBlocksTypenameEnum.ContentPageBlocksShortcuts),
|
|
shortcuts: z.object({
|
|
shortcuts: z.array(
|
|
z.object({
|
|
linkConnection: rteConnectionRefs,
|
|
})
|
|
),
|
|
}),
|
|
})
|
|
|
|
const contentPageBlockRefsItem = z.discriminatedUnion("__typename", [
|
|
contentPageBlockTextContentRefs,
|
|
contentPageShortcutsRefs,
|
|
contentPageCardsRefs,
|
|
])
|
|
|
|
export const validateContentPageRefsSchema = z.object({
|
|
content_page: z.object({
|
|
blocks: z.array(contentPageBlockRefsItem).nullable(),
|
|
system: z.object({
|
|
content_type_uid: z.string(),
|
|
uid: z.string(),
|
|
}),
|
|
}),
|
|
})
|
|
|
|
export type ContentPageRefsDataRaw = z.infer<
|
|
typeof validateContentPageRefsSchema
|
|
>
|