feat(SW-190): added hero to static content pages
This commit is contained in:
5
server/routers/contentstack/contentPage/index.ts
Normal file
5
server/routers/contentstack/contentPage/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { mergeRouters } from "@/server/trpc"
|
||||
|
||||
import { contentPageQueryRouter } from "./query"
|
||||
|
||||
export const contentPageRouter = mergeRouters(contentPageQueryRouter)
|
||||
30
server/routers/contentstack/contentPage/output.ts
Normal file
30
server/routers/contentstack/contentPage/output.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
import { ImageVaultAsset } from "@/types/components/imageVaultImage"
|
||||
|
||||
export const validateContentPageSchema = z.object({
|
||||
content_page: z.object({
|
||||
title: z.string(),
|
||||
header: z.object({
|
||||
heading: z.string(),
|
||||
preamble: z.string(),
|
||||
}),
|
||||
hero_image: z.any().nullable(),
|
||||
system: z.object({
|
||||
uid: z.string(),
|
||||
locale: z.nativeEnum(Lang),
|
||||
created_at: z.string(),
|
||||
updated_at: z.string(),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
export type ContentPageDataRaw = z.infer<typeof validateContentPageSchema>
|
||||
|
||||
type ContentPageRaw = ContentPageDataRaw["content_page"]
|
||||
|
||||
export type ContentPage = Omit<ContentPageRaw, "hero_image"> & {
|
||||
hero_image?: ImageVaultAsset
|
||||
}
|
||||
64
server/routers/contentstack/contentPage/query.ts
Normal file
64
server/routers/contentstack/contentPage/query.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { Lang } from "@/constants/languages"
|
||||
import { GetContentPage } from "@/lib/graphql/Query/ContentPage.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { notFound } from "@/server/errors/trpc"
|
||||
import { contentstackExtendedProcedureUID, router } from "@/server/trpc"
|
||||
|
||||
import {
|
||||
ContentPage,
|
||||
ContentPageDataRaw,
|
||||
validateContentPageSchema,
|
||||
} from "./output"
|
||||
import { makeImageVaultImage } from "./utils"
|
||||
|
||||
import {
|
||||
TrackingChannelEnum,
|
||||
TrackingSDKPageData,
|
||||
} from "@/types/components/tracking"
|
||||
|
||||
export const contentPageQueryRouter = router({
|
||||
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
||||
const { lang, uid } = ctx
|
||||
|
||||
const response = await request<ContentPageDataRaw>(GetContentPage, {
|
||||
locale: lang,
|
||||
uid,
|
||||
})
|
||||
|
||||
if (!response.data) {
|
||||
throw notFound(response)
|
||||
}
|
||||
|
||||
const validatedContentPage = validateContentPageSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
|
||||
if (!validatedContentPage.success) {
|
||||
console.error(
|
||||
`Failed to validate Contentpage Data - (lang: ${lang}, uid: ${uid})`
|
||||
)
|
||||
console.error(validatedContentPage.error)
|
||||
return null
|
||||
}
|
||||
|
||||
const contentPageData = validatedContentPage.data.content_page
|
||||
const contentPage: ContentPage = {
|
||||
...contentPageData,
|
||||
hero_image: makeImageVaultImage(contentPageData.hero_image),
|
||||
}
|
||||
|
||||
const tracking: TrackingSDKPageData = {
|
||||
pageId: contentPageData.system.uid,
|
||||
lang: contentPageData.system.locale as Lang,
|
||||
publishedDate: contentPageData.system.updated_at,
|
||||
createdDate: contentPageData.system.created_at,
|
||||
channel: TrackingChannelEnum["static-content-page"],
|
||||
pageType: "staticcontentpage",
|
||||
}
|
||||
|
||||
return {
|
||||
contentPage,
|
||||
tracking,
|
||||
}
|
||||
}),
|
||||
})
|
||||
9
server/routers/contentstack/contentPage/utils.ts
Normal file
9
server/routers/contentstack/contentPage/utils.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { insertResponseToImageVaultAsset } from "@/utils/imageVault"
|
||||
|
||||
import { InsertResponse } from "@/types/components/imageVaultImage"
|
||||
|
||||
export function makeImageVaultImage(image: any) {
|
||||
return image && !!Object.keys(image).length
|
||||
? insertResponseToImageVaultAsset(image as InsertResponse)
|
||||
: undefined
|
||||
}
|
||||
Reference in New Issue
Block a user