feat: useMenuItems
This commit is contained in:
@@ -1,3 +1,7 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
const { data: menuItems } = await useMenuItems({ location: "MAIN" });
|
||||||
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<UHeader mode="slideover">
|
<UHeader mode="slideover">
|
||||||
<template #left>
|
<template #left>
|
||||||
@@ -5,5 +9,9 @@
|
|||||||
<SvgSiteLogo class="h-10 w-auto" />
|
<SvgSiteLogo class="h-10 w-auto" />
|
||||||
</NuxtLink>
|
</NuxtLink>
|
||||||
</template>
|
</template>
|
||||||
|
<UNavigationMenu :items="menuItems" content-orientation="vertical" />
|
||||||
|
<template #body>
|
||||||
|
<UNavigationMenu :items="menuItems" orientation="vertical" class="-mx-2.5" />
|
||||||
|
</template>
|
||||||
</UHeader>
|
</UHeader>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { delay } from "es-toolkit/promise";
|
import { delay } from "es-toolkit";
|
||||||
|
|
||||||
import type { ButtonProps } from "@nuxt/ui";
|
import type { ButtonProps } from "@nuxt/ui";
|
||||||
import type { FormSubmitEvent } from "@nuxt/ui";
|
import type { FormSubmitEvent } from "@nuxt/ui";
|
||||||
|
|||||||
19
wp-content/themes/headless/app/composables/useMenuItems.gql
Normal file
19
wp-content/themes/headless/app/composables/useMenuItems.gql
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
fragment MenuItem on MenuItem {
|
||||||
|
id
|
||||||
|
label @nullToUndefined
|
||||||
|
to: path @nullToUndefined
|
||||||
|
target
|
||||||
|
}
|
||||||
|
|
||||||
|
query MenuItems($location: MenuLocationEnum) {
|
||||||
|
menuItems(where: { location: $location, parentDatabaseId: 0 }) {
|
||||||
|
nodes {
|
||||||
|
...MenuItem
|
||||||
|
childItems {
|
||||||
|
nodes {
|
||||||
|
...MenuItem
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
10
wp-content/themes/headless/app/composables/useMenuItems.ts
Normal file
10
wp-content/themes/headless/app/composables/useMenuItems.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import type { MenuItemsVariables } from "#graphql/types";
|
||||||
|
|
||||||
|
export const useMenuItems = (variables: MenuItemsVariables) =>
|
||||||
|
useAsyncGraphQLQuery("MenuItems", variables, {
|
||||||
|
transform: ({ menuItems }) =>
|
||||||
|
extractNodes(menuItems).map(({ childItems, ...menuItem }) => ({
|
||||||
|
...menuItem,
|
||||||
|
children: extractNodes(childItems),
|
||||||
|
})),
|
||||||
|
});
|
||||||
@@ -43,10 +43,10 @@ export async function handleLogout(event: H3Event) {
|
|||||||
* @param user The AuthUserFragment containing user data from the GraphQL response
|
* @param user The AuthUserFragment containing user data from the GraphQL response
|
||||||
* @returns A User object with the expected structure for nuxt-auth-utils, including an array of role names
|
* @returns A User object with the expected structure for nuxt-auth-utils, including an array of role names
|
||||||
*/
|
*/
|
||||||
function getAuthUser(user: AuthUserFragment) {
|
function getAuthUser({ roles, ...user }: AuthUserFragment) {
|
||||||
return {
|
return {
|
||||||
...user,
|
...user,
|
||||||
roles: user.roles.nodes.map(({ name }) => name) || [],
|
roles: extractNodes(roles).map(({ name }) => name),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
4
wp-content/themes/headless/shared/utils/graphql.ts
Normal file
4
wp-content/themes/headless/shared/utils/graphql.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
// Helper: Extracts nodes from a GraphQL connection object, returning an empty array if nodes are absent.
|
||||||
|
export function extractNodes<T>(connection: { nodes?: T[] } | null | undefined): T[] {
|
||||||
|
return connection?.nodes ?? [];
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user