From 584c456b436bb1134d82993ff2adddd6ab689c34 Mon Sep 17 00:00:00 2001 From: Angelina Filippova Date: Fri, 29 Jan 2021 00:21:19 +0300 Subject: [PATCH] Refactoring, move merging routes with settings menu items to view file --- src/store/modules/permission.js | 44 +++---------------- src/views/layout/components/Sidebar/index.vue | 17 ++++++- 2 files changed, 22 insertions(+), 39 deletions(-) diff --git a/src/store/modules/permission.js b/src/store/modules/permission.js index c214e9ce..71ef5b5d 100644 --- a/src/store/modules/permission.js +++ b/src/store/modules/permission.js @@ -40,48 +40,18 @@ const permission = { addRouters: [] }, mutations: { - SET_ADD_ROUTERS: (state, routers) => { - state.addRouters = routers - }, SET_ROUTERS: (state, routers) => { - state.routers = routers + state.addRouters = routers + state.routers = constantRouterMap.concat(routers) } }, actions: { - GenerateRoutes({ commit }, { roles, routes }) { + GenerateRoutes({ commit }, { roles, _routesWithSettings }) { return new Promise(resolve => { - if (!routes) { - let accessedRouters - if (roles.includes('admin')) { - accessedRouters = asyncRouterMap - } else { - accessedRouters = filterAsyncRouter(asyncRouterMap, roles) - } - commit('SET_ADD_ROUTERS', accessedRouters) - commit('SET_ROUTERS', constantRouterMap.concat(accessedRouters)) - resolve() - } else { - const updRoutes = routes.reduce((acc, element) => { - if (acc.findIndex(route => route.path === element.path) !== -1) { - return acc - } else if (element.parent) { - const index = acc.findIndex(route => route.path === element.parent.path) - if (index > -1) { - const children = acc[index].children ? [...acc[index].children, element] : [element] - acc[index] = { ...acc[index], children } - return acc - } else { - const newRoute = { ...element.parent, children: [element] } - return [...acc, newRoute] - } - } else { - return [...acc, element] - } - }, []) - - commit('SET_ROUTERS', updRoutes) - resolve() - } + const routes = _routesWithSettings || asyncRouterMap + const accessedRouters = roles.includes('admin') ? routes : filterAsyncRouter(asyncRouterMap, roles) + commit('SET_ROUTERS', accessedRouters) + resolve() }) } } diff --git a/src/views/layout/components/Sidebar/index.vue b/src/views/layout/components/Sidebar/index.vue index 73c7a98b..c0e140dd 100644 --- a/src/views/layout/components/Sidebar/index.vue +++ b/src/views/layout/components/Sidebar/index.vue @@ -19,6 +19,7 @@ import { mapGetters } from 'vuex' import SidebarItem from './SidebarItem' import variables from '@/styles/variables.scss' import router from '@/router' +import { asyncRouterMap } from '@/router' export default { components: { SidebarItem }, @@ -40,6 +41,18 @@ export default { this.$store.dispatch('FetchOpenReportsCount') }, methods: { + getMergedRoutes() { + const routes = router.getRoutes().filter(item => !item.hidden) + return routes.reduce((acc, element) => { + if (!element.parent || element.parent.path !== '/settings') { + return acc + } else { + const index = acc.findIndex(route => route.path === '/settings') + acc[index] = { ...acc[index], children: [...acc[index].children, element] } + return acc + } + }, [...asyncRouterMap]) + }, async handleOpen($event) { if ($event === '/settings') { if (!localStorage.getItem('settingsTabs')) { @@ -55,8 +68,8 @@ export default { meta: { title: label } }) }) - const routes = router.getRoutes().filter(item => !item.hidden) - this.$store.dispatch('GenerateRoutes', { roles: this.roles, routes }) + const routes = this.getMergedRoutes() + this.$store.dispatch('GenerateRoutes', { roles: this.roles, _routesWithSettings: routes }) } } }