-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstore.js
80 lines (70 loc) · 1.69 KB
/
store.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
// eslint-disable-next-line camelcase
import { Hello_Greet } from './client/hello'
import axios from 'axios'
const state = {
config: null,
message: ''
}
const getters = {
config: state => state.config,
message: state => state.message
}
const actions = {
// Used by ocis-web.
loadConfig ({ commit }, config) {
commit('LOAD_CONFIG', config)
},
submitName ({ commit, dispatch, getters, rootGetters }, value) {
injectAuthToken(rootGetters)
Hello_Greet({
$domain: rootGetters.configuration.server,
body: { name: value }
})
.then(response => {
console.log(response)
if (response.status === 200 || response.status === 201) {
commit('SET_MESSAGE', response.data.message)
} else {
dispatch('showMessage', {
title: 'Response failed',
desc: response.statusText,
status: 'danger'
}, { root: true })
}
})
.catch(error => {
console.error(error)
dispatch('showMessage', {
title: 'Saving your name failed',
desc: error.message,
status: 'danger'
}, { root: true })
})
}
}
const mutations = {
SET_MESSAGE (state, payload) {
state.message = payload
},
LOAD_CONFIG (state, config) {
state.config = config
}
}
export default {
namespaced: true,
state,
getters,
actions,
mutations
}
function injectAuthToken (rootGetters) {
axios.interceptors.request.use(config => {
if (typeof config.headers.Authorization === 'undefined') {
const token = rootGetters.user.token
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
}
return config
})
}