96 lines
2.6 KiB
TypeScript
96 lines
2.6 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import {
|
|
contentCardRefSchema,
|
|
contentCardSchema,
|
|
transformContentCard,
|
|
} from "./cards/contentCard"
|
|
import { buttonSchema } from "./utils/buttonLinkSchema"
|
|
import { linkConnectionRefsSchema } from "./utils/linkConnection"
|
|
|
|
import { BlocksEnums } from "@/types/enums/blocks"
|
|
import {
|
|
type CardGalleryFilter,
|
|
CardGalleryFilterEnum,
|
|
} from "@/types/enums/cardGallery"
|
|
|
|
export const cardGallerySchema = z.object({
|
|
typename: z
|
|
.literal(BlocksEnums.block.CardGallery)
|
|
.optional()
|
|
.default(BlocksEnums.block.CardGallery),
|
|
card_gallery: z
|
|
.object({
|
|
heading: z.string().optional(),
|
|
link: buttonSchema.optional(),
|
|
card_groups: z.array(
|
|
z.object({
|
|
filter_identifier: z.nativeEnum(CardGalleryFilterEnum),
|
|
filter_label: z.string(),
|
|
cardsConnection: z.object({
|
|
edges: z.array(z.object({ node: contentCardSchema })),
|
|
}),
|
|
})
|
|
),
|
|
})
|
|
.transform((data) => {
|
|
const filterCategories = data.card_groups.reduce<
|
|
Array<{
|
|
identifier: CardGalleryFilter
|
|
label: string
|
|
}>
|
|
>((acc, group) => {
|
|
const identifier = group.filter_identifier
|
|
if (!acc.some((category) => category.identifier === identifier)) {
|
|
acc.push({
|
|
identifier,
|
|
label: group.filter_label,
|
|
})
|
|
}
|
|
return acc
|
|
}, [])
|
|
|
|
return {
|
|
heading: data.heading,
|
|
filterCategories,
|
|
cards: data.card_groups.flatMap((group) =>
|
|
group.cardsConnection.edges
|
|
.map((edge) => transformContentCard(edge.node))
|
|
.filter((card): card is NonNullable<typeof card> => card !== null)
|
|
.map((card) => ({
|
|
...card,
|
|
filterId: group.filter_identifier,
|
|
}))
|
|
),
|
|
defaultFilter:
|
|
data.card_groups[0]?.filter_identifier ??
|
|
filterCategories[0]?.identifier,
|
|
link:
|
|
data.link?.href && data.link.title
|
|
? { href: data.link.href, text: data.link.title }
|
|
: undefined,
|
|
}
|
|
}),
|
|
})
|
|
|
|
export const cardGalleryRefsSchema = z.object({
|
|
typename: z
|
|
.literal(BlocksEnums.block.CardGallery)
|
|
.optional()
|
|
.default(BlocksEnums.block.CardGallery),
|
|
card_gallery: z.object({
|
|
card_groups: z.array(
|
|
z.object({
|
|
cardsConnection: z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: contentCardRefSchema,
|
|
})
|
|
),
|
|
}),
|
|
})
|
|
),
|
|
link: linkConnectionRefsSchema.optional(),
|
|
}),
|
|
})
|