Refactoring, move merging routes with settings menu items to view file

This commit is contained in:
Angelina Filippova 2021-01-29 00:21:19 +03:00
parent 17f4ac6291
commit 584c456b43
2 changed files with 22 additions and 39 deletions

View file

@ -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()
})
}
}

View file

@ -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 })
}
}
}