Skip to content

Commit d0a1f65

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 3e7c4f1 + c87ac15 commit d0a1f65

File tree

7 files changed

+142
-21
lines changed

7 files changed

+142
-21
lines changed

src/App.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<template>
22
<div id="app">
33
<NavBar/>
4+
<NotificationContainer/>
45
<router-view :key="$route.fullPath" />
56
</div>
67
</template>
78

89
<script>
910
import NavBar from '@/components/NavBar.vue'
11+
import NotificationContainer from '@/components/NotificationContainer.vue'
1012
1113
export default {
1214
components: {
13-
NavBar
15+
NavBar,
16+
NotificationContainer
1417
}
1518
}
1619
</script>

src/components/NotificationBar.vue

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<template>
2+
<div class="notification-bar" :class="notificationTypeClass">
3+
<p>{{ notification.message }}</p>
4+
</div>
5+
</template>
6+
7+
<script>
8+
import { mapActions } from 'vuex'
9+
10+
export default {
11+
props: {
12+
notification: {
13+
type: Object,
14+
required: true
15+
}
16+
},
17+
data() {
18+
return {
19+
timeout: null
20+
}
21+
},
22+
mounted() {
23+
this.timeout = setTimeout(() => this.remove(this.notification), 5000)
24+
},
25+
beforeDestroy() {
26+
clearTimeout(this.timeout)
27+
},
28+
computed: {
29+
notificationTypeClass() {
30+
return `-text-${this.notification.type}`
31+
}
32+
},
33+
methods: mapActions('notification', ['remove'])
34+
}
35+
</script>
36+
37+
<style scoped>
38+
.notification-bar {
39+
margin: 1em 0 1em;
40+
}
41+
</style>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<template>
2+
<div class="notification-container">
3+
<NotificationBar
4+
v-for="notification in notifications"
5+
:key="notification.id"
6+
:notification="notification"
7+
/>
8+
</div>
9+
</template>
10+
11+
<script>
12+
import NotificationBar from '@/components/NotificationBar.vue'
13+
import { mapState } from 'vuex'
14+
15+
export default {
16+
components: {
17+
NotificationBar
18+
},
19+
computed: mapState('notification', ['notifications'])
20+
}
21+
</script>
22+
23+
<style scoped>
24+
.notification-container {
25+
position: fixed;
26+
bottom: 0;
27+
right: 0;
28+
padding-right: 40px;
29+
}
30+
</style>

src/store/modules/event.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,39 @@ export const mutations = {
2424
}
2525

2626
export const actions = {
27-
createEvent({ commit }, event) {
28-
return EventService.postEvent(event).then(() => {
29-
commit('ADD_EVENT', event)
30-
})
27+
createEvent({ commit, dispatch }, event) {
28+
return EventService.postEvent(event)
29+
.then(() => {
30+
commit('ADD_EVENT', event)
31+
const notification = {
32+
type: 'success',
33+
message: 'Your event has been created!'
34+
}
35+
dispatch('notification/add', notification, { root: true })
36+
})
37+
.catch(error => {
38+
const notification = {
39+
type: 'error',
40+
message: 'There was a problem creating your event: ' + error.message
41+
}
42+
dispatch('notification/add', notification, { root: true })
43+
})
3144
},
32-
fetchEvents({ commit }, { perPage, page }) {
45+
fetchEvents({ commit, dispatch }, { perPage, page }) {
3346
EventService.getEvents(perPage, page)
3447
.then(response => {
3548
commit('SET_EVENTS_TOTAL', parseInt(response.headers['x-total-count']))
3649
commit('SET_EVENTS', response.data)
3750
})
3851
.catch(error => {
39-
console.log('There was an error:', error.response)
52+
const notification = {
53+
type: 'error',
54+
message: 'There was a problem fetching events: ' + error.message
55+
}
56+
dispatch('notification/add', notification, { root: true })
4057
})
4158
},
42-
fetchEvent({ commit, getters }, id) {
59+
fetchEvent({ commit, getters, dispatch }, id) {
4360
var event = getters.getEventById(id)
4461

4562
if (event) {
@@ -50,7 +67,11 @@ export const actions = {
5067
commit('SET_EVENT', response.data)
5168
})
5269
.catch(error => {
53-
console.log('There was an error:', error.response)
70+
const notification = {
71+
type: 'error',
72+
message: 'There was a problem fetching event: ' + error.message
73+
}
74+
dispatch('notification/add', notification, { root: true })
5475
})
5576
}
5677
}

src/store/modules/notification.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export const namespaced = true
2+
3+
export const state = {
4+
notifications: []
5+
}
6+
7+
let nextId = 1
8+
9+
export const mutations = {
10+
PUSH(state, notification) {
11+
state.notifications.push({
12+
...notification,
13+
id: nextId++
14+
})
15+
},
16+
DELETE(state, notificationToRemove) {
17+
state.notifications = state.notifications.filter(
18+
notification => notification.id !== notificationToRemove.id
19+
)
20+
}
21+
}
22+
export const actions = {
23+
add({ commit }, notification) {
24+
commit('PUSH', notification)
25+
},
26+
remove({ commit }, notificationToRemove) {
27+
commit('DELETE', notificationToRemove)
28+
}
29+
}

src/store/store.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import Vue from 'vue'
22
import Vuex from 'vuex'
33
import * as user from '@/store/modules/user.js'
44
import * as event from '@/store/modules/event.js'
5+
import * as notification from '@/store/modules/notification.js'
56

67
Vue.use(Vuex)
78

89
export default new Vuex.Store({
910
modules: {
1011
user,
11-
event
12+
event,
13+
notification
1214
},
1315
state: {
1416
categories: [

src/views/EventCreate.vue

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,13 @@ export default {
6464
},
6565
methods: {
6666
createEvent() {
67-
this.$store
68-
.dispatch('event/createEvent', this.event)
69-
.then(() => {
70-
this.$router.push({
71-
name: 'event-show',
72-
params: { id: this.event.id }
73-
})
74-
this.event = this.createFreshEventObject()
75-
})
76-
.catch(() => {
77-
console.log('There was a problem creating your event')
67+
this.$store.dispatch('event/createEvent', this.event).then(() => {
68+
this.$router.push({
69+
name: 'event-show',
70+
params: { id: this.event.id }
7871
})
72+
this.event = this.createFreshEventObject()
73+
})
7974
},
8075
createFreshEventObject() {
8176
const user = this.$store.state.user.user

0 commit comments

Comments
 (0)