fix: refactor use of tokens
This commit is contained in:
@@ -21,9 +21,15 @@ const pageLink = z.object({
|
||||
uid: z.string(),
|
||||
locale: z.nativeEnum(Lang),
|
||||
}),
|
||||
web: z
|
||||
.object({
|
||||
original_url: z.string().nullable(),
|
||||
})
|
||||
.nullable()
|
||||
.optional(),
|
||||
url: z.string(),
|
||||
title: z.string(),
|
||||
__typename: z.string(),
|
||||
__typename: z.string().optional(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
@@ -39,21 +45,7 @@ const loyaltyPageDynamicContent = z.object({
|
||||
component: z.nativeEnum(LoyaltyComponentEnum),
|
||||
link: z.object({
|
||||
text: z.string().nullable(),
|
||||
pageConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
system: z.object({
|
||||
uid: z.string(),
|
||||
locale: z.nativeEnum(Lang),
|
||||
}),
|
||||
url: z.string(),
|
||||
title: z.string(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
totalCount: z.number(),
|
||||
}),
|
||||
pageConnection: pageLink,
|
||||
}),
|
||||
}),
|
||||
})
|
||||
@@ -65,27 +57,7 @@ const loyaltyPageShortcuts = z.object({
|
||||
preamble: z.string().nullable(),
|
||||
shortcuts: z.array(
|
||||
z.object({
|
||||
linkConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
system: z.object({
|
||||
uid: z.string(),
|
||||
locale: z.nativeEnum(Lang),
|
||||
}),
|
||||
url: z.string(),
|
||||
web: z
|
||||
.object({
|
||||
original_url: z.string().nullable(),
|
||||
})
|
||||
.nullable()
|
||||
.optional(),
|
||||
title: z.string(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
totalCount: z.number(),
|
||||
}),
|
||||
linkConnection: pageLink,
|
||||
text: z.string().nullable(),
|
||||
open_in_new_tab: z.boolean(),
|
||||
})
|
||||
@@ -193,7 +165,6 @@ const loyaltyPageJoinLoyaltyContact = z.object({
|
||||
),
|
||||
contact: z.object({
|
||||
display_text: z.string().nullable(),
|
||||
|
||||
contact_field: z.string(),
|
||||
}),
|
||||
})
|
||||
@@ -243,38 +214,29 @@ export interface RteBlockContent extends BlockContentRaw {
|
||||
|
||||
type CardsGridRaw = z.infer<typeof loyaltyPageCards>
|
||||
|
||||
export type CardsRaw =
|
||||
CardsGridRaw["cards_grid"]["cardConnection"]["edges"][number]["node"]
|
||||
|
||||
export type CardsGrid = Omit<CardsGridRaw, "cards_grid"> & {
|
||||
cards_grid: Omit<CardsGridRaw["cards_grid"], "cardConnection"> & {
|
||||
cards: {
|
||||
system: {
|
||||
locale: Lang
|
||||
uid: string
|
||||
}
|
||||
heading: string | null
|
||||
body_text: string | null
|
||||
scripted_top_title: string | null
|
||||
cards: (Omit<CardsRaw, "primaryButton" | "secondaryButton"> & {
|
||||
primaryButton:
|
||||
| {
|
||||
open_in_new_tab: boolean
|
||||
link: {
|
||||
title: string
|
||||
href: string
|
||||
}
|
||||
openInNewTab: boolean
|
||||
title: string
|
||||
href: string
|
||||
isExternal: boolean
|
||||
}
|
||||
| undefined
|
||||
secondaryButton:
|
||||
| {
|
||||
open_in_new_tab: boolean
|
||||
link: {
|
||||
title: string
|
||||
href: string
|
||||
}
|
||||
openInNewTab: boolean
|
||||
title: string
|
||||
href: string
|
||||
isExternal: boolean
|
||||
}
|
||||
| undefined
|
||||
background_image?: any
|
||||
}[]
|
||||
})[]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { _ } from "@/lib/translation"
|
||||
import { internalServerError, notFound } from "@/server/errors/trpc"
|
||||
import { contentstackProcedure, publicProcedure, router } from "@/server/trpc"
|
||||
import { contentstackProcedure, router } from "@/server/trpc"
|
||||
|
||||
import {
|
||||
generateRefsResponseTag,
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
|
||||
import { removeEmptyObjects } from "../../utils"
|
||||
import {
|
||||
CardsRaw,
|
||||
type LoyaltyPage,
|
||||
type LoyaltyPageDataRaw,
|
||||
type LoyaltyPageRefsDataRaw,
|
||||
@@ -31,6 +32,26 @@ import { Embeds } from "@/types/requests/embeds"
|
||||
import { Edges } from "@/types/requests/utils/edges"
|
||||
import { RTEDocument } from "@/types/rte/node"
|
||||
|
||||
function makeButtonObject(
|
||||
button: CardsRaw["primary_button" | "secondary_button"]
|
||||
) {
|
||||
if (!button) return undefined
|
||||
return {
|
||||
openInNewTab: button.open_in_new_tab,
|
||||
title:
|
||||
button.cta_text ||
|
||||
(button.is_contentstack_link && button.linkConnection.edges.length
|
||||
? 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 ||
|
||||
`/${button.linkConnection.edges[0].node.system.locale}${button.linkConnection.edges[0].node.url}`
|
||||
: button.external_link.href,
|
||||
isExternal: !button.is_contentstack_link,
|
||||
}
|
||||
}
|
||||
|
||||
export const loyaltyPageQueryRouter = router({
|
||||
get: contentstackProcedure.query(async ({ ctx }) => {
|
||||
const { lang, uid } = ctx
|
||||
@@ -121,7 +142,7 @@ export const loyaltyPageQueryRouter = router({
|
||||
...block,
|
||||
dynamic_content: {
|
||||
...block.dynamic_content,
|
||||
link: block.dynamic_content.link.pageConnection.totalCount
|
||||
link: block.dynamic_content.link.pageConnection.edges.length
|
||||
? {
|
||||
text: block.dynamic_content.link.text,
|
||||
href: `/${block.dynamic_content.link.pageConnection.edges[0].node.system.locale}${block.dynamic_content.link.pageConnection.edges[0].node.url}`,
|
||||
@@ -165,58 +186,12 @@ export const loyaltyPageQueryRouter = router({
|
||||
...block.cards_grid,
|
||||
cards: block.cards_grid.cardConnection.edges.map(
|
||||
({ node: card }) => {
|
||||
const primaryButton = card.primary_button
|
||||
? {
|
||||
open_in_new_tab:
|
||||
card.primary_button.open_in_new_tab,
|
||||
link: {
|
||||
title:
|
||||
card.primary_button.cta_text ||
|
||||
(card.primary_button.is_contentstack_link &&
|
||||
card.primary_button.linkConnection.edges.length
|
||||
? card.primary_button.linkConnection.edges[0]
|
||||
.node.title
|
||||
: card.primary_button.external_link.title),
|
||||
href:
|
||||
card.primary_button.is_contentstack_link &&
|
||||
card.primary_button.linkConnection.edges.length
|
||||
? `/${card.primary_button.linkConnection.edges[0].node.system.locale}${card.primary_button.linkConnection.edges[0].node.url}`
|
||||
: card.primary_button.external_link.href,
|
||||
},
|
||||
isExternal:
|
||||
!card.primary_button.is_contentstack_link,
|
||||
}
|
||||
: undefined
|
||||
|
||||
const secondaryButton = card.secondary_button
|
||||
? {
|
||||
open_in_new_tab:
|
||||
card.secondary_button.open_in_new_tab,
|
||||
link: {
|
||||
title:
|
||||
card.secondary_button.cta_text ||
|
||||
(card.secondary_button.is_contentstack_link &&
|
||||
card.secondary_button.linkConnection.edges
|
||||
.length
|
||||
? card.secondary_button.linkConnection
|
||||
.edges[0].node.title
|
||||
: card.secondary_button.external_link.title),
|
||||
href:
|
||||
card.secondary_button.is_contentstack_link &&
|
||||
card.secondary_button.linkConnection.edges
|
||||
.length
|
||||
? `/${card.secondary_button.linkConnection.edges[0].node.system.locale}${card.secondary_button.linkConnection.edges[0].node.url}`
|
||||
: card.secondary_button.external_link.title,
|
||||
},
|
||||
isExternal:
|
||||
!card.secondary_button.is_contentstack_link,
|
||||
}
|
||||
: undefined
|
||||
|
||||
return {
|
||||
...card,
|
||||
primaryButton,
|
||||
secondaryButton,
|
||||
primaryButton: makeButtonObject(card.primary_button),
|
||||
secondaryButton: makeButtonObject(
|
||||
card.secondary_button
|
||||
),
|
||||
}
|
||||
}
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user