This commit is contained in:
Andrey Kuvshinov
2025-12-13 23:29:25 +03:00
parent 40aa3d7814
commit 80fb06e420
8 changed files with 574 additions and 60 deletions

View File

@@ -1,50 +1,71 @@
import { fetchGraphQL } from './graphql-client.js';
import type { MenuItem } from '../types/graphql.js';
interface MenuItemNode {
uri: string;
url: string;
order: number;
label: string;
}
interface MenuNode {
name: string;
menuItems: {
nodes: MenuItemNode[];
};
}
// Функция ДЛЯ ГЛАВНОГО МЕНЮ (максимум 5 пунктов)
export async function getMainHeaderMenu(): Promise<MenuItem[]> {
const query = `
query GetMainHeaderMenu {
menu(id: "103245", idType: DATABASE_ID) {
menuItems(
first: 5, # Берем только 5
where: {parentId: null} # Только верхний уровень
) {
nodes {
id
label
url
target
order
cssClasses
interface MenusResponse {
menus: {
nodes: MenuNode[];
};
}
/**
* Get navigation menu from WordPress
*/
export async function navQuery(): Promise<MenusResponse> {
try {
const query = `{
menus(where: {location: PRIMARY}) {
nodes {
name
menuItems {
nodes {
uri
url
order
label
}
}
}
}
}
`;
}`;
try {
const data = await fetchGraphQL(query);
const items = data.menu?.menuItems?.nodes || [];
// Сортируем по order и гарантируем максимум 5
return items
.sort((a: MenuItem, b: MenuItem) => a.order - b.order)
.slice(0, 5);
} catch (error) {
console.error('Ошибка загрузки главного меню:', error);
// Запасной вариант на случай ошибки (ровно 5 пунктов)
return [
{ id: '1', label: 'Главная', url: '/', order: 0 },
{ id: '2', label: 'Каталог', url: '/catalog', order: 1 },
{ id: '3', label: 'О нас', url: '/about', order: 2 },
{ id: '4', label: 'Контакты', url: '/contacts', order: 3 },
{ id: '5', label: 'Блог', url: '/blog', order: 4 }
];
}
return await executeQuery<MenusResponse>(query, {}, "navigation");
} catch (error) {
log.error("Error fetching nav: " + error);
// Return fallback data for development
return {
menus: {
nodes: [
{
name: "Primary",
menuItems: {
nodes: [
{ uri: "/", url: "/", order: 1, label: "Home" },
{ uri: "/about/", url: "/about/", order: 2, label: "About" },
{
uri: "/contact/",
url: "/contact/",
order: 3,
label: "Contact",
},
],
},
},
],
},
};
}
}