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
|
|
|
}),
|
|
|
|
mutations: {
|
2020-07-16 16:30:19 +02:00
|
|
|
'SET_DOMAINS' (state, domains) {
|
|
|
|
state.domains = domains
|
|
|
|
},
|
|
|
|
'SET_USERS' (state, users) {
|
2020-07-17 13:11:14 +02:00
|
|
|
state.users = Object.keys(users).length === 0 ? null : users
|
|
|
|
},
|
|
|
|
'SET_USERS_PARAM' (state, [username, userData]) {
|
|
|
|
Vue.set(state.users_details, username, userData)
|
2020-07-16 16:07:54 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
actions: {
|
2020-07-17 13:11:14 +02:00
|
|
|
'FETCH' ({ state, commit, dispatch }, { uri, param, storeKey = uri, force = false }) {
|
|
|
|
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 13:11:14 +02:00
|
|
|
if (currentState !== undefined && !force) return
|
|
|
|
console.log(`will query: "/${param ? `${uri}/${param}` : uri}" and will store in "${storeKey || uri}"`)
|
|
|
|
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-16 16:07:54 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getters: {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|