# Render Menu from the backend

Able Pro is already rendering the menu from the backend. The dashboard menus (Default, Analytics) are rendered via the back end.

You can check the Fack backend API here that we used to render the menu:&#x20;

{% embed url="<https://github.com/phoenixcoded20/mock-data-api-nextjs>" %}

{% tabs %}
{% tab title="NEXT  (TS)" %}
To add a menu from the backend, you can follow the steps below:

1. Open the file **`menu.ts`** *(src/api/menu.ts)*, and edit the API URL in the **useGetMenu** function

{% code title="src/api/menu.ts" %}

```typescript
const endpoints = {
  key: 'api/menu',
  master: 'master',
  dashboard: '/dashboard' // server URL
};

export function useGetMenu() {
  const { data, isLoading, error, isValidating } = useSWR(endpoints.key + endpoints.dashboard, fetcher, {
    revalidateIfStale: false,
    revalidateOnFocus: false,
    revalidateOnReconnect: false
  });

  const memoizedValue = useMemo(() => {
    let updatedMenu = data?.dashboard;

    if (updatedMenu && Array.isArray(updatedMenu.children) && updatedMenu.children.length > 0) {
      updatedMenu = {
        ...updatedMenu,
        children: updatedMenu.children.map((group: NavItemType) => {
          if (Array.isArray(group.children)) {
            return {
              ...group,
              children: [...group.children, staticMenuItem]
            };
          }
          return group;
        })
      };
    }

    return {
      menu: updatedMenu as NavItemType,
      menuLoading: isLoading,
      menuError: error,
      menuValidating: isValidating,
      menuEmpty: !isLoading && !data?.length
    };
  }, [data, error, isLoading, isValidating]);

  return memoizedValue;
}
```

{% endcode %}

2. Add a file **`menu.tsx`** in the src/menu-items, and copy the code from **`dashboard.tsx`** (src/menu-items/). Set icons and local files according to the API response.
3. Open the file **`index.tsx`** *(*&#x73;rc/layout/Dashboard/Drawer/DrawerContent/Navigation/index.ts&#x78;*)*, and add the code below the line.

{% code title="src/layout/Dashboard/Drawer/DrawerContent/Navigation/index.tsx" %}

```typescript
import { useGetMenu } from 'api/menu';
import menuItem from 'menu-items';
import { MenuFromAPI } from 'menu-items/dashboard';

function isFound(arr: any, str: string) {
  return arr.items.some((element: any) => {
    if (element.id === str) {
      return true;
    }
    return false;
  });
}

// ==============================|| DRAWER CONTENT - NAVIGATION ||============================== //

export default function Navigation() {
  const { menuLoading } = useGetMenu();
  
  const [menuItems, setMenuItems] = useState<{ items: NavItemType[] }>({ items: [] });

  const dashboardMenu = MenuFromAPI();
  useLayoutEffect(() => {
    if (menuLoading && !isFound(menuItem, 'group-dashboard-loading')) {
      menuItem.items.splice(0, 0, dashboardMenu);
      setMenuItems({ items: [...menuItem.items] });
    } else if (!menuLoading && dashboardMenu?.id !== undefined && !isFound(menuItem, 'group-dashboard')) {
      menuItem.items.splice(0, 1, dashboardMenu);
      setMenuItems({ items: [...menuItem.items] });
    } else {
      setMenuItems({ items: [...menuItem.items] });
    }
    // eslint-disable-next-line
  }, [menuLoading]);

  ...
}
```

{% endcode %}
{% endtab %}

{% tab title="NEXT  (JS)" %}
To add a menu from the backend, you can follow the steps below:

1. Open the file **`menu.js`** *(src/api/menu.js)*, and edit the API URL in the **useGetMenu** function

{% code title="src/api/menu.js" %}

```javascript
const endpoints = {
  key: 'api/menu',
  dashboard: '/dashboard' // server URL
};

export function useGetMenu() {
  const { data, isLoading, error, isValidating } = useSWR(endpoints.key + endpoints.dashboard, fetcher, {
    revalidateIfStale: false,
    revalidateOnFocus: false,
    revalidateOnReconnect: false
  });

  const memoizedValue = useMemo(() => {
    let updatedMenu = data?.dashboard;

    if (updatedMenu && Array.isArray(updatedMenu.children) && updatedMenu.children.length > 0) {
      updatedMenu = {
        ...updatedMenu,
        children: updatedMenu.children.map((group) => {
          if (Array.isArray(group.children)) {
            return {
              ...group,
              children: [...group.children, staticMenuItem]
            };
          }
          return group;
        })
      };
    }

    return {
      menu: updatedMenu,
      menuLoading: isLoading,
      menuError: error,
      menuValidating: isValidating,
      menuEmpty: !isLoading && !data?.length
    };
  }, [data, error, isLoading, isValidating]);

  return memoizedValue;
}
```

{% endcode %}

2. Add a file **`menu.jsx`** in the src/menu-items, and copy the code from **`dashboard.jsx`** (src/menu-items/). Set icons and local files according to the API response.
3. Open the file **`index.jsx`** *(*&#x73;rc/layout/Dashboard/Drawer/DrawerContent/Navigation/index.js&#x78;*)*, and add the code below the line.

{% code title="src/layout/Dashboard/Drawer/DrawerContent/Navigation/index.jsx" %}

```javascript
import { useGetMenu } from 'api/menu';
import menuItem from 'menu-items';
import { MenuFromAPI } from 'menu-items/dashboard';

function isFound(arr, str) {
  return arr.items.some((element) => {
    if (element.id === str) {
      return true;
    }
    return false;
  });
}

// ==============================|| DRAWER CONTENT - NAVIGATION ||============================== //

export default function Navigation() {
  const { menuLoading } = useGetMenu();
  
  const [menuItems, setMenuItems] = useState({ items: [] });

  const dashboardMenu = MenuFromAPI();
  useLayoutEffect(() => {
    if (menuLoading && !isFound(menuItem, 'group-dashboard-loading')) {
      menuItem.items.splice(0, 0, dashboardMenu);
      setMenuItems({ items: [...menuItem.items] });
    } else if (!menuLoading && dashboardMenu?.id !== undefined && !isFound(menuItem, 'group-dashboard')) {
      menuItem.items.splice(0, 1, dashboardMenu);
      setMenuItems({ items: [...menuItem.items] });
    } else {
      setMenuItems({ items: [...menuItem.items] });
    }
    // eslint-disable-next-line
  }, [menuLoading]);

  ...
}
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://phoenixcoded.gitbook.io/able-pro/nextjs/how-to/render-menu-from-the-backend.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
