feat(SW-66, SW-348): search functionality and ui

This commit is contained in:
Simon Emanuelsson
2024-08-28 10:47:57 +02:00
parent b9dbcf7d90
commit af850c90e7
437 changed files with 7663 additions and 9881 deletions
@@ -1,106 +1,62 @@
import { removeMultipleSlashes } from "@/utils/url"
import { LoyaltyPageEnum } from "@/types/enums/loyaltyPage"
import type { System } from "@/types/requests/system"
import type { LoyaltyPageRefs } from "@/types/trpc/routers/contentstack/loyaltyPage"
import { LoyaltyPageRefsDataRaw } from "./output"
export function getConnections({ loyalty_page }: LoyaltyPageRefs) {
const connections: System["system"][] = [loyalty_page.system]
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"
export function getConnections(refs: LoyaltyPageRefsDataRaw) {
const connections: Edges<NodeRefs>[] = []
if (refs.loyalty_page.blocks) {
refs.loyalty_page.blocks.forEach((item) => {
switch (item.__typename) {
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksContent: {
if (item.content.content.embedded_itemsConnection.edges.length) {
connections.push(item.content.content.embedded_itemsConnection)
if (loyalty_page.blocks) {
loyalty_page.blocks.forEach((block) => {
switch (block.__typename) {
case LoyaltyPageEnum.ContentStack.blocks.CardsGrid:
if (block.cards_grid.length) {
connections.push(...block.cards_grid)
}
break
}
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksCardsGrid: {
connections.push(item.cards_grid.cardConnection)
item.cards_grid.cardConnection.edges.forEach((card) => {
switch (card.node.__typename) {
case LoyaltyCardsGridEnum.LoyaltyCard: {
if (card.node.link) {
connections.push(card.node.link?.linkConnection)
}
break
}
case LoyaltyCardsGridEnum.Card: {
if (card.node.primary_button) {
connections.push(card.node.primary_button?.linkConnection)
} else if (card.node.secondary_button) {
connections.push(card.node.secondary_button?.linkConnection)
}
break
}
}
})
break
}
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksShortcuts: {
item.shortcuts.shortcuts.forEach((shortcut) => {
if (shortcut.linkConnection.edges.length) {
connections.push(shortcut.linkConnection)
}
})
break
}
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksDynamicContent: {
if (item.dynamic_content.link.pageConnection.edges.length) {
connections.push(item.dynamic_content.link.pageConnection)
case LoyaltyPageEnum.ContentStack.blocks.Content:
if (block.content.length) {
// TS has trouble infering the filtered types
// @ts-ignore
connections.push(...block.content)
}
break
}
case LoyaltyPageEnum.ContentStack.blocks.DynamicContent:
if (block.dynamic_content.link) {
connections.push(block.dynamic_content.link)
}
break
case LoyaltyPageEnum.ContentStack.blocks.Shortcuts:
if (block.shortcuts.shortcuts.length) {
connections.push(...block.shortcuts.shortcuts)
}
break
default:
break
}
})
}
if (refs.loyalty_page.sidebar) {
refs.loyalty_page.sidebar?.forEach((item) => {
switch (item.__typename) {
case SidebarTypenameEnum.LoyaltyPageSidebarContent:
if (item.content.content.embedded_itemsConnection.edges.length) {
connections.push(item.content.content.embedded_itemsConnection)
if (loyalty_page.sidebar) {
loyalty_page.sidebar.forEach((block) => {
switch (block?.__typename) {
case LoyaltyPageEnum.ContentStack.sidebar.Content:
if (block.content.length) {
// TS has trouble infering the filtered types
// @ts-ignore
connections.push(...block.content)
}
break
case SidebarTypenameEnum.LoyaltyPageSidebarJoinLoyaltyContact:
if (item.join_loyalty_contact.button?.linkConnection) {
connections.push(item.join_loyalty_contact.button.linkConnection)
case LoyaltyPageEnum.ContentStack.sidebar.JoinLoyaltyContact:
if (block.join_loyalty_contact?.button) {
connections.push(block.join_loyalty_contact.button)
}
break
default:
break
}
})
}
return connections
}
export function makeButtonObject(button: any) {
if (!button) return null
const isContenstackLink =
button?.is_contentstack_link || button.linkConnection?.edges?.length
const linkConnnectionNode = isContenstackLink
? button.linkConnection.edges[0]?.node
: null
return {
openInNewTab: button?.open_in_new_tab,
title:
button.cta_text ||
(linkConnnectionNode
? linkConnnectionNode.title
: button.external_link.title),
href: linkConnnectionNode
? linkConnnectionNode.web?.original_url ||
removeMultipleSlashes(
`/${linkConnnectionNode.system.locale}/${linkConnnectionNode.url}`
)
: button.external_link.href,
isExternal: !isContenstackLink,
}
}