normalize store params & allow extra params passed to the commit function

This commit is contained in:
axolotle 2021-04-16 22:10:43 +02:00
parent 17e2d726b9
commit bc41d0ad9d
2 changed files with 29 additions and 24 deletions

View file

@ -14,7 +14,6 @@ import { openWebSocket, getResponseData, handleError } from './handlers'
* @property {Boolean} wait - If `true`, will display the waiting modal. * @property {Boolean} wait - If `true`, will display the waiting modal.
* @property {Boolean} websocket - if `true`, will open a websocket connection. * @property {Boolean} websocket - if `true`, will open a websocket connection.
* @property {Boolean} initial - if `true` and an error occurs, the dismiss button will trigger a go back in history. * @property {Boolean} initial - if `true` and an error occurs, the dismiss button will trigger a go back in history.
* @property {Boolean} noCache - if `true`, will disable the cache mecanism for this call.
* @property {Boolean} asFormData - if `true`, will send the data with a body encoded as `"multipart/form-data"` instead of `"x-www-form-urlencoded"`). * @property {Boolean} asFormData - if `true`, will send the data with a body encoded as `"multipart/form-data"` instead of `"x-www-form-urlencoded"`).
*/ */

View file

@ -1,6 +1,7 @@
import Vue from 'vue' import Vue from 'vue'
import api from '@/api' import api from '@/api'
import { isEmptyValue } from '@/helpers/commons'
export default { export default {
@ -14,31 +15,31 @@ export default {
}), }),
mutations: { mutations: {
'SET_DOMAINS' (state, domains) { 'SET_DOMAINS' (state, [domains]) {
state.domains = domains state.domains = domains
}, },
'ADD_DOMAINS' (state, { domain }) { 'ADD_DOMAINS' (state, [{ domain }]) {
state.domains.push(domain) state.domains.push(domain)
}, },
'DEL_DOMAINS' (state, domain) { 'DEL_DOMAINS' (state, [domain]) {
state.domains.splice(state.domains.indexOf(domain), 1) state.domains.splice(state.domains.indexOf(domain), 1)
}, },
'SET_MAIN_DOMAIN' (state, response) { 'SET_MAIN_DOMAIN' (state, [response]) {
state.main_domain = response.current_main_domain state.main_domain = response.current_main_domain
}, },
'UPDATE_MAIN_DOMAIN' (state, domain) { 'UPDATE_MAIN_DOMAIN' (state, [domain]) {
state.main_domain = domain state.main_domain = domain
}, },
'SET_USERS' (state, users) { 'SET_USERS' (state, [users]) {
state.users = Object.keys(users).length === 0 ? null : users state.users = Object.keys(users).length === 0 ? null : users
}, },
'ADD_USERS' (state, user) { 'ADD_USERS' (state, [user]) {
if (!state.users) state.users = {} if (!state.users) state.users = {}
Vue.set(state.users, user.username, user) Vue.set(state.users, user.username, user)
}, },
@ -60,7 +61,7 @@ export default {
this.commit('SET_USERS_DETAILS', payload) this.commit('SET_USERS_DETAILS', payload)
}, },
'DEL_USERS_DETAILS' (state, username) { 'DEL_USERS_DETAILS' (state, [username]) {
Vue.delete(state.users_details, username) Vue.delete(state.users_details, username)
if (state.users) { if (state.users) {
Vue.delete(state.users, username) Vue.delete(state.users, username)
@ -70,60 +71,65 @@ export default {
} }
}, },
'SET_GROUPS' (state, groups) { 'SET_GROUPS' (state, [groups]) {
state.groups = groups state.groups = groups
}, },
'ADD_GROUPS' (state, { name }) { 'ADD_GROUPS' (state, [{ name }]) {
if (state.groups !== undefined) { if (state.groups !== undefined) {
Vue.set(state.groups, name, { members: [], permissions: [] }) Vue.set(state.groups, name, { members: [], permissions: [] })
} }
}, },
'DEL_GROUPS' (state, groupname) { 'DEL_GROUPS' (state, [groupname]) {
Vue.delete(state.groups, groupname) Vue.delete(state.groups, groupname)
}, },
'SET_PERMISSIONS' (state, permissions) { 'SET_PERMISSIONS' (state, [permissions]) {
state.permissions = permissions state.permissions = permissions
} }
}, },
actions: { actions: {
'GET' ({ state, commit, rootState }, { uri, param, humanKey, storeKey = uri, options = {} }) { 'GET' (
const noCache = !rootState.cache || options.noCache || false { state, commit, rootState },
{ uri, param, storeKey = uri, humanKey, noCache, options, ...extraParams }
) {
const currentState = param ? state[storeKey][param] : state[storeKey] const currentState = param ? state[storeKey][param] : state[storeKey]
// if data has already been queried, simply return // if data has already been queried, simply return
if (currentState !== undefined && !noCache) return currentState const ignoreCache = !rootState.cache || noCache || false
if (currentState !== undefined && !ignoreCache) return currentState
return api.fetch('GET', param ? `${uri}/${param}` : uri, null, humanKey, options).then(responseData => { return api.fetch('GET', param ? `${uri}/${param}` : uri, null, humanKey, options).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, data, extraParams].filter(item => !isEmptyValue(item))
)
return param ? state[storeKey][param] : state[storeKey] return param ? state[storeKey][param] : state[storeKey]
}) })
}, },
'POST' ({ state, commit }, { uri, storeKey = uri, data, humanKey, options }) { 'POST' ({ state, commit }, { uri, storeKey = uri, data, humanKey, options, ...extraParams }) {
return api.fetch('POST', uri, data, humanKey, options).then(responseData => { return api.fetch('POST', uri, data, humanKey, options).then(responseData => {
// FIXME api/domains returns null // FIXME api/domains returns null
if (responseData === null) responseData = data if (responseData === null) responseData = data
responseData = responseData[storeKey] ? responseData[storeKey] : responseData responseData = responseData[storeKey] ? responseData[storeKey] : responseData
commit('ADD_' + storeKey.toUpperCase(), responseData) commit('ADD_' + storeKey.toUpperCase(), [responseData, extraParams].filter(item => !isEmptyValue(item)))
return state[storeKey] return state[storeKey]
}) })
}, },
'PUT' ({ state, commit }, { uri, param, storeKey = uri, data, humanKey, options }) { 'PUT' ({ state, commit }, { uri, param, storeKey = uri, data, humanKey, options, ...extraParams }) {
return api.fetch('PUT', param ? `${uri}/${param}` : uri, data, humanKey, options).then(responseData => { return api.fetch('PUT', param ? `${uri}/${param}` : uri, data, humanKey, options).then(responseData => {
const data = responseData[storeKey] ? responseData[storeKey] : responseData const data = responseData[storeKey] ? responseData[storeKey] : responseData
commit('UPDATE_' + storeKey.toUpperCase(), param ? [param, data] : data) commit('UPDATE_' + storeKey.toUpperCase(), [param, data, extraParams].filter(item => !isEmptyValue(item)))
return param ? state[storeKey][param] : state[storeKey] return param ? state[storeKey][param] : state[storeKey]
}) })
}, },
'DELETE' ({ commit }, { uri, param, storeKey = uri, data, humanKey, options }) { 'DELETE' ({ commit }, { uri, param, storeKey = uri, data, humanKey, options, ...extraParams }) {
return api.fetch('DELETE', param ? `${uri}/${param}` : uri, data, humanKey, options).then(() => { return api.fetch('DELETE', param ? `${uri}/${param}` : uri, data, humanKey, options).then(() => {
commit('DEL_' + storeKey.toUpperCase(), param) commit('DEL_' + storeKey.toUpperCase(), [param, extraParams].filter(item => !isEmptyValue(item)))
}) })
} }
}, },