feat: add Admin layout
This commit is contained in:
+37
-18
@@ -6,9 +6,21 @@ declare module 'vue-router' {
|
||||
public?: boolean
|
||||
screenKey?: string
|
||||
title?: string
|
||||
adminOnly?: boolean
|
||||
}
|
||||
}
|
||||
|
||||
function landingPath(): string {
|
||||
const auth = useAuthStore()
|
||||
return auth.isAdmin ? '/projects' : '/select-project'
|
||||
}
|
||||
|
||||
function fallbackLocation() {
|
||||
const last = localStorage.getItem('mws_last_project')
|
||||
if (last) return { name: 'tasks-board', params: { id: last } }
|
||||
return landingPath()
|
||||
}
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes: [
|
||||
@@ -25,21 +37,11 @@ const router = createRouter({
|
||||
},
|
||||
{
|
||||
path: '/',
|
||||
component: () => import('../layouts/MainLayout.vue'),
|
||||
component: () => import('../layouts/AdminLayout.vue'),
|
||||
children: [
|
||||
{ path: '', redirect: '/select-project' },
|
||||
{ path: '', redirect: () => landingPath() },
|
||||
{ path: 'projects', name: 'projects', component: () => import('../views/projects/ProjectsListView.vue'), meta: { screenKey: 'projects', title: 'Projects', adminOnly: true } },
|
||||
{ path: 'users', name: 'users', component: () => import('../views/users/UsersView.vue'), meta: { screenKey: 'users', title: 'Users' } },
|
||||
{
|
||||
path: 'projects/:id',
|
||||
component: () => import('../layouts/ProjectLayout.vue'),
|
||||
meta: { screenKey: 'projects' },
|
||||
children: [
|
||||
{ path: '', redirect: (to) => ({ name: 'tasks-board', params: { id: to.params.id } }) },
|
||||
{ path: 'documents', name: 'documents', component: () => import('../views/documents/DocumentsView.vue'), meta: { screenKey: 'documents' } },
|
||||
{ path: 'tasks/board', name: 'tasks-board', component: () => import('../views/tasks/TasksBoardView.vue'), meta: { screenKey: 'tasks' } },
|
||||
{ path: 'members', name: 'members', component: () => import('../views/projects/MembersView.vue') },
|
||||
],
|
||||
},
|
||||
{
|
||||
path: 'settings',
|
||||
component: () => import('../views/settings/SettingsView.vue'),
|
||||
@@ -53,7 +55,18 @@ const router = createRouter({
|
||||
},
|
||||
],
|
||||
},
|
||||
{ path: '/:pathMatch(.*)*', redirect: '/select-project' },
|
||||
{
|
||||
path: '/projects/:id',
|
||||
component: () => import('../layouts/MainLayout.vue'),
|
||||
meta: { screenKey: 'projects' },
|
||||
children: [
|
||||
{ path: '', redirect: (to) => ({ name: 'tasks-board', params: { id: to.params.id } }) },
|
||||
{ path: 'documents', name: 'documents', component: () => import('../views/documents/DocumentsView.vue'), meta: { screenKey: 'documents' } },
|
||||
{ path: 'tasks/board', name: 'tasks-board', component: () => import('../views/tasks/TasksBoardView.vue'), meta: { screenKey: 'tasks' } },
|
||||
{ path: 'members', name: 'members', component: () => import('../views/projects/MembersView.vue') },
|
||||
],
|
||||
},
|
||||
{ path: '/:pathMatch(.*)*', redirect: () => landingPath() },
|
||||
],
|
||||
})
|
||||
|
||||
@@ -63,14 +76,20 @@ router.beforeEach(async (to) => {
|
||||
return { name: 'login', query: { redirect: to.fullPath } }
|
||||
}
|
||||
if (to.name === 'login' && auth.isAuthenticated) {
|
||||
return { path: '/select-project' }
|
||||
return landingPath()
|
||||
}
|
||||
if (auth.isAuthenticated) {
|
||||
await auth.ensureMenu()
|
||||
try {
|
||||
await auth.ensureMenu()
|
||||
} catch {
|
||||
// never block navigation on a menu fetch failure
|
||||
}
|
||||
}
|
||||
if (to.meta.adminOnly && !auth.isAdmin) {
|
||||
return fallbackLocation()
|
||||
}
|
||||
if (to.meta.screenKey && !auth.canView(to.meta.screenKey)) {
|
||||
const last = localStorage.getItem('mws_last_project')
|
||||
return last ? { name: 'tasks-board', params: { id: last } } : { name: 'select-project' }
|
||||
return fallbackLocation()
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user