Files
web/server/routers/contentstack/schemas/blocks/cardGallery.ts
Erik Tiekstra 2781a41110 Merged in feat/SW-1443-card-gallery-destination-overview (pull request #1362)
feat(SW-1443): added cardGallery block to destination overview page instead of carousel functionality

* feat(SW-1443): added cardGallery block to destination overview page instead of carousel functionality


Approved-by: Fredrik Thorsson
2025-02-18 14:42:36 +00:00

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(),
cardConnection: 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.cardConnection.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({
cardConnection: z.object({
edges: z.array(
z.object({
node: contentCardRefSchema,
})
),
}),
})
),
link: linkConnectionRefsSchema.optional(),
}),
})