From f01bd44c445ef4cbc0328fef87885954ab133d88 Mon Sep 17 00:00:00 2001 From: Arvid Norlin Date: Mon, 29 Apr 2024 16:40:26 +0200 Subject: [PATCH] fix: align typings with loyalty-page branch --- components/MyPages/AccountPage/Content.tsx | 6 +- .../AccountPageContentDynamicContent.graphql | 1 + .../AccountPageContentShortcuts.graphql | 1 + .../AccountPageContentTextContent.graphql | 11 +++ .../contentstack/accountPage/output.ts | 76 ++++++++++++++----- .../routers/contentstack/accountPage/query.ts | 70 ++++++++++++++++- types/requests/myPages/accountpage.ts | 4 +- 7 files changed, 142 insertions(+), 27 deletions(-) diff --git a/components/MyPages/AccountPage/Content.tsx b/components/MyPages/AccountPage/Content.tsx index 2adafd6ba..c53e236a1 100644 --- a/components/MyPages/AccountPage/Content.tsx +++ b/components/MyPages/AccountPage/Content.tsx @@ -1,5 +1,4 @@ import JsonToHtml from "@/components/JsonToHtml" -import { renderOptions } from "@/components/JsonToHtml/renderOptions" import CurrentBenefitsBlock from "@/components/MyPages/Blocks/Benefits/CurrentLevel" import NextLevelBenefitsBlock from "@/components/MyPages/Blocks/Benefits/NextLevel" import Overview from "@/components/MyPages/Blocks/Overview" @@ -83,9 +82,10 @@ export default function Content({ lang, content }: ContentProps) { return (
) diff --git a/lib/graphql/Fragments/MyPages/AccountPage/AccountPageContentDynamicContent.graphql b/lib/graphql/Fragments/MyPages/AccountPage/AccountPageContentDynamicContent.graphql index 75a3517c6..c7abc5fa0 100644 --- a/lib/graphql/Fragments/MyPages/AccountPage/AccountPageContentDynamicContent.graphql +++ b/lib/graphql/Fragments/MyPages/AccountPage/AccountPageContentDynamicContent.graphql @@ -9,6 +9,7 @@ fragment AccountPageContentDynamicContent on AccountPageContentDynamicContent { link { link_text linkConnection { + totalCount edges { node { ...LoyaltyPageLink diff --git a/lib/graphql/Fragments/MyPages/AccountPage/AccountPageContentShortcuts.graphql b/lib/graphql/Fragments/MyPages/AccountPage/AccountPageContentShortcuts.graphql index 817954557..4b8076973 100644 --- a/lib/graphql/Fragments/MyPages/AccountPage/AccountPageContentShortcuts.graphql +++ b/lib/graphql/Fragments/MyPages/AccountPage/AccountPageContentShortcuts.graphql @@ -18,6 +18,7 @@ fragment AccountPageContentShortcuts on AccountPageContentShortcuts { ...ContentPageLink } } + totalCount } } } diff --git a/lib/graphql/Fragments/MyPages/AccountPage/AccountPageContentTextContent.graphql b/lib/graphql/Fragments/MyPages/AccountPage/AccountPageContentTextContent.graphql index 074994a49..5ce1f9280 100644 --- a/lib/graphql/Fragments/MyPages/AccountPage/AccountPageContentTextContent.graphql +++ b/lib/graphql/Fragments/MyPages/AccountPage/AccountPageContentTextContent.graphql @@ -1,6 +1,17 @@ +#import "../../Image.graphql" + fragment AccountPageContentTextContent on AccountPageContentTextContent { text_content { content { + embedded_itemsConnection { + totalCount + edges { + node { + __typename + ...Image + } + } + } json } } diff --git a/server/routers/contentstack/accountPage/output.ts b/server/routers/contentstack/accountPage/output.ts index a9a5896dc..65df04bb4 100644 --- a/server/routers/contentstack/accountPage/output.ts +++ b/server/routers/contentstack/accountPage/output.ts @@ -1,9 +1,14 @@ import { z } from "zod" +import { Lang } from "@/constants/languages" + +import { Embeds } from "@/types/requests/embeds" import { ContentEntries, DynamicContentComponents, } from "@/types/requests/myPages/accountpage" +import { Edges } from "@/types/requests/utils/edges" +import { RTEDocument } from "@/types/rte/node" const accountPageShortcuts = z.object({ __typename: z.literal(ContentEntries.AccountPageContentShortcuts), @@ -20,13 +25,14 @@ const accountPageShortcuts = z.object({ node: z.object({ system: z.object({ uid: z.string(), - locale: z.string(), + locale: z.nativeEnum(Lang), }), url: z.string(), title: z.string(), }), }) ), + totalCount: z.number(), }), text: z.string().optional(), open_in_new_tab: z.boolean(), @@ -41,24 +47,25 @@ const accountPageDynamicContent = z.object({ title: z.string().optional(), preamble: z.string().optional(), component: z.nativeEnum(DynamicContentComponents), - link: z - .object({ - linkConnection: z.object({ - edges: z.array( - z.object({ - node: z.object({ - system: z.object({ - uid: z.string(), - locale: z.string(), - }), - url: z.string(), - title: z.string(), + link: z.object({ + linkConnection: z.object({ + edges: z.array( + z.object({ + node: z.object({ + system: z.object({ + uid: z.string(), + locale: z.nativeEnum(Lang), }), - }) - ), - }), - }) - .optional(), + url: z.string(), + title: z.string(), + }), + }) + ), + totalCount: z.number(), + }), + link_text: z.string(), + }), + // .optional(), }), }) @@ -75,11 +82,32 @@ const accountPageTextContent = z.object({ __typename: z.literal(ContentEntries.AccountPageContentTextContent), text_content: z.object({ content: z.object({ - json: jsonSchema, + json: z.any(), + embedded_itemsConnection: z.object({ + edges: z.array(z.any()), + totalCount: z.number(), + }), }), }), }) +type TextContentRaw = z.infer + +type DynamicContentRaw = z.infer + +type ShortcutsRaw = z.infer + +export type RteTextContent = Omit & { + text_content: { + content: { + json: RTEDocument + embedded_itemsConnection: Edges + } + } +} + +type AccountPageContentItem = DynamicContentRaw | ShortcutsRaw | RteTextContent + const accountPageContentItem = z.discriminatedUnion("__typename", [ accountPageShortcuts, accountPageDynamicContent, @@ -120,3 +148,13 @@ export const validateAccountPageBenefitsSchema = z.object({ ), }), }) + +type AccountPageDataRaw = z.infer + +type AccountPageRaw = AccountPageDataRaw["all_account_page"]["items"][0] + +export type AccountPage = Omit & { + url: string + title: string + content: AccountPageContentItem[] +} diff --git a/server/routers/contentstack/accountPage/query.ts b/server/routers/contentstack/accountPage/query.ts index 14aedac76..631354207 100644 --- a/server/routers/contentstack/accountPage/query.ts +++ b/server/routers/contentstack/accountPage/query.ts @@ -4,9 +4,15 @@ import { badRequestError, internalServerError } from "@/server/errors/trpc" import { publicProcedure, router } from "@/server/trpc" import { getAccountPageInput } from "./input" -import { validateAccountPageSchema } from "./output" +import { type AccountPage, validateAccountPageSchema } from "./output" -import type { GetAccountPageData } from "@/types/requests/myPages/accountpage" +import { Embeds } from "@/types/requests/embeds" +import { + ContentEntries, + GetAccountPageData, +} from "@/types/requests/myPages/accountpage" +import { Edges } from "@/types/requests/utils/edges" +import { RTEDocument } from "@/types/rte/node" export const accountPageQueryRouter = router({ getOverview: publicProcedure @@ -30,8 +36,36 @@ export const accountPageQueryRouter = router({ if (!validatedAccountPage.success) { throw badRequestError() } + // TODO: Make returned data nicer + const content = + validatedAccountPage.data.all_account_page.items[0].content.map( + (block) => { + switch (block.__typename) { + case ContentEntries.AccountPageContentDynamicContent: + case ContentEntries.AccountPageContentShortcuts: + return block + case ContentEntries.AccountPageContentTextContent: + return { + ...block, + text_content: { + content: { + json: block.text_content.content.json as RTEDocument, + embedded_itemsConnection: block.text_content.content + .embedded_itemsConnection as Edges, + }, + }, + } + default: + return null + } + } + ) - return response.data.all_account_page.items[0] + const accountPage = { + ...validatedAccountPage.data.all_account_page.items[0], + content, + } as AccountPage + return accountPage } catch (error) { console.info(`Get Account Page Overview Error`) console.error(error) @@ -60,7 +94,35 @@ export const accountPageQueryRouter = router({ throw badRequestError() } - return response.data.all_account_page.items[0] + const content = + validatedAccountPage.data.all_account_page.items[0].content.map( + (block) => { + switch (block.__typename) { + case ContentEntries.AccountPageContentDynamicContent: + case ContentEntries.AccountPageContentShortcuts: + return block + case ContentEntries.AccountPageContentTextContent: + return { + ...block, + text_content: { + content: { + json: block.text_content.content.json as RTEDocument, + embedded_itemsConnection: block.text_content.content + .embedded_itemsConnection as Edges, + }, + }, + } + default: + return null + } + } + ) + + const accountPage = { + ...validatedAccountPage.data.all_account_page.items[0], + content, + } as AccountPage + return accountPage } catch (error) { console.info(`Get Account Page Benefits Error`) console.error(error) diff --git a/types/requests/myPages/accountpage.ts b/types/requests/myPages/accountpage.ts index b25cc295b..340f3bda1 100644 --- a/types/requests/myPages/accountpage.ts +++ b/types/requests/myPages/accountpage.ts @@ -1,3 +1,4 @@ +import { Embeds } from "../embeds" import { AllRequestResponse } from "../utils/all" import { PageLink } from "../utils/pageLink" @@ -23,7 +24,7 @@ export enum ContentEntries { type Shortcut = { linkConnection: Edges open_in_new_tab: boolean - text: string + text?: string } type DynamicContent = { @@ -52,6 +53,7 @@ type AccountPageContentTextContent = Typename< text_content: { content: { json: RTEDocument + embedded_itemsConnection: Edges } } },