fix: refs for account page

This commit is contained in:
Christel Westerberg
2024-05-15 11:06:52 +02:00
parent d651ea526c
commit 863d99ad44
5 changed files with 210 additions and 7 deletions

View File

@@ -2,6 +2,11 @@
#import "../Fragments/MyPages/AccountPage/AccountPageContentShortcuts.graphql"
#import "../Fragments/MyPages/AccountPage/AccountPageContentTextContent.graphql"
#import "../Fragments/Refs/AccountPage.graphql"
#import "../Fragments/Refs/ContentPage.graphql"
#import "../Fragments/Refs/LoyaltyPage.graphql"
#import "../Fragments/Refs/System.graphql"
query GetAccountPage($locale: String!, $url: String!) {
all_account_page(limit: 1, locale: $locale, where: { url: $url }) {
items {
@@ -17,3 +22,48 @@ query GetAccountPage($locale: String!, $url: String!) {
total
}
}
query GetAccountPageRefs($locale: String!, $url: String!) {
all_account_page(limit: 1, locale: $locale, where: { url: $url }) {
items {
content {
... on AccountPageContentDynamicContent {
__typename
dynamic_content {
link {
linkConnection {
edges {
node {
__typename
...AccountPageRef
...LoyaltyPageRef
}
}
}
}
}
}
... on AccountPageContentShortcuts {
__typename
shortcuts {
shortcuts {
linkConnection {
edges {
node {
__typename
...AccountPageRef
...ContentPageRef
...LoyaltyPageRef
}
}
}
}
}
}
}
system {
...System
}
}
}
}

View File

@@ -7,6 +7,7 @@ import {
DynamicContentComponents,
} from "@/types/components/myPages/myPage/enums"
import { Embeds } from "@/types/requests/embeds"
import { PageLinkEnum } from "@/types/requests/pageLinks"
import { Edges } from "@/types/requests/utils/edges"
import { RTEDocument } from "@/types/rte/node"
@@ -139,3 +140,61 @@ export type AccountPage = Omit<AccountPageRaw, "content"> & {
title: string
content: AccountPageContentItem[]
}
// Refs types
const pageConnectionRefs = z.object({
edges: z.array(
z.object({
node: z.object({
__typename: z.nativeEnum(PageLinkEnum),
system: z.object({
content_type_uid: z.string(),
uid: z.string(),
}),
}),
})
),
})
const accountPageShortcutsRefs = z.object({
__typename: z.literal(ContentEntries.AccountPageContentShortcuts),
shortcuts: z.object({
shortcuts: z.array(
z.object({
linkConnection: pageConnectionRefs,
})
),
}),
})
const accountPageDynamicContentRefs = z.object({
__typename: z.literal(ContentEntries.AccountPageContentDynamicContent),
dynamic_content: z.object({
link: z.object({
linkConnection: pageConnectionRefs,
}),
}),
})
const accountPageContentItemRefs = z.discriminatedUnion("__typename", [
accountPageDynamicContentRefs,
accountPageShortcutsRefs,
])
export const validateAccountPageRefsSchema = z.object({
all_account_page: z.object({
items: z.array(
z.object({
content: z.array(accountPageContentItemRefs),
system: z.object({
content_type_uid: z.string(),
uid: z.string(),
}),
})
),
}),
})
export type AccountPageRefsDataRaw = z.infer<
typeof validateAccountPageRefsSchema
>

View File

@@ -1,10 +1,26 @@
import GetAccountPage from "@/lib/graphql/Query/AccountPage.graphql"
import {
GetAccountPage,
GetAccountPageRefs,
} from "@/lib/graphql/Query/AccountPage.graphql"
import { request } from "@/lib/graphql/request"
import { badRequestError, internalServerError } from "@/server/errors/trpc"
import { publicProcedure, router } from "@/server/trpc"
import { removeEmptyObjects } from "@/utils/contentType"
import {
generateRefsResponseTag,
generateTag,
generateTags,
} from "@/utils/generateTag"
import { getAccountPageInput } from "./input"
import { type AccountPage, validateAccountPageSchema } from "./output"
import {
type AccountPage,
AccountPageRefsDataRaw,
validateAccountPageRefsSchema,
validateAccountPageSchema,
} from "./output"
import { getConnections } from "./utils"
import { ContentEntries } from "@/types/components/myPages/myPage/enums"
import { Embeds } from "@/types/requests/embeds"
@@ -14,10 +30,55 @@ import { RTEDocument } from "@/types/rte/node"
export const accountPageQueryRouter = router({
get: publicProcedure.input(getAccountPageInput).query(async ({ input }) => {
try {
const response = await request<AccountPage>(GetAccountPage, {
locale: input.lang,
url: input.url,
})
const { lang, url } = input
const refsResponse = await request<AccountPageRefsDataRaw>(
GetAccountPageRefs,
{
locale: lang,
url,
},
{
next: {
tags: [generateRefsResponseTag(lang, "account_page")],
},
}
)
if (!refsResponse.data) {
console.error("Bad response for `GetAccountPageRefs`")
console.error({ refsResponse })
throw internalServerError()
}
const cleanedData = removeEmptyObjects(refsResponse.data)
const validatedAccountPageRefs =
validateAccountPageRefsSchema.safeParse(cleanedData)
if (!validatedAccountPageRefs.success) {
console.error("Bad validation for `GetAccountPageRefs`")
console.error(validatedAccountPageRefs.error)
throw badRequestError()
}
const connections = getConnections(validatedAccountPageRefs.data)
const tags = generateTags(lang, connections)
tags.push(
generateTag(
lang,
validatedAccountPageRefs.data.all_account_page.items[0].system.uid
)
)
const response = await request<AccountPage>(
GetAccountPage,
{
locale: lang,
url,
},
{ next: { tags } }
)
if (!response.data) {
throw badRequestError()

View File

@@ -0,0 +1,33 @@
import { AccountPageRefsDataRaw } from "./output"
import { ContentEntries } from "@/types/components/myPages/myPage/enums"
import type { Edges } from "@/types/requests/utils/edges"
import type { NodeRefs } from "@/types/requests/utils/refs"
export function getConnections(refs: AccountPageRefsDataRaw) {
const connections: Edges<NodeRefs>[] = []
refs.all_account_page.items.forEach((ref) => {
if (ref.content) {
ref.content.forEach((item) => {
switch (item.__typename) {
case ContentEntries.AccountPageContentShortcuts: {
item.shortcuts.shortcuts.forEach((shortcut) => {
if (shortcut.linkConnection.edges.length) {
connections.push(shortcut.linkConnection)
}
})
break
}
case ContentEntries.AccountPageContentDynamicContent: {
if (item.dynamic_content.link.linkConnection.edges.length) {
connections.push(item.dynamic_content.link.linkConnection)
}
break
}
}
})
}
})
return connections
}

View File

@@ -51,7 +51,7 @@ export const loyaltyPageQueryRouter = router({
)
if (!refsResponse.data) {
console.error("Bad response for `GetNavigationMyPagesRefs`")
console.error("Bad response for `GetLoyaltyPageRefs`")
console.error({ refsResponse })
throw internalServerError()
}