2020-01-13 10:07:53 -05:00
|
|
|
/**
|
|
|
|
* Returns computed properties two way bound to vuex
|
|
|
|
*
|
|
|
|
* @param {(string[]|Object[])} list - list of string matching state keys or list objects
|
|
|
|
* @param {string} list[].key - the key matching the key present in the vuex state
|
|
|
|
* @param {string} list[].getter - the name of the getter, leave it empty to not use a getter
|
|
|
|
* @param {string} list[].updateFn - the name of the action, leave it empty to use the default action
|
|
|
|
* @param {string} defaultUpdateFn - the default function to dispatch
|
2021-07-14 08:09:23 -04:00
|
|
|
* @param {string|function} root - the key of the state where to search for the keys described in list
|
2020-01-13 10:07:53 -05:00
|
|
|
* @returns {Object} a dictionary with all the computed properties generated
|
|
|
|
*/
|
|
|
|
export const mapComputed = (list, defaultUpdateFn, root) => {
|
2019-12-19 07:07:35 -05:00
|
|
|
const result = {};
|
2020-12-23 16:10:24 -05:00
|
|
|
list.forEach((item) => {
|
2020-01-13 10:07:53 -05:00
|
|
|
const [getter, key, updateFn] =
|
|
|
|
typeof item === 'string'
|
|
|
|
? [false, item, defaultUpdateFn]
|
|
|
|
: [item.getter, item.key, item.updateFn || defaultUpdateFn];
|
2019-12-19 07:07:35 -05:00
|
|
|
result[key] = {
|
|
|
|
get() {
|
2020-01-13 10:07:53 -05:00
|
|
|
if (getter) {
|
|
|
|
return this.$store.getters[getter];
|
|
|
|
} else if (root) {
|
2021-07-14 08:09:23 -04:00
|
|
|
if (typeof root === 'function') {
|
|
|
|
return root(this.$store.state)[key];
|
|
|
|
}
|
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
return this.$store.state[root][key];
|
|
|
|
}
|
|
|
|
return this.$store.state[key];
|
2019-12-19 07:07:35 -05:00
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
this.$store.dispatch(updateFn, { [key]: value });
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
};
|