feat(SW-285): Ship support for ContentPageBlocksContent

This commit is contained in:
Chuma McPhoy
2024-08-30 08:10:57 +02:00
parent b806824fde
commit 9a51cc6cb5
9 changed files with 198 additions and 13 deletions
@@ -7,17 +7,19 @@ import { contentstackExtendedProcedureUID, router } from "@/server/trpc"
import { generateTag } from "@/utils/generateTag"
import { makeImageVaultImage } from "@/utils/imageVault"
import { validateContentPageSchema } from "./output"
import { removeEmptyObjects } from "../../utils"
import {
Block,
ContentPage,
ContentPageDataRaw,
validateContentPageSchema,
} from "./output"
import { ImageVaultAsset } from "@/types/components/imageVault"
import { ContentBlocksTypenameEnum } from "@/types/components/content/enums"
import {
TrackingChannelEnum,
TrackingSDKPageData,
} from "@/types/components/tracking"
import {
ContentPage,
ContentPageDataRaw,
} from "@/types/trpc/routers/contentstack/contentPage"
export const contentPageQueryRouter = router({
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
@@ -32,15 +34,28 @@ export const contentPageQueryRouter = router({
{ cache: "force-cache", next: { tags: [generateTag(lang, uid)] } }
)
const { content_page } = response.data
const { content_page } = removeEmptyObjects(response.data)
if (!content_page) {
throw notFound(response)
}
const processedBlocks = content_page.blocks
? content_page.blocks.map((block: any) => {
switch (block.__typename) {
case ContentBlocksTypenameEnum.ContentPageBlocksContent:
return block
default:
return block
}
})
: null
const heroImage = makeImageVaultImage(content_page.hero_image)
const validatedContentPage = validateContentPageSchema.safeParse({
content_page: {
...content_page,
hero_image: makeImageVaultImage(content_page.hero_image),
blocks: processedBlocks,
hero_image: heroImage,
},
})
@@ -52,14 +67,13 @@ export const contentPageQueryRouter = router({
return null
}
// Destructure hero_image and rename it to heroImage
const { hero_image: heroImage, ...restContentPage } =
const { hero_image, blocks, ...restContentPage } =
validatedContentPage.data.content_page
// Construct the contentPage object with the correct structure
const contentPage: ContentPage = {
...restContentPage,
heroImage: heroImage as ImageVaultAsset | undefined,
heroImage,
blocks: blocks as Block[],
}
const tracking: TrackingSDKPageData = {