feat(SW-1389): refactor page settings
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
import { mergeRouters } from "@/server/trpc"
|
||||
|
||||
import { pageSettingsQueryRouter } from "./query"
|
||||
|
||||
export const pageSettingsRouter = mergeRouters(pageSettingsQueryRouter)
|
||||
@@ -0,0 +1,15 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const pageSettingsSchema = z.object({
|
||||
hide_booking_widget: z.boolean(),
|
||||
})
|
||||
|
||||
export type PageSettingsSchema = z.output<typeof pageSettingsSchema>
|
||||
|
||||
export const getPageSettingsSchema = z.object({
|
||||
page: z.object({
|
||||
settings: pageSettingsSchema,
|
||||
}),
|
||||
})
|
||||
|
||||
export type GetPageSettingsSchema = z.output<typeof getPageSettingsSchema>
|
||||
@@ -0,0 +1,103 @@
|
||||
import {
|
||||
GetAccountPageSettings,
|
||||
GetCollectionPageSettings,
|
||||
GetContentPageSettings,
|
||||
GetCurrentBlocksPageSettings,
|
||||
GetDestinationCityPageSettings,
|
||||
GetDestinationCountryPageSettings,
|
||||
GetDestinationOverviewPageSettings,
|
||||
GetHotelPageSettings,
|
||||
GetLoyaltyPageSettings,
|
||||
GetStartPageSettings,
|
||||
} from "@/lib/graphql/Query/PageSettings.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { contentstackBaseProcedure, router } from "@/server/trpc"
|
||||
import { langInput } from "@/server/utils"
|
||||
|
||||
import { generateTag } from "@/utils/generateTag"
|
||||
|
||||
import { type GetPageSettingsSchema, getPageSettingsSchema } from "./output"
|
||||
import { affix } from "./utils"
|
||||
|
||||
import { PageContentTypeEnum } from "@/types/requests/contentType"
|
||||
|
||||
export const pageSettingsQueryRouter = router({
|
||||
get: contentstackBaseProcedure
|
||||
.input(langInput)
|
||||
.query(async ({ input, ctx }) => {
|
||||
const { contentType, uid } = ctx
|
||||
const lang = input.lang ?? ctx.lang
|
||||
|
||||
// This condition is to handle 404 page case and booking flow
|
||||
if (!contentType || !uid) {
|
||||
console.log("No proper params defined: ", contentType, uid)
|
||||
return null
|
||||
}
|
||||
|
||||
let GetPageSettings = ""
|
||||
|
||||
switch (contentType) {
|
||||
case PageContentTypeEnum.accountPage:
|
||||
GetPageSettings = GetAccountPageSettings
|
||||
break
|
||||
case PageContentTypeEnum.collectionPage:
|
||||
GetPageSettings = GetCollectionPageSettings
|
||||
break
|
||||
case PageContentTypeEnum.contentPage:
|
||||
GetPageSettings = GetContentPageSettings
|
||||
break
|
||||
case PageContentTypeEnum.currentBlocksPage:
|
||||
GetPageSettings = GetCurrentBlocksPageSettings
|
||||
break
|
||||
case PageContentTypeEnum.destinationCityPage:
|
||||
GetPageSettings = GetDestinationCityPageSettings
|
||||
break
|
||||
case PageContentTypeEnum.destinationCountryPage:
|
||||
GetPageSettings = GetDestinationCountryPageSettings
|
||||
break
|
||||
case PageContentTypeEnum.destinationOverviewPage:
|
||||
GetPageSettings = GetDestinationOverviewPageSettings
|
||||
break
|
||||
case PageContentTypeEnum.hotelPage:
|
||||
GetPageSettings = GetHotelPageSettings
|
||||
break
|
||||
case PageContentTypeEnum.loyaltyPage:
|
||||
GetPageSettings = GetLoyaltyPageSettings
|
||||
break
|
||||
case PageContentTypeEnum.startPage:
|
||||
GetPageSettings = GetStartPageSettings
|
||||
break
|
||||
}
|
||||
|
||||
if (!GetPageSettings) {
|
||||
console.error(
|
||||
"[pageSettings] No proper Content type defined: ",
|
||||
contentType
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
const response = await request<GetPageSettingsSchema>(
|
||||
GetPageSettings,
|
||||
{
|
||||
uid: uid,
|
||||
locale: lang,
|
||||
},
|
||||
{
|
||||
cache: "force-cache",
|
||||
next: {
|
||||
tags: [generateTag(lang, uid, affix)],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
const result = getPageSettingsSchema.safeParse(response.data)
|
||||
|
||||
if (!result.success) {
|
||||
console.error("Page settings fetch error: ", result.error)
|
||||
return null
|
||||
}
|
||||
|
||||
return result.data
|
||||
}),
|
||||
})
|
||||
@@ -0,0 +1 @@
|
||||
export const affix = "pageSettings"
|
||||
Reference in New Issue
Block a user