feat(SW-497): Changes to global alert schema
This commit is contained in:
36
lib/graphql/Fragments/Alert.graphql
Normal file
36
lib/graphql/Fragments/Alert.graphql
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
fragment GlobalAlert on GlobalAlert {
|
||||
type
|
||||
heading
|
||||
text
|
||||
phone_contact {
|
||||
display_text
|
||||
phone_number
|
||||
footnote
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
5
types/enums/alert.ts
Normal file
5
types/enums/alert.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export enum AlertTypeEnum {
|
||||
Info = "info",
|
||||
Warning = "warning",
|
||||
Alarm = "alarm",
|
||||
}
|
||||
@@ -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<typeof siteConfigSchema>
|
||||
export type SiteConfig = z.output<typeof siteConfigSchema>
|
||||
|
||||
export type GetGlobalAlertData = z.input<typeof globalAlertSchema>
|
||||
export type GlobalAlert = z.output<typeof globalAlertSchema>
|
||||
export type Alert = z.output<typeof alertSchema>
|
||||
|
||||
Reference in New Issue
Block a user