feat(698): fix correct order of blocks from CS
This commit is contained in:
@@ -19,7 +19,11 @@ import {
|
||||
TrackingChannelEnum,
|
||||
type TrackingSDKPageData,
|
||||
} 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({
|
||||
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 = {
|
||||
...mainResponse.data,
|
||||
content_page: {
|
||||
...mainResponse.data.content_page,
|
||||
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
|
||||
blocks: sortedBlocks,
|
||||
},
|
||||
}
|
||||
|
||||
const contentPage = contentPageSchema.safeParse(responseData)
|
||||
|
||||
if (!contentPage.success) {
|
||||
console.error(
|
||||
`Failed to validate Contentpage Data - (lang: ${lang}, uid: ${uid})`
|
||||
|
||||
Reference in New Issue
Block a user