Skip to content

Commit 1530449

Browse files
committed
adding notifications
1 parent e4ecc09 commit 1530449

File tree

4 files changed

+67
-20
lines changed

4 files changed

+67
-20
lines changed

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)