/
notifications.js
93 lines (88 loc) · 2.29 KB
/
notifications.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"use strict"
export default () => {
const noteSignUp = {
icon: `mood`,
title: `Welcome!`,
body: `Thank you for signing up.`,
time: 0
}
const noteSignIn = {
icon: `mood`,
title: `Signed In`,
body: `Welcome back.`,
time: 0
}
const noteSignOut = {
icon: `exit_to_app`,
title: `Signed Out`,
body: `Come back again soon.`,
time: 0
}
const noteAuthRequired = {
icon: `error`,
title: `Authentication Required`,
body: `You must sign up or sign in to view that page.`,
time: 0
}
let key = 0
const state = []
const mutations = {
addNotification(state, note) {
// log notifications so we can see the notifications in the log to debug
console.log(
`[${note.type ? note.type.toUpperCase() : `INFO`}] ${note.title}: ${
note.body
}`
)
state.push(Object.assign({}, note, { key: key++ }))
},
notify(state, data) {
const note = data
note.icon = `check_circle`
note.time = Date.now()
mutations.addNotification(state, note)
},
notifyWarn(state, data) {
const note = data
note.icon = `warning`
note.time = Date.now()
note.type = `warning`
mutations.addNotification(state, note)
},
notifyError(state, data) {
const note = data
note.icon = `error`
note.time = Date.now()
note.layout = `alert`
note.type = `error`
mutations.addNotification(state, note)
},
notifySignUp(state) {
const note = JSON.parse(JSON.stringify(noteSignUp))
note.time = Date.now()
mutations.addNotification(state, note)
},
notifySignIn(state) {
const note = JSON.parse(JSON.stringify(noteSignIn))
note.time = Date.now()
mutations.addNotification(state, note)
},
notifySignOut(state) {
const note = JSON.parse(JSON.stringify(noteSignOut))
note.time = Date.now()
mutations.addNotification(state, note)
},
notifyAuthRequired(state, body) {
const note = JSON.parse(JSON.stringify(noteAuthRequired))
note.time = Date.now()
if (body) note.body = body
mutations.addNotification(state, note)
}
}
const actions = {
resetSessionData({ state }) {
state.notifications = []
}
}
return { state, mutations, actions }
}