From de025fc88972358da49c1911867e6a6da4a68bb8 Mon Sep 17 00:00:00 2001 From: Arvid Norlin Date: Mon, 8 Apr 2024 10:49:31 +0200 Subject: [PATCH] fix: add navigation subitems --- .../(live)/(protected)/my-pages/layout.tsx | 29 +++++++----- components/MyPages/Sidebar/index.tsx | 17 +++++-- lib/graphql/Query/NavigationMyPages.graphql | 44 ++++++++++++++----- types/requests/myPages/navigation.ts | 16 +++++-- 4 files changed, 78 insertions(+), 28 deletions(-) diff --git a/app/[lang]/(live)/(protected)/my-pages/layout.tsx b/app/[lang]/(live)/(protected)/my-pages/layout.tsx index 09f39ce78..2b1aff1f2 100644 --- a/app/[lang]/(live)/(protected)/my-pages/layout.tsx +++ b/app/[lang]/(live)/(protected)/my-pages/layout.tsx @@ -7,7 +7,11 @@ import styles from "./layout.module.css" import type { LangParams, LayoutArgs } from "@/types/params" import { request } from "@/lib/graphql/request" -import { GetNavigationMyPagesData } from "@/types/requests/myPages/navigation" +import { + GetNavigationMyPagesData, + NavigationItem, + MenuItem, +} from "@/types/requests/myPages/navigation" import { GetNavigationMyPages } from "@/lib/graphql/Query/NavigationMyPages.graphql" export default async function MyPagesLayout({ @@ -20,16 +24,21 @@ export default async function MyPagesLayout({ locale: params.lang, } ) - const navigation = response.data.all_navigation_my_pages.items[0] - const menuItems = navigation.items.map(({ item }) => { - const { title, uid } = item.pageConnection.edges[0].node - return { - title, - uid, - linkText: item.link_text, - } - }) + function mapMenuItems(navigationItems: NavigationItem[]) { + return navigationItems.map(({ item }): MenuItem => { + const { title, uid } = item.pageConnection.edges[0].node + return { + title, + uid, + linkText: item.link_text, + subItems: item.sub_items ? mapMenuItems(item.sub_items) : null, + } + }) + } + + const navigation = response.data.all_navigation_my_pages.items[0] + const menuItems = mapMenuItems(navigation.items) return (
diff --git a/components/MyPages/Sidebar/index.tsx b/components/MyPages/Sidebar/index.tsx index 19c80655b..cc191f97d 100644 --- a/components/MyPages/Sidebar/index.tsx +++ b/components/MyPages/Sidebar/index.tsx @@ -13,9 +13,20 @@ export default function Sidebar({ menuItems }: SidebarProps) { */} {/* Base styles.active on menuItem href and current path */} {menuItems.map((item) => ( - - {item.linkText || item.title} - + <> + + {item.linkText || item.title} + + {item.subItems + ? item.subItems.map((subItem) => { + return ( + + {subItem.linkText || subItem.title} + + ) + }) + : null} + ))} diff --git a/lib/graphql/Query/NavigationMyPages.graphql b/lib/graphql/Query/NavigationMyPages.graphql index 32209370a..61a85d584 100644 --- a/lib/graphql/Query/NavigationMyPages.graphql +++ b/lib/graphql/Query/NavigationMyPages.graphql @@ -5,25 +5,45 @@ query GetNavigationMyPages { ... on NavigationMyPagesItemsItem { __typename item { - pageConnection { - edges { - node { - ... on CodeDefinedPage { - title - } - ... on ContentPage { - title + link_text + sub_items { + ... on NavigationMyPagesItemsItemBlockSubItemsItem { + __typename + item { + link_text + pageConnection { + edges { + node { + ... on CodeDefinedPage { + title + url + } + ... on ContentPage { + title + } + } + } + } + } + } + } + pageConnection { + edges { + node { + ... on ContentPage { + title + } + ... on CodeDefinedPage { + title + url } } } } - link_text } } } - system { - uid - } + title } } } diff --git a/types/requests/myPages/navigation.ts b/types/requests/myPages/navigation.ts index 2221b20a9..d92c61f58 100644 --- a/types/requests/myPages/navigation.ts +++ b/types/requests/myPages/navigation.ts @@ -1,14 +1,24 @@ import type { AllRequestResponse } from "../utils/all" import { Edges } from "../utils/edges" -export type MenuItem = { uid: string; title: string; linkText: string } +export type MenuItem = { + uid: string + title: string + linkText: string + subItems: MenuItem[] | null + url?: string +} export type SidebarProps = { menuItems: MenuItem[] } -export type PageLink = { uid: string; title: string } +export type PageLink = { uid: string; title: string; url?: string } export type NavigationItem = { - item: { pageConnection: Edges; link_text: string } + item: { + pageConnection: Edges + link_text: string + sub_items: NavigationItem[] | null + } } export type NavigationMyPages = { items: NavigationItem[] }