Merged in fix/default-page-settings (pull request #2076)
fix: default page settings when invalid or missing * fix: default page settings when invalid or missing since most pages don't have pageSettings Approved-by: Anton Gunnarsson
This commit is contained in:
@@ -266,7 +266,7 @@ export const isBookingWidgetHidden = cache(
|
||||
siteConfigResults.status === "fulfilled" ? siteConfigResults.value : null
|
||||
|
||||
const hideFromPageSettings =
|
||||
pageSettings?.page?.settings?.hide_booking_widget ?? false
|
||||
pageSettings?.page.settings.hide_booking_widget ?? false
|
||||
const hideFromSiteConfig = siteConfig?.bookingWidgetDisabled ?? false
|
||||
|
||||
return hideFromPageSettings || hideFromSiteConfig
|
||||
|
||||
@@ -8,11 +8,23 @@ export const pageSettingsSchema = z.object({
|
||||
})
|
||||
|
||||
export type PageSettingsSchema = z.output<typeof pageSettingsSchema>
|
||||
const DEFAULT_PAGE_SETTINGS: PageSettingsSchema = {
|
||||
hide_booking_widget: false,
|
||||
booking_code: "",
|
||||
} as const
|
||||
|
||||
export const getPageSettingsSchema = z.object({
|
||||
page: z.object({
|
||||
settings: pageSettingsSchema,
|
||||
settings: pageSettingsSchema
|
||||
.nullable()
|
||||
.optional()
|
||||
.transform((val) => val ?? DEFAULT_PAGE_SETTINGS),
|
||||
}),
|
||||
})
|
||||
|
||||
export type GetPageSettingsSchema = z.output<typeof getPageSettingsSchema>
|
||||
export const DEFAULT_GET_PAGE_SETTINGS: GetPageSettingsSchema = {
|
||||
page: {
|
||||
settings: DEFAULT_PAGE_SETTINGS,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import * as Sentry from "@sentry/nextjs"
|
||||
|
||||
import {
|
||||
GetAccountPageSettings,
|
||||
GetCollectionPageSettings,
|
||||
@@ -16,7 +18,11 @@ import { langInput } from "@/server/utils"
|
||||
|
||||
import { generateTag } from "@/utils/generateTag"
|
||||
|
||||
import { type GetPageSettingsSchema, getPageSettingsSchema } from "./output"
|
||||
import {
|
||||
DEFAULT_GET_PAGE_SETTINGS,
|
||||
type GetPageSettingsSchema,
|
||||
getPageSettingsSchema,
|
||||
} from "./output"
|
||||
import { affix } from "./utils"
|
||||
|
||||
import { PageContentTypeEnum } from "@/types/requests/contentType"
|
||||
@@ -24,61 +30,27 @@ import { PageContentTypeEnum } from "@/types/requests/contentType"
|
||||
export const pageSettingsQueryRouter = router({
|
||||
get: contentstackBaseProcedure
|
||||
.input(langInput)
|
||||
.query(async ({ input, ctx }) => {
|
||||
.query(async ({ input, ctx }): Promise<GetPageSettingsSchema> => {
|
||||
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
|
||||
return DEFAULT_GET_PAGE_SETTINGS
|
||||
}
|
||||
|
||||
let GetPageSettings = ""
|
||||
const getPageSettingsQuery =
|
||||
graphqlQueriesForContentType[contentType as PageContentTypeEnum]
|
||||
|
||||
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
|
||||
if (!getPageSettingsQuery) {
|
||||
Sentry.captureMessage(
|
||||
`GetPageSettings: No proper Content type defined for '${contentType}'`
|
||||
)
|
||||
return null
|
||||
return DEFAULT_GET_PAGE_SETTINGS
|
||||
}
|
||||
|
||||
const response = await request<GetPageSettingsSchema>(
|
||||
GetPageSettings,
|
||||
getPageSettingsQuery,
|
||||
{
|
||||
uid: uid,
|
||||
locale: lang,
|
||||
@@ -89,13 +61,28 @@ export const pageSettingsQueryRouter = router({
|
||||
}
|
||||
)
|
||||
|
||||
const result = getPageSettingsSchema.safeParse(response.data)
|
||||
try {
|
||||
const result = getPageSettingsSchema.parse(response.data)
|
||||
return result
|
||||
} catch (error) {
|
||||
Sentry.captureException(error, { extra: { uid, contentType } })
|
||||
|
||||
if (!result.success) {
|
||||
console.error("Page settings fetch error: ", result.error)
|
||||
return null
|
||||
return DEFAULT_GET_PAGE_SETTINGS
|
||||
}
|
||||
|
||||
return result.data
|
||||
}),
|
||||
})
|
||||
|
||||
const graphqlQueriesForContentType: Record<PageContentTypeEnum, any> = {
|
||||
[PageContentTypeEnum.accountPage]: GetAccountPageSettings,
|
||||
[PageContentTypeEnum.collectionPage]: GetCollectionPageSettings,
|
||||
[PageContentTypeEnum.contentPage]: GetContentPageSettings,
|
||||
[PageContentTypeEnum.currentBlocksPage]: GetCurrentBlocksPageSettings,
|
||||
[PageContentTypeEnum.destinationCityPage]: GetDestinationCityPageSettings,
|
||||
[PageContentTypeEnum.destinationCountryPage]:
|
||||
GetDestinationCountryPageSettings,
|
||||
[PageContentTypeEnum.destinationOverviewPage]:
|
||||
GetDestinationOverviewPageSettings,
|
||||
[PageContentTypeEnum.hotelPage]: GetHotelPageSettings,
|
||||
[PageContentTypeEnum.loyaltyPage]: GetLoyaltyPageSettings,
|
||||
[PageContentTypeEnum.startPage]: GetStartPageSettings,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user