2020-07-17 13:11:14 +02:00
|
|
|
import Vue from 'vue'
|
2020-07-16 16:07:54 +02:00
|
|
|
import api from './api'
|
|
|
|
|
|
|
|
export default {
|
|
|
|
state: () => ({
|
2020-07-17 13:11:14 +02:00
|
|
|
domains: undefined, // Array
|
|
|
|
users: undefined, // basic user data: Object {username: {data}}
|
|
|
|
users_details: {} // precise user data: Object {username: {data}}
|
2020-07-16 16:07:54 +02:00
|
|
|
}),
|
2020-07-27 20:46:27 +02:00
|
|
|
|
2020-07-16 16:07:54 +02:00
|
|
|
mutations: {
|
2020-07-16 16:30:19 +02:00
|
|
|
'SET_DOMAINS' (state, domains) {
|
|
|
|
state.domains = domains
|
|
|
|
},
|
2020-07-27 20:46:27 +02:00
|
|
|
|
2020-07-16 16:30:19 +02:00
|
|
|
'SET_USERS' (state, users) {
|
2020-07-17 13:11:14 +02:00
|
|
|
state.users = Object.keys(users).length === 0 ? null : users
|
|
|
|
},
|
2020-07-27 20:46:27 +02:00
|
|
|
|
2020-07-26 19:42:00 +02:00
|
|
|
'ADD_USER' (state, user) {
|
|
|
|
// FIXME will trigger an error if first created user
|
|
|
|
Vue.set(state.users, user.username, user)
|
|
|
|
},
|
2020-07-27 20:46:27 +02:00
|
|
|
|
2020-07-17 13:11:14 +02:00
|
|
|
'SET_USERS_PARAM' (state, [username, userData]) {
|
|
|
|
Vue.set(state.users_details, username, userData)
|
2020-07-27 20:46:27 +02:00
|
|
|
if (!state.users) return
|
|
|
|
const user = state.users[username]
|
|
|
|
for (const key of ['firstname', 'lastname', 'mail']) {
|
|
|
|
if (user[key] !== userData[key]) {
|
|
|
|
Vue.set(user, key, userData[key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Vue.set(user, 'fullname', `${userData.firstname} ${userData.lastname}`)
|
2020-07-28 00:18:47 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'DEL_USERS_PARAM' (state, username) {
|
|
|
|
Vue.delete(state.users_details, username)
|
|
|
|
if (state.users) {
|
|
|
|
Vue.delete(state.users, username)
|
|
|
|
}
|
2020-07-16 16:07:54 +02:00
|
|
|
}
|
|
|
|
},
|
2020-07-27 20:46:27 +02:00
|
|
|
|
2020-07-16 16:07:54 +02:00
|
|
|
actions: {
|
2020-07-26 19:42:00 +02:00
|
|
|
'FETCH' ({ state, commit }, { uri, param, storeKey = uri, force = false }) {
|
2020-07-17 13:11:14 +02:00
|
|
|
const currentState = param ? state[storeKey][param] : state[storeKey]
|
2020-07-16 19:18:01 +02:00
|
|
|
// if data has already been queried, simply return
|
2020-07-17 14:35:30 +02:00
|
|
|
if (currentState !== undefined && !force) return currentState
|
2020-07-26 19:42:00 +02:00
|
|
|
|
2020-07-17 13:11:14 +02:00
|
|
|
return api.get(param ? `${uri}/${param}` : uri).then(responseData => {
|
2020-07-16 16:30:19 +02:00
|
|
|
const data = responseData[uri] ? responseData[uri] : responseData
|
2020-07-17 13:11:14 +02:00
|
|
|
if (param) {
|
|
|
|
commit(`SET_${uri.toUpperCase()}_PARAM`, [param, data])
|
|
|
|
} else {
|
|
|
|
commit('SET_' + uri.toUpperCase(), data)
|
|
|
|
}
|
2020-07-17 14:35:30 +02:00
|
|
|
return param ? state[storeKey][param] : state[storeKey]
|
2020-07-16 16:07:54 +02:00
|
|
|
})
|
2020-07-26 19:42:00 +02:00
|
|
|
},
|
|
|
|
|
2020-07-27 20:46:27 +02:00
|
|
|
'POST' ({ state, commit }, { uri, data }) {
|
2020-07-26 19:42:00 +02:00
|
|
|
return api.post(uri, data)
|
2020-07-27 20:46:27 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'PUT' ({ state, commit }, { uri, param, data, storeKey = uri }) {
|
2020-07-28 00:18:47 +02:00
|
|
|
return api.put(param ? `${uri}/${param}` : uri, data).then(responseData => {
|
2020-07-27 20:46:27 +02:00
|
|
|
const data = responseData[uri] ? responseData[uri] : responseData
|
|
|
|
if (param) {
|
|
|
|
commit(`SET_${uri.toUpperCase()}_PARAM`, [param, data])
|
|
|
|
} else {
|
|
|
|
commit('SET_' + uri.toUpperCase(), data)
|
|
|
|
}
|
|
|
|
return param ? state[storeKey][param] : state[storeKey]
|
|
|
|
})
|
2020-07-28 00:18:47 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'DELETE' ({ state, commit }, { uri, param, data }) {
|
|
|
|
return api.delete(param ? `${uri}/${param}` : uri, data).then(() => {
|
|
|
|
if (param) {
|
|
|
|
commit(`DEL_${uri.toUpperCase()}_PARAM`, param)
|
|
|
|
} else {
|
|
|
|
commit('DEL_' + uri.toUpperCase())
|
|
|
|
}
|
|
|
|
})
|
2020-07-16 16:07:54 +02:00
|
|
|
}
|
|
|
|
},
|
2020-07-27 20:46:27 +02:00
|
|
|
|
2020-07-16 16:07:54 +02:00
|
|
|
getters: {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|