23
23
* https://router.vuejs.org/en/
24
24
*/
25
25
26
- // Lib imports
27
26
import { createRouter , createWebHashHistory } from 'vue-router'
28
27
import NProgress from 'nprogress'
29
- import { store } from '@/store/index'
30
-
31
- import 'nprogress/css/nprogress.css'
28
+ import { i18n } from '@/i18n'
32
29
33
- // Routes
34
- import paths from './paths '
30
+ import paths from '@/router/paths'
31
+ import { store } from '@/store/index '
35
32
import { Alert } from '@/model/Alert.model'
36
33
34
+ const defaultPageTitle = i18n . global . t ( 'App.name' )
35
+
37
36
NProgress . configure ( { showSpinner : false } )
38
37
39
- function route ( path ) {
40
- const copy = Object . assign ( { } , path )
41
- const view = copy . view
42
- return Object . assign ( copy , {
43
- name : path . name || view ,
44
- component : ( resolve ) => import (
45
- `@/views/${ view } .vue`
46
- ) . then ( resolve )
47
- } )
38
+ function getRoute ( path ) {
39
+ return {
40
+ ...path ,
41
+ name : path . name || path . view ,
42
+ component : ( ) => import ( `@/views/${ path . view } .vue` )
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Return the page title for a particular route.
48
+ * @param {import('vue-router').RouteLocation } route
49
+ */
50
+ export function getPageTitle ( { meta, params } ) {
51
+ const extra = meta . getTitle ?. ( params ) || meta . title
52
+ return extra
53
+ ? `${ defaultPageTitle } | ${ extra } `
54
+ : defaultPageTitle
48
55
}
49
56
50
57
// Create a new router
51
58
const router = createRouter ( {
52
59
history : createWebHashHistory ( ) ,
53
- routes : paths . map ( path => route ( path ) ) ,
54
- // .concat([{ path: '*', redirect: '/dashboard' }]),
60
+ routes : paths . map ( getRoute ) ,
55
61
scrollBehavior ( to , from , savedPosition ) {
56
62
if ( savedPosition ) {
57
63
return savedPosition
@@ -66,34 +72,40 @@ const router = createRouter({
66
72
router . beforeEach ( async ( to , from ) => {
67
73
NProgress . start ( )
68
74
if ( ! store . state . user . user ) {
69
- try {
70
- const user = await router . app . config . globalProperties . $userService . getUserProfile ( )
71
- store . commit ( 'user/SET_USER' , user )
72
- } catch ( err ) {
73
- store . dispatch ( 'setAlert' , new Alert ( err , 'error' ) )
74
- }
75
+ const user = await router . app . config . globalProperties . $userService . getUserProfile ( )
76
+ // TODO: catch error getting user profile and redirect to static error page
77
+ store . commit ( 'user/SET_USER' , user )
75
78
}
76
- if ( ! store . state . user . user . permissions . includes ( 'read' ) && to . name !== 'noAuth' ) {
77
- return { name : 'noAuth' }
79
+ if ( ! store . state . user . user . permissions ?. includes ( 'read' ) ) {
80
+ if ( to . name !== 'NoAuth' ) { // Avoid infinite redirect?
81
+ return { name : 'NoAuth' }
82
+ }
83
+ } else if ( to . name === 'NoAuth' ) {
84
+ // If authorized, redirect no-auth page to home page
85
+ return { path : '/' }
78
86
}
79
87
80
- if ( to . name ) {
81
- let title = to . name
82
- let workflowName = null
83
- if ( to . meta . toolbar ) {
84
- // When a workflow is being displayed, we set the title to a
85
- // different value.
86
- title = to . params . workflowName
87
- workflowName = to . params . workflowName
88
- }
89
- store . commit ( 'app/setTitle' , title )
90
- store . commit ( 'workflows/SET_WORKFLOW_NAME' , workflowName )
91
- store . dispatch ( 'setAlert' , null )
88
+ // Set page title:
89
+ document . title = getPageTitle ( to )
90
+
91
+ // Set toolbar title:
92
+ let title = to . name
93
+ if ( to . meta . toolbar ) {
94
+ // When a workflow is being displayed, we set the title to a
95
+ // different value.
96
+ title = to . params . workflowName
92
97
}
98
+ store . commit ( 'app/setTitle' , title )
99
+ store . dispatch ( 'setAlert' , null )
93
100
} )
94
101
95
102
router . afterEach ( ( ) => {
96
103
NProgress . done ( )
97
104
} )
98
105
106
+ router . onError ( ( err , to , from ) => {
107
+ store . dispatch ( 'setAlert' , new Alert ( err , 'error' ) )
108
+ NProgress . done ( )
109
+ } )
110
+
99
111
export default router
0 commit comments