Able Pro
NextJS
NextJS
  • ✨Overview
  • 🌴Pre-requisites
  • 🚀Quick Start
  • 📂Folder Structure
  • 🌀State Management
  • 🔒Authentication
  • 📡Axios API Calls
  • 🌐Internationalization
  • 🛠️Project Configuration
  • 🎨Color Presets
  • 🎭Theme/Style Configuration
  • 📚How to
    • Login as First Page
    • Dashboard as First Page
    • Remove menu render from Backend
    • Render Menu from the backend
    • Remove Auth
  • 🖌️Figma
  • 🤝Integration
    • Seed
    • Comparison
  • 📦Dependencies
  • 🗓️Roadmap
  • 🆘Support
  • 📅Changelog
Powered by GitBook
On this page
  1. How to

Render Menu from the backend

Render menu via backend

PreviousRemove menu render from BackendNextRemove Auth

Last updated 2 months ago

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:

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
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(
    () => ({
      menu: data?.dashboard as NavItemType,
      menuLoading: isLoading,
      menuError: error,
      menuValidating: isValidating,
      menuEmpty: !isLoading && !data?.length
    }),
    [data, error, isLoading, isValidating]
  );

  return memoizedValue;
}
  1. Add file menu.tsx in src/menu-items, and copy the 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 { useGetMenu } from 'api/menu';

import menuItem from 'menu-items';

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: [] });

  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]);

  ...
}

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

src/api/menu.js
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(
    () => ({
      menu: data?.dashboard,
      menuLoading: isLoading,
      menuError: error,
      menuValidating: isValidating,
      menuEmpty: !isLoading && !data?.length
    }),
    [data, error, isLoading, isValidating]
  );

  return memoizedValue;
}
  1. Add file menu.jsx in src/menu-items, and copy the code from dashboard.jsx (src/menu-items/). Set icons and local files according to API response.

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

src/layout/Dashboard/Drawer/DrawerContent/Navigation/index.jsx
import { useGetMenu } from 'api/menu';

import menuItem from 'menu-items';

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: [] });

  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]);

  ...
}
📚
GitHub - phoenixcoded20/mock-data-api-nextjsGitHub
Logo