fix: get joinloyalty button from contentstack
This commit is contained in:
@@ -152,6 +152,14 @@ const loyaltyPageJoinLoyaltyContact = z.object({
|
||||
join_loyalty_contact: z.object({
|
||||
title: z.string().nullable(),
|
||||
preamble: z.string().nullable(),
|
||||
button: z
|
||||
.object({
|
||||
openInNewTab: z.boolean(),
|
||||
title: z.string(),
|
||||
href: z.string(),
|
||||
isExternal: z.boolean(),
|
||||
})
|
||||
.nullable(),
|
||||
contact: z.array(
|
||||
z.object({
|
||||
__typename: z.literal(
|
||||
@@ -362,8 +370,22 @@ const loyaltyPageSidebarTextContentRef = z.object({
|
||||
}),
|
||||
})
|
||||
|
||||
const loyaltyPageSidebarJoinLoyaltyContactRef = z.object({
|
||||
__typename: z.literal(
|
||||
SidebarTypenameEnum.LoyaltyPageSidebarJoinLoyaltyContact
|
||||
),
|
||||
join_loyalty_contact: z.object({
|
||||
button: z
|
||||
.object({
|
||||
linkConnection: pageConnectionRefs,
|
||||
})
|
||||
.nullable(),
|
||||
}),
|
||||
})
|
||||
|
||||
const loyaltyPageSidebarRefsItem = z.discriminatedUnion("__typename", [
|
||||
loyaltyPageSidebarTextContentRef,
|
||||
loyaltyPageSidebarJoinLoyaltyContactRef,
|
||||
])
|
||||
|
||||
export const validateLoyaltyPageRefsSchema = z.object({
|
||||
|
||||
@@ -27,6 +27,7 @@ import { InsertResponse } from "@/types/components/imageVaultImage"
|
||||
import {
|
||||
LoyaltyBlocksTypenameEnum,
|
||||
LoyaltyCardsGridEnum,
|
||||
SidebarTypenameEnum,
|
||||
} from "@/types/components/loyalty/enums"
|
||||
|
||||
function makeImageVaultImage(image: any) {
|
||||
@@ -36,21 +37,25 @@ function makeImageVaultImage(image: any) {
|
||||
}
|
||||
|
||||
function makeButtonObject(button: any) {
|
||||
if (!button) return null
|
||||
|
||||
const isContenstackLink =
|
||||
button?.is_contentstack_link || button.linkConnection?.edges?.length
|
||||
|
||||
return {
|
||||
openInNewTab: button.open_in_new_tab,
|
||||
openInNewTab: button?.open_in_new_tab,
|
||||
title:
|
||||
button.cta_text ||
|
||||
(button.is_contentstack_link && button.linkConnection.edges.length
|
||||
(isContenstackLink
|
||||
? button.linkConnection.edges[0].node.title
|
||||
: button.external_link.title),
|
||||
href:
|
||||
button.is_contentstack_link && button.linkConnection.edges.length
|
||||
? button.linkConnection.edges[0].node.web?.original_url ||
|
||||
removeMultipleSlashes(
|
||||
`/${button.linkConnection.edges[0].node.system.locale}/${button.linkConnection.edges[0].node.url}`
|
||||
)
|
||||
: button.external_link.href,
|
||||
isExternal: !button.is_contentstack_link,
|
||||
href: isContenstackLink
|
||||
? button.linkConnection.edges[0].node.web?.original_url ||
|
||||
removeMultipleSlashes(
|
||||
`/${button.linkConnection.edges[0].node.system.locale}/${button.linkConnection.edges[0].node.url}`
|
||||
)
|
||||
: button.external_link.href,
|
||||
isExternal: !isContenstackLink,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,11 +160,7 @@ export const loyaltyPageQueryRouter = router({
|
||||
return {
|
||||
...card,
|
||||
image: makeImageVaultImage(card.image),
|
||||
link: makeButtonObject({
|
||||
...card.link,
|
||||
is_contentstack_link:
|
||||
!!card.link.linkConnection.edges.length,
|
||||
}),
|
||||
link: makeButtonObject(card.link),
|
||||
}
|
||||
case LoyaltyCardsGridEnum.Card:
|
||||
return {
|
||||
@@ -185,11 +186,28 @@ export const loyaltyPageQueryRouter = router({
|
||||
})
|
||||
: null
|
||||
|
||||
const sidebar = response.data.loyalty_page.sidebar
|
||||
? response.data.loyalty_page.sidebar.map((item: any) => {
|
||||
switch (item.__typename) {
|
||||
case SidebarTypenameEnum.LoyaltyPageSidebarJoinLoyaltyContact:
|
||||
return {
|
||||
...item,
|
||||
join_loyalty_contact: {
|
||||
...item.join_loyalty_contact,
|
||||
button: makeButtonObject(item.join_loyalty_contact.button),
|
||||
},
|
||||
}
|
||||
default:
|
||||
return item
|
||||
}
|
||||
})
|
||||
: null
|
||||
|
||||
const loyaltyPage = {
|
||||
heading: response.data.loyalty_page.heading,
|
||||
system: response.data.loyalty_page.system,
|
||||
blocks,
|
||||
sidebar: response.data.loyalty_page.sidebar,
|
||||
sidebar,
|
||||
}
|
||||
|
||||
const validatedLoyaltyPage =
|
||||
|
||||
@@ -3,6 +3,7 @@ import { LoyaltyPageRefsDataRaw } from "./output"
|
||||
import {
|
||||
LoyaltyBlocksTypenameEnum,
|
||||
LoyaltyCardsGridEnum,
|
||||
SidebarTypenameEnum,
|
||||
} from "@/types/components/loyalty/enums"
|
||||
import type { Edges } from "@/types/requests/utils/edges"
|
||||
import type { NodeRefs } from "@/types/requests/utils/refs"
|
||||
@@ -59,8 +60,17 @@ export function getConnections(refs: LoyaltyPageRefsDataRaw) {
|
||||
}
|
||||
if (refs.loyalty_page.sidebar) {
|
||||
refs.loyalty_page.sidebar?.forEach((item) => {
|
||||
if (item.content.content.embedded_itemsConnection.edges.length) {
|
||||
connections.push(item.content.content.embedded_itemsConnection)
|
||||
switch (item.__typename) {
|
||||
case SidebarTypenameEnum.LoyaltyPageSidebarContent:
|
||||
if (item.content.content.embedded_itemsConnection.edges.length) {
|
||||
connections.push(item.content.content.embedded_itemsConnection)
|
||||
}
|
||||
break
|
||||
case SidebarTypenameEnum.LoyaltyPageSidebarJoinLoyaltyContact:
|
||||
if (item.join_loyalty_contact.button?.linkConnection) {
|
||||
connections.push(item.join_loyalty_contact.button.linkConnection)
|
||||
}
|
||||
break
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user