diff --git a/astro/src/content/menu/menu.ts b/astro/src/content/menu/menu.ts new file mode 100644 index 0000000..9d557df --- /dev/null +++ b/astro/src/content/menu/menu.ts @@ -0,0 +1,39 @@ +import { createDirectusConnection } from "@/lib/directus"; +import { print } from "graphql"; +import getMenuQuery from "@/graphql/menu/getMenu.graphql"; + +export async function getMenu(): Promise { + const client = await createDirectusConnection(); + const result = await client.query(print(getMenuQuery)); + + const menuRecord = result['Menu']; + + let menu: Menu = { + id: menuRecord['id'], + items: [] + }; + + menuRecord['items'].forEach((menuItem: any) => { + if (menuItem['collection'] === "Menu_Column") { + let menuColumnItem: MenuColumn = { + id: menuItem['item']['id'], + type: "Column", + title: menuItem['item']['title'], + links: [] + }; + + menuItem['item']['links'].forEach((menuItemLink: any) => { + menuColumnItem.links.push({ + id: menuItemLink['id'], + type: "Link", + text: menuItemLink['text'], + url: menuItemLink['url'] + }); + }); + + menu.items.push(menuColumnItem); + } + }); + + return menu; +} \ No newline at end of file diff --git a/astro/src/graphql/menu/getMenu.graphql b/astro/src/graphql/menu/getMenu.graphql new file mode 100644 index 0000000..9b57f28 --- /dev/null +++ b/astro/src/graphql/menu/getMenu.graphql @@ -0,0 +1,35 @@ +query getMenu { + Menu { + id, + date_created, + date_updated, + items { + id, + item { + ...on Menu_Column { + __typename, + id, + date_created, + date_updated, + title, + links { + id, + date_created, + date_updated, + text, + url + } + } + + ...on Menu_Link { + __typename, + id, + date_created, + date_updated, + text, + url + } + } + } + } +} \ No newline at end of file diff --git a/astro/src/types/menu/menu.d.ts b/astro/src/types/menu/menu.d.ts new file mode 100644 index 0000000..adbb856 --- /dev/null +++ b/astro/src/types/menu/menu.d.ts @@ -0,0 +1,20 @@ +type Menu = { + id: string; + items: MenuColumn[]; +} + +type MenuColumn = { + type: "Column"; + + id: string; + title: string; + links: MenuLink[]; +} + +type MenuLink = { + type: "Link"; + + id: string; + text: string; + url: string; +}