feat(SW-1384): add CarouselCards block to start page * feat(SW-1384): add filterable carousel cards block to start page * fix(SW-1384): remove unnecessary link prop from SectionHeader * fix(SW-1384): remove uneeded undefined * fix(SW-1384): better type safety * feat(SW-1384): Add see all link to filterable carousel cards section header * refactor(SW-1384): Replace FilterableCarouselCards with CarouselCards block * fix(SW-1384): Remove CardsEnumType type definition * fix(SW-1384):Implement code review feedback to CarouselCards * refactor(SW-1384): Convert CarouselCardFilterEnum to const enum with type Approved-by: Christian Andolf
138 lines
3.6 KiB
TypeScript
138 lines
3.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 CarouselCardFilter,
|
|
CarouselCardFilterEnum,
|
|
} from "@/types/enums/carouselCards"
|
|
|
|
const commonFields = {
|
|
heading: z.string().optional(),
|
|
link: buttonSchema.optional(),
|
|
} as const
|
|
|
|
const carouselCardsWithFilters = z.object({
|
|
...commonFields,
|
|
enable_filters: z.literal(true),
|
|
card_groups: z.array(
|
|
z.object({
|
|
filter_category: z.object({
|
|
filter_identifier: z.nativeEnum(CarouselCardFilterEnum),
|
|
filter_label: z.string(),
|
|
}),
|
|
cardConnection: z.object({
|
|
edges: z.array(z.object({ node: contentCardSchema })),
|
|
}),
|
|
})
|
|
),
|
|
default_filter: z.nativeEnum(CarouselCardFilterEnum),
|
|
})
|
|
|
|
const carouselCardsWithoutFilters = z.object({
|
|
...commonFields,
|
|
enable_filters: z.literal(false),
|
|
card_groups: z.array(
|
|
z.object({
|
|
filter_category: z.object({
|
|
filter_identifier: z.null(),
|
|
filter_label: z.string(),
|
|
}),
|
|
cardConnection: z.object({
|
|
edges: z.array(z.object({ node: contentCardSchema })),
|
|
}),
|
|
})
|
|
),
|
|
default_filter: z.null(),
|
|
})
|
|
|
|
export const carouselCardsSchema = z.object({
|
|
typename: z
|
|
.literal(BlocksEnums.block.CarouselCards)
|
|
.optional()
|
|
.default(BlocksEnums.block.CarouselCards),
|
|
carousel_cards: z
|
|
.discriminatedUnion("enable_filters", [
|
|
carouselCardsWithFilters,
|
|
carouselCardsWithoutFilters,
|
|
])
|
|
.transform((data) => {
|
|
if (!data.enable_filters) {
|
|
return {
|
|
heading: data.heading,
|
|
enableFilters: false,
|
|
filterCategories: [],
|
|
cards: data.card_groups.flatMap((group) =>
|
|
group.cardConnection.edges.map((edge) =>
|
|
transformContentCard(edge.node)
|
|
)
|
|
),
|
|
defaultFilter: null,
|
|
link: data.link
|
|
? { href: data.link.href, text: data.link.title }
|
|
: undefined,
|
|
}
|
|
}
|
|
|
|
const filterCategories = data.card_groups.reduce<
|
|
Array<{
|
|
identifier: CarouselCardFilter
|
|
label: string
|
|
}>
|
|
>((acc, group) => {
|
|
const identifier = group.filter_category.filter_identifier
|
|
if (!acc.some((category) => category.identifier === identifier)) {
|
|
acc.push({
|
|
identifier,
|
|
label: group.filter_category.filter_label,
|
|
})
|
|
}
|
|
return acc
|
|
}, [])
|
|
|
|
return {
|
|
heading: data.heading,
|
|
enableFilters: true,
|
|
filterCategories,
|
|
cards: data.card_groups.flatMap((group) =>
|
|
group.cardConnection.edges.map((edge) => ({
|
|
...transformContentCard(edge.node),
|
|
filterId: group.filter_category.filter_identifier,
|
|
}))
|
|
),
|
|
defaultFilter: data.default_filter,
|
|
link: data.link
|
|
? { href: data.link.href, text: data.link.title }
|
|
: undefined,
|
|
}
|
|
}),
|
|
})
|
|
|
|
export const carouselCardsRefsSchema = z.object({
|
|
typename: z
|
|
.literal(BlocksEnums.block.CarouselCards)
|
|
.optional()
|
|
.default(BlocksEnums.block.CarouselCards),
|
|
carousel_cards: z.object({
|
|
card_groups: z.array(
|
|
z.object({
|
|
cardConnection: z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: contentCardRefSchema,
|
|
})
|
|
),
|
|
}),
|
|
})
|
|
),
|
|
link: linkConnectionRefsSchema.optional(),
|
|
}),
|
|
})
|