feat(SW-497): Changes to global alert schema

This commit is contained in:
Erik Tiekstra
2024-10-16 12:30:59 +02:00
parent 3f12246e12
commit c8d4f6c47c
8 changed files with 131 additions and 49 deletions

View File

@@ -14,8 +14,8 @@ import { removeMultipleSlashes } from "@/utils/url"
import { systemSchema } from "../schemas/system"
import { Image } from "@/types/image"
import { GlobalAlertType } from "@/types/trpc/routers/contentstack/siteConfig"
import { AlertTypeEnum } from "@/types/enums/alert"
import type { Image } from "@/types/image"
// Help me write this zod schema based on the type ContactConfig
export const validateContactConfigSchema = z.object({
@@ -664,16 +664,75 @@ export const headerSchema = z
}
})
export const globalAlertSchema = z.object({
type: z.nativeEnum(GlobalAlertType),
text: z.string(),
heading: z.string(),
phone_contact: z.object({
display_text: z.string().nullable(),
phone_number: z.string().nullable(),
footnote: z.string().nullable(),
}),
})
export const alertSchema = z
.object({
type: z.nativeEnum(AlertTypeEnum),
text: z.string(),
heading: z.string(),
phone_contact: z.object({
display_text: z.string(),
phone_number: z.string().nullable(),
footnote: z.string().nullable(),
}),
has_sidepeek_button: z.boolean().default(false),
sidepeek_button: z.object({
cta_text: z.string(),
}),
sidepeek_content: z.object({
heading: z.string(),
content: z.object({
json: z.any(),
embedded_itemsConnection: z.object({
edges: z.array(
z.object({
node: z
.discriminatedUnion("__typename", [
pageLinks.accountPageSchema,
pageLinks.contentPageSchema,
pageLinks.hotelPageSchema,
pageLinks.loyaltyPageSchema,
])
.transform((data) => {
const link = pageLinks.transform(data)
if (link) {
return link
}
return data
}),
})
),
}),
}),
}),
})
.transform(
({
type,
heading,
text,
phone_contact,
has_sidepeek_button,
sidepeek_button,
sidepeek_content,
}) => {
return {
type,
text,
heading,
phoneContact:
phone_contact.display_text && phone_contact.phone_number
? {
displayText: phone_contact.display_text,
phoneNumber: phone_contact.phone_number,
footnote: phone_contact.footnote,
}
: null,
hasSidepeekButton: has_sidepeek_button,
sidepeekButton: has_sidepeek_button ? sidepeek_button : null,
sidepeekContent: has_sidepeek_button ? sidepeek_content : null,
}
}
)
export const siteConfigSchema = z
.object({
@@ -687,7 +746,7 @@ export const siteConfigSchema = z
edges: z
.array(
z.object({
node: globalAlertSchema,
node: alertSchema,
})
)
.max(1),
@@ -701,7 +760,7 @@ export const siteConfigSchema = z
.transform((data) => {
if (!data.all_site_config.items.length) {
return {
globalAlert: null,
sitewideAlert: null,
bookingWidgetDisabled: false,
}
}
@@ -709,7 +768,7 @@ export const siteConfigSchema = z
const { sitewide_alert } = data.all_site_config.items[0]
return {
globalAlert: sitewide_alert.alertConnection.edges[0]?.node || null,
sitewideAlert: sitewide_alert.alertConnection.edges[0]?.node || null,
bookingWidgetDisabled: sitewide_alert.booking_widget_disabled,
}
})