diff --git a/lib/graphql/Fragments/Alert.graphql b/lib/graphql/Fragments/Alert.graphql new file mode 100644 index 000000000..2bd93830f --- /dev/null +++ b/lib/graphql/Fragments/Alert.graphql @@ -0,0 +1,36 @@ +#import "./PageLink/AccountPageLink.graphql" +#import "./PageLink/ContentPageLink.graphql" +#import "./PageLink/HotelPageLink.graphql" +#import "./PageLink/LoyaltyPageLink.graphql" + +fragment Alert on Alert { + type + heading + text + phone_contact { + display_text + phone_number + footnote + } + has_sidepeek_button + sidepeek_button { + cta_text + } + sidepeek_content { + heading + content { + embedded_itemsConnection { + edges { + node { + __typename + ...AccountPageLink + ...ContentPageLink + ...HotelPageLink + ...LoyaltyPageLink + } + } + } + json + } + } +} diff --git a/lib/graphql/Fragments/GlobalAlert.graphql b/lib/graphql/Fragments/GlobalAlert.graphql deleted file mode 100644 index a9d5712f9..000000000 --- a/lib/graphql/Fragments/GlobalAlert.graphql +++ /dev/null @@ -1,10 +0,0 @@ -fragment GlobalAlert on GlobalAlert { - type - heading - text - phone_contact { - display_text - phone_number - footnote - } -} diff --git a/lib/graphql/Query/SiteConfig.graphql b/lib/graphql/Query/SiteConfig.graphql index 52bac0fa4..59cc80616 100644 --- a/lib/graphql/Query/SiteConfig.graphql +++ b/lib/graphql/Query/SiteConfig.graphql @@ -1,4 +1,4 @@ -#import "../Fragments/GlobalAlert.graphql" +#import "../Fragments/Alert.graphql" query GetSiteConfig($locale: String!) { all_site_config(limit: 1, locale: $locale) { @@ -8,7 +8,7 @@ query GetSiteConfig($locale: String!) { alertConnection { edges { node { - ...GlobalAlert + ...Alert } } } diff --git a/server/routers/contentstack/base/output.ts b/server/routers/contentstack/base/output.ts index 55a94f790..e6d54f36d 100644 --- a/server/routers/contentstack/base/output.ts +++ b/server/routers/contentstack/base/output.ts @@ -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, } }) diff --git a/server/routers/contentstack/base/query.ts b/server/routers/contentstack/base/query.ts index 98597a825..580ebf7b2 100644 --- a/server/routers/contentstack/base/query.ts +++ b/server/routers/contentstack/base/query.ts @@ -67,9 +67,9 @@ import { getSiteConfigSuccessCounter, } from "./telemetry" import { + getAlertPhoneContactData, getConnections, getFooterConnections, - getGlobalAlertPhoneContactData, } from "./utils" import type { @@ -667,15 +667,15 @@ export const baseQueryRouter = router({ JSON.stringify({ query: { lang } }) ) - const { globalAlert } = validatedSiteConfig.data + const { sitewideAlert } = validatedSiteConfig.data return { ...validatedSiteConfig.data, - globalAlert: globalAlert + sitewideAlert: sitewideAlert ? { - ...globalAlert, + ...sitewideAlert, phone_contact: contactConfig - ? getGlobalAlertPhoneContactData(globalAlert, contactConfig) + ? getAlertPhoneContactData(sitewideAlert, contactConfig) : null, } : null, diff --git a/server/routers/contentstack/base/utils.ts b/server/routers/contentstack/base/utils.ts index 1bab9b170..48cee6b95 100644 --- a/server/routers/contentstack/base/utils.ts +++ b/server/routers/contentstack/base/utils.ts @@ -7,7 +7,7 @@ import { System } from "@/types/requests/system" import { Edges } from "@/types/requests/utils/edges" import { NodeRefs } from "@/types/requests/utils/refs" import type { HeaderRefs } from "@/types/trpc/routers/contentstack/header" -import { SiteConfig } from "@/types/trpc/routers/contentstack/siteConfig" +import { Alert } from "@/types/trpc/routers/contentstack/siteConfig" export function getConnections({ header }: HeaderRefs) { const connections: System["system"][] = [header.system] @@ -71,18 +71,16 @@ export function getFooterConnections(refs: FooterRefDataRaw) { return connections } -export function getGlobalAlertPhoneContactData( - globalAlert: SiteConfig["globalAlert"], +export function getAlertPhoneContactData( + alert: Alert, contactConfig: ContactConfig ) { - if (globalAlert?.phone_contact) { - const { display_text, phone_number, footnote } = globalAlert.phone_contact + if (alert.phoneContact) { + const { displayText, phoneNumber, footnote } = alert.phoneContact return { - display_text, - phone: phone_number - ? getValueFromContactConfig(phone_number, contactConfig) - : null, + displayText, + phoneNumber: getValueFromContactConfig(phoneNumber, contactConfig), footnote: footnote ? getValueFromContactConfig(footnote, contactConfig) : null, diff --git a/types/enums/alert.ts b/types/enums/alert.ts new file mode 100644 index 000000000..2d46121fe --- /dev/null +++ b/types/enums/alert.ts @@ -0,0 +1,5 @@ +export enum AlertTypeEnum { + Info = "info", + Warning = "warning", + Alarm = "alarm", +} diff --git a/types/trpc/routers/contentstack/siteConfig.ts b/types/trpc/routers/contentstack/siteConfig.ts index f51470770..962e7777b 100644 --- a/types/trpc/routers/contentstack/siteConfig.ts +++ b/types/trpc/routers/contentstack/siteConfig.ts @@ -1,17 +1,11 @@ import { z } from "zod" import { - globalAlertSchema, + alertSchema, siteConfigSchema, } from "@/server/routers/contentstack/base/output" -export enum GlobalAlertType { - Info = "info", - Alarm = "alarm", -} - export type GetSiteConfigData = z.input export type SiteConfig = z.output -export type GetGlobalAlertData = z.input -export type GlobalAlert = z.output +export type Alert = z.output