feat(SW-200): refactored breadcrumbs fetching and added json schema to layout
This commit is contained in:
@@ -1,16 +1,11 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { removeMultipleSlashes } from "@/utils/url"
|
||||
|
||||
import { systemSchema } from "../schemas/system"
|
||||
import { homeBreadcrumbs } from "./utils"
|
||||
|
||||
export const getBreadcrumbsSchema = z.array(
|
||||
z.object({
|
||||
href: z.string().optional(),
|
||||
title: z.string(),
|
||||
uid: z.string(),
|
||||
})
|
||||
)
|
||||
|
||||
const breadcrumbsRefs = z.object({
|
||||
export const breadcrumbsRefsSchema = z.object({
|
||||
web: z
|
||||
.object({
|
||||
breadcrumbs: z
|
||||
@@ -32,43 +27,7 @@ const breadcrumbsRefs = z.object({
|
||||
system: systemSchema,
|
||||
})
|
||||
|
||||
export type BreadcrumbsRefs = z.infer<typeof breadcrumbsRefs>
|
||||
|
||||
export const validateMyPagesBreadcrumbsRefsContentstackSchema = z.object({
|
||||
account_page: breadcrumbsRefs,
|
||||
})
|
||||
|
||||
export type GetMyPagesBreadcrumbsRefsData = z.infer<
|
||||
typeof validateMyPagesBreadcrumbsRefsContentstackSchema
|
||||
>
|
||||
|
||||
export const validateLoyaltyPageBreadcrumbsRefsContentstackSchema = z.object({
|
||||
loyalty_page: breadcrumbsRefs,
|
||||
})
|
||||
|
||||
export type GetLoyaltyPageBreadcrumbsRefsData = z.infer<
|
||||
typeof validateLoyaltyPageBreadcrumbsRefsContentstackSchema
|
||||
>
|
||||
|
||||
export const validateCollectionPageBreadcrumbsRefsContentstackSchema = z.object(
|
||||
{
|
||||
collection_page: breadcrumbsRefs,
|
||||
}
|
||||
)
|
||||
|
||||
export type GetCollectionPageBreadcrumbsRefsData = z.infer<
|
||||
typeof validateCollectionPageBreadcrumbsRefsContentstackSchema
|
||||
>
|
||||
|
||||
export const validateContentPageBreadcrumbsRefsContentstackSchema = z.object({
|
||||
content_page: breadcrumbsRefs,
|
||||
})
|
||||
|
||||
export type GetContentPageBreadcrumbsRefsData = z.infer<
|
||||
typeof validateContentPageBreadcrumbsRefsContentstackSchema
|
||||
>
|
||||
|
||||
const page = z.object({
|
||||
export const rawBreadcrumbsDataSchema = z.object({
|
||||
web: z.object({
|
||||
breadcrumbs: z.object({
|
||||
title: z.string(),
|
||||
@@ -92,36 +51,24 @@ const page = z.object({
|
||||
system: systemSchema,
|
||||
})
|
||||
|
||||
export type Page = z.infer<typeof page>
|
||||
export const breadcrumbsSchema = rawBreadcrumbsDataSchema.transform((data) => {
|
||||
const { parentsConnection, title } = data.web.breadcrumbs
|
||||
const parentBreadcrumbs = parentsConnection.edges.map((breadcrumb) => {
|
||||
return {
|
||||
href: removeMultipleSlashes(
|
||||
`/${breadcrumb.node.system.locale}/${breadcrumb.node.url}`
|
||||
),
|
||||
title: breadcrumb.node.web.breadcrumbs.title,
|
||||
uid: breadcrumb.node.system.uid,
|
||||
}
|
||||
})
|
||||
|
||||
export const validateMyPagesBreadcrumbsContentstackSchema = z.object({
|
||||
account_page: page,
|
||||
const pageBreadcrumb = {
|
||||
title,
|
||||
uid: data.system.uid,
|
||||
href: undefined,
|
||||
}
|
||||
const homeBreadcrumb = homeBreadcrumbs[data.system.locale]
|
||||
|
||||
return [homeBreadcrumb, parentBreadcrumbs, pageBreadcrumb].flat()
|
||||
})
|
||||
|
||||
export type GetMyPagesBreadcrumbsData = z.infer<
|
||||
typeof validateMyPagesBreadcrumbsContentstackSchema
|
||||
>
|
||||
|
||||
export const validateLoyaltyPageBreadcrumbsContentstackSchema = z.object({
|
||||
loyalty_page: page,
|
||||
})
|
||||
|
||||
export type GetLoyaltyPageBreadcrumbsData = z.infer<
|
||||
typeof validateLoyaltyPageBreadcrumbsContentstackSchema
|
||||
>
|
||||
|
||||
export const validateContentPageBreadcrumbsContentstackSchema = z.object({
|
||||
content_page: page,
|
||||
})
|
||||
|
||||
export type GetContentPageBreadcrumbsData = z.infer<
|
||||
typeof validateContentPageBreadcrumbsContentstackSchema
|
||||
>
|
||||
|
||||
export const validateCollectionPageBreadcrumbsContentstackSchema = z.object({
|
||||
collection_page: page,
|
||||
})
|
||||
|
||||
export type GetCollectionPageBreadcrumbsData = z.infer<
|
||||
typeof validateCollectionPageBreadcrumbsContentstackSchema
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user