add cache functionnality to mutation FETCH_ALL and add state for permissions and groups

This commit is contained in:
Axolotle 2020-08-01 22:35:39 +02:00
parent d4aa49fd34
commit 3955344fa6

View file

@ -5,7 +5,9 @@ export default {
state: () => ({ state: () => ({
domains: undefined, // Array domains: undefined, // Array
users: undefined, // basic user data: Object {username: {data}} users: undefined, // basic user data: Object {username: {data}}
users_details: {} // precise user data: Object {username: {data}} users_details: {}, // precise user data: Object {username: {data}}
groups: undefined,
permissions: undefined
}), }),
mutations: { mutations: {
@ -39,6 +41,14 @@ export default {
if (state.users) { if (state.users) {
Vue.delete(state.users, username) Vue.delete(state.users, username)
} }
},
'SET_GROUPS' (state, groups) {
state.groups = groups
},
'SET_PERMISSIONS' (state, permissions) {
state.permissions = permissions
} }
}, },
@ -51,31 +61,35 @@ export default {
return api.get(param ? `${uri}/${param}` : uri).then(responseData => { return api.get(param ? `${uri}/${param}` : uri).then(responseData => {
const data = responseData[storeKey] ? responseData[storeKey] : responseData const data = responseData[storeKey] ? responseData[storeKey] : responseData
commit('SET_' + storeKey.toUpperCase(), param ? [param, data] : data) commit('SET_' + storeKey.toUpperCase(), param ? [param, data] : data)
return data return param ? state[storeKey][param] : state[storeKey]
}) })
}, },
'FETCH_ALL' ({ state, commit }, queries) { 'FETCH_ALL' ({ state, commit }, queries) {
// TODO do not get if data is already present
return Promise.all(queries.map(({ uri, param, storeKey = uri, force = false }) => { return Promise.all(queries.map(({ uri, param, storeKey = uri, force = false }) => {
return api.get(param ? `${uri}/${param}` : uri) const currentState = param ? state[storeKey][param] : state[storeKey]
// if data has already been queried, simply return
if (currentState !== undefined && !force) {
return { cached: true, responseData: currentState }
}
return api.get(param ? `${uri}/${param}` : uri).then(responseData => {
return { storeKey, param, responseData }
})
})).then(responsesData => { })).then(responsesData => {
return responsesData.map((responseData, i) => { return responsesData.map(({ storeKey, param, responseData, cached = false }) => {
const storeKey = queries[i].storeKey || queries[i].uri if (cached) return responseData
const param = queries[i].param
const data = responseData[storeKey] ? responseData[storeKey] : responseData const data = responseData[storeKey] ? responseData[storeKey] : responseData
commit('SET_' + storeKey.toUpperCase(), param ? [param, data] : data) commit('SET_' + storeKey.toUpperCase(), param ? [param, data] : data)
return data return param ? state[storeKey][param] : state[storeKey]
}) })
}) })
}, },
'POST' ({ state, commit }, { uri, data, storeKey = uri }) { 'POST' ({ state, commit }, { uri, data, storeKey = uri }) {
return api.post(uri, data).then(responseData => { return api.post(uri, data).then(responseData => {
console.log(responseData)
const data = responseData[storeKey] ? responseData[storeKey] : responseData const data = responseData[storeKey] ? responseData[storeKey] : responseData
commit('ADD_' + storeKey.toUpperCase(), data) commit('ADD_' + storeKey.toUpperCase(), data)
return data return state[storeKey]
}) })
}, },
@ -83,7 +97,7 @@ export default {
return api.put(param ? `${uri}/${param}` : uri, data).then(responseData => { return api.put(param ? `${uri}/${param}` : uri, data).then(responseData => {
const data = responseData[storeKey] ? responseData[storeKey] : responseData const data = responseData[storeKey] ? responseData[storeKey] : responseData
commit('SET_' + storeKey.toUpperCase(), param ? [param, data] : data) commit('SET_' + storeKey.toUpperCase(), param ? [param, data] : data)
return data return param ? state[storeKey][param] : state[storeKey]
}) })
}, },