From 15b499dc24eff1331b4ea26ee5d4d3b461176804 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Thu, 22 Nov 2018 08:27:46 +0800 Subject: [PATCH] refactor: use a properer method name for menu data (#2937) --- src/layouts/BasicLayout.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/layouts/BasicLayout.js b/src/layouts/BasicLayout.js index 06c403ea..4b43e4d6 100644 --- a/src/layouts/BasicLayout.js +++ b/src/layouts/BasicLayout.js @@ -20,9 +20,8 @@ import Exception403 from '../pages/Exception/403'; const { Content } = Layout; -// Conversion router to menu. -function formatter(data, parentAuthority, parentName) { - return data +function mapRoutesToMenu(routes, parentAuthority, parentName) { + return routes .map(item => { if (!item.name || !item.path) { return null; @@ -42,7 +41,7 @@ function formatter(data, parentAuthority, parentName) { authority: item.authority || parentAuthority, }; if (item.routes) { - const children = formatter(item.routes, item.authority, locale); + const children = mapRoutesToMenu(item.routes, item.authority, locale); // Reduce memory usage result.children = children; } @@ -52,7 +51,7 @@ function formatter(data, parentAuthority, parentName) { .filter(item => item); } -const memoizeOneFormatter = memoizeOne(formatter, isEqual); +const memoizedMapRoutesToMenu = memoizeOne(mapRoutesToMenu, isEqual); const query = { 'screen-xs': { @@ -145,7 +144,7 @@ class BasicLayout extends React.PureComponent { const { route: { routes, authority }, } = this.props; - return memoizeOneFormatter(routes, authority); + return memoizedMapRoutesToMenu(routes, authority); } /** @@ -154,16 +153,16 @@ class BasicLayout extends React.PureComponent { */ getBreadcrumbNameMap() { const routerMap = {}; - const mergeMenuAndRouter = data => { + const flattenMenuData = data => { data.forEach(menuItem => { if (menuItem.children) { - mergeMenuAndRouter(menuItem.children); + flattenMenuData(menuItem.children); } // Reduce memory usage routerMap[menuItem.path] = menuItem; }); }; - mergeMenuAndRouter(this.getMenuData()); + flattenMenuData(this.getMenuData()); return routerMap; } -- GitLab