diff --git a/app/src/helpers/dataStore.js b/app/src/helpers/dataStore.js index ca67f1d5..1d2ac4fa 100644 --- a/app/src/helpers/dataStore.js +++ b/app/src/helpers/dataStore.js @@ -5,7 +5,9 @@ export default { state: () => ({ domains: undefined, // Array 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: { @@ -39,6 +41,14 @@ export default { if (state.users) { 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 => { const data = responseData[storeKey] ? responseData[storeKey] : responseData commit('SET_' + storeKey.toUpperCase(), param ? [param, data] : data) - return data + return param ? state[storeKey][param] : state[storeKey] }) }, '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 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 => { - return responsesData.map((responseData, i) => { - const storeKey = queries[i].storeKey || queries[i].uri - const param = queries[i].param + return responsesData.map(({ storeKey, param, responseData, cached = false }) => { + if (cached) return responseData const data = responseData[storeKey] ? responseData[storeKey] : responseData commit('SET_' + storeKey.toUpperCase(), param ? [param, data] : data) - return data + return param ? state[storeKey][param] : state[storeKey] }) }) }, 'POST' ({ state, commit }, { uri, data, storeKey = uri }) { return api.post(uri, data).then(responseData => { - console.log(responseData) const data = responseData[storeKey] ? responseData[storeKey] : responseData 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 => { const data = responseData[storeKey] ? responseData[storeKey] : responseData commit('SET_' + storeKey.toUpperCase(), param ? [param, data] : data) - return data + return param ? state[storeKey][param] : state[storeKey] }) },