../src/layout/Dashboard/Drawer/DrawerContent/Navigation/index.tsx
Copy import { Fragment , useLayoutEffect , useState } from 'react' ;
...
import menuItem from 'menu-items' ;
import { useGetMenu , useGetMenuMaster } from 'api/menu' ;
../src/layout/Dashboard/Drawer/DrawerContent/Navigation/index.tsx
Copy import { Fragment , useState } from 'react' ;
...
import menuItems from 'menu-items' ;
import { useGetMenuMaster } from 'api/menu' ;
../src/layout/Dashboard/Drawer/DrawerContent/Navigation/index.tsx
Copy 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 ;
});
}
export default function Navigation () {
...
const { menuLoading } = useGetMenu ();
...
const [ menuItems , setMenuItems ] = useState <{ items : NavItemType [] }>({ items : [] });
let 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]);
}
Copy import { MenuProps , NavItemType } from 'types/menu' ;
Copy import { MenuProps } from 'types/menu' ;
Copy import { fetcher } from 'utils/axios' ;
export const endpoints = {
...
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;
}