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

@@ -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
}
}
}

View File

@@ -1,10 +0,0 @@
fragment GlobalAlert on GlobalAlert {
type
heading
text
phone_contact {
display_text
phone_number
footnote
}
}

View File

@@ -1,4 +1,4 @@
#import "../Fragments/GlobalAlert.graphql" #import "../Fragments/Alert.graphql"
query GetSiteConfig($locale: String!) { query GetSiteConfig($locale: String!) {
all_site_config(limit: 1, locale: $locale) { all_site_config(limit: 1, locale: $locale) {
@@ -8,7 +8,7 @@ query GetSiteConfig($locale: String!) {
alertConnection { alertConnection {
edges { edges {
node { node {
...GlobalAlert ...Alert
} }
} }
} }

View File

@@ -14,8 +14,8 @@ import { removeMultipleSlashes } from "@/utils/url"
import { systemSchema } from "../schemas/system" import { systemSchema } from "../schemas/system"
import { Image } from "@/types/image" import { AlertTypeEnum } from "@/types/enums/alert"
import { GlobalAlertType } from "@/types/trpc/routers/contentstack/siteConfig" import type { Image } from "@/types/image"
// Help me write this zod schema based on the type ContactConfig // Help me write this zod schema based on the type ContactConfig
export const validateContactConfigSchema = z.object({ export const validateContactConfigSchema = z.object({
@@ -664,16 +664,75 @@ export const headerSchema = z
} }
}) })
export const globalAlertSchema = z.object({ export const alertSchema = z
type: z.nativeEnum(GlobalAlertType), .object({
type: z.nativeEnum(AlertTypeEnum),
text: z.string(), text: z.string(),
heading: z.string(), heading: z.string(),
phone_contact: z.object({ phone_contact: z.object({
display_text: z.string().nullable(), display_text: z.string(),
phone_number: z.string().nullable(), phone_number: z.string().nullable(),
footnote: 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 export const siteConfigSchema = z
.object({ .object({
@@ -687,7 +746,7 @@ export const siteConfigSchema = z
edges: z edges: z
.array( .array(
z.object({ z.object({
node: globalAlertSchema, node: alertSchema,
}) })
) )
.max(1), .max(1),
@@ -701,7 +760,7 @@ export const siteConfigSchema = z
.transform((data) => { .transform((data) => {
if (!data.all_site_config.items.length) { if (!data.all_site_config.items.length) {
return { return {
globalAlert: null, sitewideAlert: null,
bookingWidgetDisabled: false, bookingWidgetDisabled: false,
} }
} }
@@ -709,7 +768,7 @@ export const siteConfigSchema = z
const { sitewide_alert } = data.all_site_config.items[0] const { sitewide_alert } = data.all_site_config.items[0]
return { return {
globalAlert: sitewide_alert.alertConnection.edges[0]?.node || null, sitewideAlert: sitewide_alert.alertConnection.edges[0]?.node || null,
bookingWidgetDisabled: sitewide_alert.booking_widget_disabled, bookingWidgetDisabled: sitewide_alert.booking_widget_disabled,
} }
}) })

View File

@@ -67,9 +67,9 @@ import {
getSiteConfigSuccessCounter, getSiteConfigSuccessCounter,
} from "./telemetry" } from "./telemetry"
import { import {
getAlertPhoneContactData,
getConnections, getConnections,
getFooterConnections, getFooterConnections,
getGlobalAlertPhoneContactData,
} from "./utils" } from "./utils"
import type { import type {
@@ -667,15 +667,15 @@ export const baseQueryRouter = router({
JSON.stringify({ query: { lang } }) JSON.stringify({ query: { lang } })
) )
const { globalAlert } = validatedSiteConfig.data const { sitewideAlert } = validatedSiteConfig.data
return { return {
...validatedSiteConfig.data, ...validatedSiteConfig.data,
globalAlert: globalAlert sitewideAlert: sitewideAlert
? { ? {
...globalAlert, ...sitewideAlert,
phone_contact: contactConfig phone_contact: contactConfig
? getGlobalAlertPhoneContactData(globalAlert, contactConfig) ? getAlertPhoneContactData(sitewideAlert, contactConfig)
: null, : null,
} }
: null, : null,

View File

@@ -7,7 +7,7 @@ import { System } from "@/types/requests/system"
import { Edges } from "@/types/requests/utils/edges" import { Edges } from "@/types/requests/utils/edges"
import { NodeRefs } from "@/types/requests/utils/refs" import { NodeRefs } from "@/types/requests/utils/refs"
import type { HeaderRefs } from "@/types/trpc/routers/contentstack/header" 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) { export function getConnections({ header }: HeaderRefs) {
const connections: System["system"][] = [header.system] const connections: System["system"][] = [header.system]
@@ -71,18 +71,16 @@ export function getFooterConnections(refs: FooterRefDataRaw) {
return connections return connections
} }
export function getGlobalAlertPhoneContactData( export function getAlertPhoneContactData(
globalAlert: SiteConfig["globalAlert"], alert: Alert,
contactConfig: ContactConfig contactConfig: ContactConfig
) { ) {
if (globalAlert?.phone_contact) { if (alert.phoneContact) {
const { display_text, phone_number, footnote } = globalAlert.phone_contact const { displayText, phoneNumber, footnote } = alert.phoneContact
return { return {
display_text, displayText,
phone: phone_number phoneNumber: getValueFromContactConfig(phoneNumber, contactConfig),
? getValueFromContactConfig(phone_number, contactConfig)
: null,
footnote: footnote footnote: footnote
? getValueFromContactConfig(footnote, contactConfig) ? getValueFromContactConfig(footnote, contactConfig)
: null, : null,

5
types/enums/alert.ts Normal file
View File

@@ -0,0 +1,5 @@
export enum AlertTypeEnum {
Info = "info",
Warning = "warning",
Alarm = "alarm",
}

View File

@@ -1,17 +1,11 @@
import { z } from "zod" import { z } from "zod"
import { import {
globalAlertSchema, alertSchema,
siteConfigSchema, siteConfigSchema,
} from "@/server/routers/contentstack/base/output" } from "@/server/routers/contentstack/base/output"
export enum GlobalAlertType {
Info = "info",
Alarm = "alarm",
}
export type GetSiteConfigData = z.input<typeof siteConfigSchema> export type GetSiteConfigData = z.input<typeof siteConfigSchema>
export type SiteConfig = z.output<typeof siteConfigSchema> export type SiteConfig = z.output<typeof siteConfigSchema>
export type GetGlobalAlertData = z.input<typeof globalAlertSchema> export type Alert = z.output<typeof alertSchema>
export type GlobalAlert = z.output<typeof globalAlertSchema>