Mantis MUI React
v3.5.0
v3.5.0
  • Documentation
  • Pre-requisites
  • Quick Start
  • Package
  • Folder Structure
  • State Management
  • Internationalization
  • Authentication
    • Switch to Auth0
    • Switch to Firebase
    • Switch to AWS Cognito
    • Switch to Supabase
  • Axios API Calls
  • Routing
  • Project Configuration
  • Color Presets
  • Theme/Style Configuration
  • How to
    • Login as First Page
    • Dashboard as First Page
    • Render Menu from the backend
    • Remove menu render from Backend
    • Remove Authentication
      • Vite
      • NextJS
  • Figma
  • Integration
    • Seed
    • To Existing Project
    • Comparison
  • Components
    • Avatar
    • BreadCrumb
    • Button
    • Dot
    • Main Card
    • Progress
    • SnackBar
    • Tooltip
    • Transitions
  • Dependencies
  • Roadmap
  • Support
  • Changelog
  • Mantis Eco System
  • FAQ
Powered by GitBook
On this page
  1. How to

Render Menu from the backend

Render menu via backend

PreviousDashboard as First PageNextRemove menu render from Backend

Mantis 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:

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.

src/api/menu.ts
...

import { fetcher } from 'utils/axios';

// types
import { MenuProps, NavItemType } from 'types/menu';

export 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: 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;
}
...
  1. Create Menu File

    1. Add file menu.tsx in src/menu-items, and copy code from dashboard.tsx (src/menu-items/). Set icons and local files according to API response.

  2. Open the file index.tsx (src/layout/Dashboard/Drawer/DrawerContent/Navigation/index.tsx), and add the below code of the line.

src/layout/Dashboard/Drawer/DrawerContent/Navigation/index.tsx

import { useLayoutEffect, useState } from 'react'
...

import menuItem from 'menu-items';
import { MenuFromAPI } from 'menu-items/menu';
import { useGetMenu, useGetMenuMaster } from 'api/menu';
...

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

export default function Navigation() {

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

let dashboardMenu = MenuFromAPI();

useLayoutEffect(() => {
    const isFound = menuItem.items.some((element) => {
      if (element.id === 'group-dashboard') {
        return true;
      }
      return false;
    });

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

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

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.

src/api/menu.ts
...

import { fetcher } from 'utils/axios';

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) => {
          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;
}

....
  1. Create Menu File

    1. Add file menu.tsx in src/menu-items, and copy code from dashboard.tsx (src/menu-items/). Set icons and local files according to API response.

  2. Open the file index.tsx (src/layout/Dashboard/Drawer/DrawerContent/Navigation/index.tsx), and add the below code of the line.

src/layout/Dashboard/Drawer/DrawerContent/Navigation/index.tsx

import { useLayoutEffect, useState } from 'react'
...

import menuItem from 'menu-items';
import { MenuFromAPI } from 'menu-items/menu';
import { useGetMenu, useGetMenuMaster } from 'api/menu';
...

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

export default function Navigation() {

...
const { menuLoading } = useGetMenu();
...
const [menuItems, setMenuItems] = useState({ items: [] });

let dashboardMenu = MenuFromAPI();

useLayoutEffect(() => {
    const isFound = menuItem.items.some((element) => {
      if (element.id === 'group-dashboard') {
        return true;
      }
      return false;
    });

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

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

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.

src/api/menu.ts
...

import { fetcher } from 'utils/axios';

// types
import { MenuProps, NavItemType } from 'types/menu';

export 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: 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;
}
...
  1. Create Menu File

    1. Add file menu.tsx in src/menu-items, and copy code from dashboard.tsx (src/menu-items/). Set icons and local files according to API response.

  2. Open the file index.tsx (src/layout/Dashboard/Drawer/DrawerContent/Navigation/index.tsx), and add the below code of the line.

src/layout/Dashboard/Drawer/DrawerContent/Navigation/index.tsx

import { useLayoutEffect, useState } from 'react'
...

import menuItem from 'menu-items';
import { MenuFromAPI } from 'menu-items/menu';
import { useGetMenu, useGetMenuMaster } from 'api/menu';
...

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

export default function Navigation() {

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

let dashboardMenu = MenuFromAPI();

useLayoutEffect(() => {
    const isFound = menuItem.items.some((element) => {
      if (element.id === 'group-dashboard') {
        return true;
      }
      return false;
    });

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

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

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.

src/api/menu.ts
...

import { fetcher } from 'utils/axios';

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) => {
          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;
}

....
  1. Create Menu File

    1. Add file menu.tsx in src/menu-items, and copy code from dashboard.tsx (src/menu-items/). Set icons and local files according to API response.

  2. Open the file index.tsx (src/layout/Dashboard/Drawer/DrawerContent/Navigation/index.tsx), and add the below code of the line.

src/layout/Dashboard/Drawer/DrawerContent/Navigation/index.tsx

import { useLayoutEffect, useState } from 'react'
...

import menuItem from 'menu-items';
import { MenuFromAPI } from 'menu-items/menu';
import { useGetMenu, useGetMenuMaster } from 'api/menu';
...

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

export default function Navigation() {

...
const { menuLoading } = useGetMenu();
...
const [menuItems, setMenuItems] = useState({ items: [] });

let dashboardMenu = MenuFromAPI();

useLayoutEffect(() => {
    const isFound = menuItem.items.some((element) => {
      if (element.id === 'group-dashboard') {
        return true;
      }
      return false;
    });

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

You can check the Fack backend API here that we used to render the menu:

You can check the Fack backend API here that we used to render the menu:

You can check the Fack backend API here that we used to render the menu:

https://github.com/phoenixcoded20/mock-data-api-nextjs
https://github.com/phoenixcoded20/mock-data-api-nextjs
https://github.com/phoenixcoded20/mock-data-api-nextjs
https://github.com/phoenixcoded20/mock-data-api-nextjs