Skip to content

Commit 7a25726

Browse files
committed
Implement UI context based on user roles (#6, Fixes #10)
1 parent e025fbf commit 7a25726

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

src/App.vue

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@
2020
"
2121
>
2222
<v-btn
23-
v-for="link in links"
24-
:key="link.label"
23+
v-for="route in $router.options.routes.filter(
24+
(r) =>
25+
r.meta &&
26+
r.meta.showInNav &&
27+
r.meta.showInNav.some(
28+
(role) => authenticatedUser.roles.map(r => r.id).includes(role)
29+
)
30+
)"
31+
:key="route.name"
2532
text
26-
:to="{ name: link.label }"
33+
:to="{ name: route.name }"
2734
active-class="primary--text"
28-
>{{ link.label }}</v-btn
35+
>{{ route.name }}</v-btn
2936
>
3037
</div>
3138

@@ -157,10 +164,10 @@ export default Vue.extend({
157164
if (storedUser) {
158165
this.$store.commit("SET_AUTHENTICATED_USER", {
159166
user: JSON.parse(storedUser),
160-
});
161-
}
162-
// Refresh user data in case of an update
163-
this.$store.dispatch("getAuthenticatedUser");
167+
});
168+
}
169+
// Refresh user data in case of an update
170+
this.$store.dispatch("getAuthenticatedUser");
164171
},
165172
methods: {
166173
signOut() {

src/router/index.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import Home from '../views/Home.vue'
55
import SignIn from '../views/SignIn.vue'
66
import SignUp from '../views/SignUp.vue'
77
import Clock from '../views/Clock.vue'
8+
import Approvals from '../views/Approvals.vue'
89
import Availability from '../views/Availability.vue'
910
import Schedule from '../views/Schedule.vue'
1011
import Messages from '../views/messages/Messages.vue'
1112
import Conversation from '../views/messages/Conversation.vue'
1213

1314
Vue.use(VueRouter)
1415

16+
const EMPLOYEE = 1, MANAGER = 2
17+
1518
const routes = [
1619
{
1720
path: '/',
@@ -27,27 +30,47 @@ const routes = [
2730
path: '/sign-up',
2831
alias: '/confirmed',
2932
name: 'signUp',
30-
component: SignUp
33+
component: SignUp,
3134
},
3235
{
3336
path: '/clock',
3437
name: 'clock',
3538
component: Clock,
39+
meta: {
40+
showInNav: [EMPLOYEE]
41+
}
42+
},
43+
{
44+
path: '/approvals',
45+
name: 'approvals',
46+
component: Approvals,
47+
meta: {
48+
showInNav: [MANAGER]
49+
}
3650
},
3751
{
3852
path: '/availability',
3953
name: 'availability',
4054
component: Availability,
55+
meta: {
56+
showInNav: [EMPLOYEE, MANAGER]
57+
}
4158
},
4259
{
4360
path: '/schedule',
4461
name: 'schedule',
4562
component: Schedule,
63+
meta: {
64+
showInNav: [EMPLOYEE, MANAGER]
65+
}
4666
},
4767
{
4868
path: '/messages',
4969
name: 'messages',
5070
component: Messages,
71+
meta: {
72+
showInNav: [EMPLOYEE, MANAGER]
73+
},
5174
children: [{
5275
name: 'conversation',
5376
path: 'conversation/:conversationId',
@@ -62,4 +85,12 @@ const router = new VueRouter({
6285
routes
6386
})
6487

88+
const userRole = 1
89+
90+
router.beforeEach((to, from, next) => {
91+
if (to.meta.showInNav && !to.meta.showInNav.includes(userRole))
92+
next({ name: 'home' })
93+
else next()
94+
})
95+
6596
export default router

src/store/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ axios.interceptors.response.use(response => {
248248
console.log(error.request)
249249

250250
// TODO: this is stupid, don't keep this. use custom axios config
251-
if (error.request.responseURL == 'http://localhost:5000/api/users/me') return
251+
if (error.request.responseURL == 'http://localhost:5000/users/me') return
252252

253253
if (res.message || res.response.error) {
254254
message = res.message || res.response.error

src/views/Approvals.vue

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<template>
2+
<v-container class="home fill-height d-flex flex-column justify-center align-center">
3+
<h2 class="text-h2 font-weight-bold mb-2">Aprovals</h2>
4+
</v-container>
5+
</template>
6+
7+
<script>
8+
import { mapState, mapActions } from 'vuex'
9+
10+
export default {
11+
name: 'approvals',
12+
computed: {
13+
...mapState(['authenticatedUser'])
14+
},
15+
methods: {
16+
...mapActions(['signOut'])
17+
}
18+
}
19+
</script>

0 commit comments

Comments
 (0)