salix-front/src/stores/useArrayDataStore.js

66 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-01-05 13:57:47 +00:00
import { ref } from 'vue';
import { defineStore } from 'pinia';
export const useArrayDataStore = defineStore('arrayDataStore', () => {
2023-01-10 13:58:24 +00:00
const state = ref({});
const defaultOpts = {
filter: {},
userFilter: {},
userParams: {},
url: '',
2024-11-14 09:36:42 +00:00
limit: 20,
skip: 0,
order: '',
isLoading: false,
userParamsChanged: false,
exprBuilder: null,
searchUrl: 'params',
navigate: null,
2024-07-03 15:53:37 +00:00
page: 1,
mapKey: 'id',
};
2023-01-05 13:57:47 +00:00
function get(key) {
2023-01-10 13:58:24 +00:00
return state.value[key];
2023-01-05 13:57:47 +00:00
}
2023-01-10 13:58:24 +00:00
function set(key) {
2024-07-03 15:53:37 +00:00
state.value[key] = getDefaultState();
2023-01-10 13:58:24 +00:00
}
function clear(key) {
delete state.value[key];
2023-01-05 13:57:47 +00:00
}
function reset(key, opts = []) {
2024-07-03 15:53:37 +00:00
if (!opts.length) state.value[key] = getDefaultState();
else
2024-07-03 15:53:37 +00:00
opts.forEach((opt) => {
if (opt.includes('.')) {
const [parent, child] = opt.split('.');
state.value[key][parent][child] = defaultOpts[child];
} else if (Object.hasOwn(state.value[key], opt))
state.value[key][opt] = defaultOpts[opt];
});
}
function getDefaultState() {
return Object.assign(JSON.parse(JSON.stringify(defaultOpts)), {
data: ref(),
map: ref(new Map()),
2024-07-03 15:53:37 +00:00
});
}
function resetPagination(key) {
reset(key, ['skip', 'filter.skip', 'page']);
}
2023-01-05 13:57:47 +00:00
return {
get,
set,
2023-03-01 15:20:28 +00:00
clear,
reset,
resetPagination,
2023-01-05 13:57:47 +00:00
};
});