feat(698): fix correct order of blocks from CS

This commit is contained in:
Matilda Landström
2024-10-29 09:20:27 +01:00
parent 24384c08f3
commit 4bd089ab2a
3 changed files with 32 additions and 9 deletions

View File

@@ -26,6 +26,9 @@ query GetContentPage($locale: String!, $uid: String!) {
preamble
...NavigationLinks
}
blocks {
__typename
}
sidebar {
__typename
...ContentSidebar_ContentPage

View File

@@ -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})`

View File

@@ -4,7 +4,6 @@ import {
blocksSchema,
contentPageRefsSchema,
contentPageSchema,
contentPageSchemaBlocks,
sidebarSchema,
} 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 GetBlock = z.input<typeof blocksSchema>
export type SidebarBlock = z.output<typeof sidebarSchema>