feat(698): fix correct order of blocks from CS
This commit is contained in:
@@ -26,6 +26,9 @@ query GetContentPage($locale: String!, $uid: String!) {
|
|||||||
preamble
|
preamble
|
||||||
...NavigationLinks
|
...NavigationLinks
|
||||||
}
|
}
|
||||||
|
blocks {
|
||||||
|
__typename
|
||||||
|
}
|
||||||
sidebar {
|
sidebar {
|
||||||
__typename
|
__typename
|
||||||
...ContentSidebar_ContentPage
|
...ContentSidebar_ContentPage
|
||||||
|
|||||||
@@ -19,7 +19,11 @@ import {
|
|||||||
TrackingChannelEnum,
|
TrackingChannelEnum,
|
||||||
type TrackingSDKPageData,
|
type TrackingSDKPageData,
|
||||||
} from "@/types/components/tracking"
|
} from "@/types/components/tracking"
|
||||||
import type { GetContentPageSchema } from "@/types/trpc/routers/contentstack/contentPage"
|
import { ContentPageEnum } from "@/types/enums/contentPage"
|
||||||
|
import type {
|
||||||
|
GetBlock,
|
||||||
|
GetContentPageSchema,
|
||||||
|
} from "@/types/trpc/routers/contentstack/contentPage"
|
||||||
|
|
||||||
export const contentPageQueryRouter = router({
|
export const contentPageQueryRouter = router({
|
||||||
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
||||||
@@ -79,21 +83,36 @@ export const contentPageQueryRouter = router({
|
|||||||
),
|
),
|
||||||
])
|
])
|
||||||
|
|
||||||
|
const blocksOrder = mainResponse.data.content_page.blocks?.map(
|
||||||
|
(block) => block.__typename
|
||||||
|
)
|
||||||
|
|
||||||
|
let sortedBlocks
|
||||||
|
if (blocksOrder) {
|
||||||
|
const blocks = [
|
||||||
|
blocksResponse1.data.content_page.blocks,
|
||||||
|
blocksResponse2.data.content_page.blocks,
|
||||||
|
]
|
||||||
|
.flat(2)
|
||||||
|
.filter((obj) => !(obj && Object.keys(obj).length < 2))
|
||||||
|
// Remove empty objects and objects with only typename
|
||||||
|
|
||||||
|
sortedBlocks = blocksOrder
|
||||||
|
.map((typename: ContentPageEnum.ContentStack.blocks) =>
|
||||||
|
blocks.find((block) => block?.__typename === typename)
|
||||||
|
)
|
||||||
|
.filter((block): block is GetBlock => !!block)
|
||||||
|
}
|
||||||
|
|
||||||
const responseData = {
|
const responseData = {
|
||||||
...mainResponse.data,
|
...mainResponse.data,
|
||||||
content_page: {
|
content_page: {
|
||||||
...mainResponse.data.content_page,
|
...mainResponse.data.content_page,
|
||||||
blocks: [
|
blocks: sortedBlocks,
|
||||||
blocksResponse1.data.content_page.blocks,
|
|
||||||
blocksResponse2.data.content_page.blocks,
|
|
||||||
]
|
|
||||||
.flat(2)
|
|
||||||
.filter((obj) => !(obj && Object.keys(obj).length < 2)), // Remove empty objects and objects with only typename
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
const contentPage = contentPageSchema.safeParse(responseData)
|
const contentPage = contentPageSchema.safeParse(responseData)
|
||||||
|
|
||||||
if (!contentPage.success) {
|
if (!contentPage.success) {
|
||||||
console.error(
|
console.error(
|
||||||
`Failed to validate Contentpage Data - (lang: ${lang}, uid: ${uid})`
|
`Failed to validate Contentpage Data - (lang: ${lang}, uid: ${uid})`
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import {
|
|||||||
blocksSchema,
|
blocksSchema,
|
||||||
contentPageRefsSchema,
|
contentPageRefsSchema,
|
||||||
contentPageSchema,
|
contentPageSchema,
|
||||||
contentPageSchemaBlocks,
|
|
||||||
sidebarSchema,
|
sidebarSchema,
|
||||||
} from "@/server/routers/contentstack/contentPage/output"
|
} from "@/server/routers/contentstack/contentPage/output"
|
||||||
|
|
||||||
@@ -21,4 +20,6 @@ export interface ContentPage extends z.output<typeof contentPageSchema> {}
|
|||||||
|
|
||||||
export type Block = z.output<typeof blocksSchema>
|
export type Block = z.output<typeof blocksSchema>
|
||||||
|
|
||||||
|
export type GetBlock = z.input<typeof blocksSchema>
|
||||||
|
|
||||||
export type SidebarBlock = z.output<typeof sidebarSchema>
|
export type SidebarBlock = z.output<typeof sidebarSchema>
|
||||||
|
|||||||
Reference in New Issue
Block a user