From 631a82ee0459b269a11dfb4af3429453e70df45e Mon Sep 17 00:00:00 2001 From: Arvid Norlin Date: Tue, 9 Apr 2024 12:50:02 +0200 Subject: [PATCH] fix: refine url logic based on typename --- .../(live)/(protected)/my-pages/layout.tsx | 18 +++++-- lib/graphql/Query/NavigationMyPages.graphql | 50 +++++++++---------- types/requests/myPages/navigation.ts | 33 +++++++++--- 3 files changed, 66 insertions(+), 35 deletions(-) diff --git a/app/[lang]/(live)/(protected)/my-pages/layout.tsx b/app/[lang]/(live)/(protected)/my-pages/layout.tsx index fc73b48c5..c801cecfc 100644 --- a/app/[lang]/(live)/(protected)/my-pages/layout.tsx +++ b/app/[lang]/(live)/(protected)/my-pages/layout.tsx @@ -11,6 +11,8 @@ import { GetNavigationMyPagesData, NavigationItem, MenuItem, + PageLink, + PageLinkEnum, } from "@/types/requests/myPages/navigation" import { GetNavigationMyPages } from "@/lib/graphql/Query/NavigationMyPages.graphql" @@ -26,16 +28,26 @@ export default async function MyPagesLayout({ ) function mapMenuItems(navigationItems: NavigationItem[]) { + function getURL(node: PageLink) { + switch (node.__typename) { + case PageLinkEnum.ContentPage: + return node.web.url + case PageLinkEnum.CodeDefinedPage: + return node.url + } + } + return navigationItems.map(({ item }): MenuItem => { + const { node } = item.pageConnection.edges[0] const { title, - url, system: { uid }, - } = item.pageConnection.edges[0].node + } = node + return { title, uid, - url, + url: getURL(node), linkText: item.link_text, subItems: item.sub_items ? mapMenuItems(item.sub_items) : null, } diff --git a/lib/graphql/Query/NavigationMyPages.graphql b/lib/graphql/Query/NavigationMyPages.graphql index 2fce36070..7a11ced7e 100644 --- a/lib/graphql/Query/NavigationMyPages.graphql +++ b/lib/graphql/Query/NavigationMyPages.graphql @@ -1,3 +1,21 @@ +fragment CodeDefinedPage on CodeDefinedPage { + title + url + system { + uid + } +} + +fragment ContentPage on ContentPage { + title + web { + url + } + system { + uid + } +} + query GetNavigationMyPages { all_navigation_my_pages { items { @@ -14,19 +32,9 @@ query GetNavigationMyPages { pageConnection { edges { node { - ... on CodeDefinedPage { - title - url - system { - uid - } - } - ... on ContentPage { - title - system { - uid - } - } + __typename + ...CodeDefinedPage + ...ContentPage } } } @@ -36,19 +44,9 @@ query GetNavigationMyPages { pageConnection { edges { node { - ... on ContentPage { - title - system { - uid - } - } - ... on CodeDefinedPage { - title - url - system { - uid - } - } + __typename + ...ContentPage + ...CodeDefinedPage } } } diff --git a/types/requests/myPages/navigation.ts b/types/requests/myPages/navigation.ts index e11cfc0cb..e54dc2e9b 100644 --- a/types/requests/myPages/navigation.ts +++ b/types/requests/myPages/navigation.ts @@ -1,5 +1,11 @@ import type { AllRequestResponse } from "../utils/all" import { Edges } from "../utils/edges" +import { Typename } from "../utils/typename" + +export enum PageLinkEnum { + CodeDefinedPage = "CodeDefinedPage", + ContentPage = "ContentPage", +} export type MenuItem = { uid: string @@ -11,12 +17,27 @@ export type MenuItem = { export type SidebarProps = { menuItems: MenuItem[] } -export type PageLink = { - uid: string - title: string - url: string - system: { uid: string } -} +export type CodeDefinedPageLink = Typename< + { + uid: string + title: string + url: string + system: { uid: string } + }, + PageLinkEnum.CodeDefinedPage +> + +export type ContentPageLink = Typename< + { + uid: string + title: string + web: { url: string } + system: { uid: string } + }, + PageLinkEnum.ContentPage +> + +export type PageLink = CodeDefinedPageLink | ContentPageLink export type NavigationItem = { item: {