0
0
Fork 0

chore: refs #7300 reset arrayData

This commit is contained in:
Jorge Penadés 2024-07-03 17:53:37 +02:00
parent 6ad0b0994f
commit c2333c9455
5 changed files with 27 additions and 23 deletions

View File

@ -112,9 +112,7 @@ async function search(evt) {
isLoading.value = true;
const filter = { ...userParams.value };
store.userParamsChanged = true;
store.filter.skip = 0;
store.skip = 0;
store.page = 1;
arrayData.reset(['skip', 'filter.skip', 'page']);
const { params: newParams } = await arrayData.addFilter({ params: userParams.value });
userParams.value = newParams;
@ -138,9 +136,7 @@ async function reload() {
async function clearFilters() {
isLoading.value = true;
store.userParamsChanged = true;
store.filter.skip = 0;
store.skip = 0;
store.page = 1;
arrayData.reset(['skip', 'filter.skip', 'page']);
// Filtrar los params no removibles
const removableFilters = Object.keys(userParams.value).filter((param) =>
$props.unremovableParams.includes(param)

View File

@ -1,5 +1,5 @@
<script setup>
import { onMounted, ref, watch } from 'vue';
import { onBeforeUnmount, onMounted, ref, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import { useArrayData } from 'composables/useArrayData';
@ -95,6 +95,8 @@ onMounted(async () => {
mounted.value = true;
});
onBeforeUnmount(() => arrayData.reset());
watch(
() => props.data,
() => {
@ -118,8 +120,7 @@ const addFilter = async (filter, params) => {
async function fetch(params) {
useArrayData(props.dataKey, params);
store.filter.skip = 0;
store.skip = 0;
arrayData.reset(['filter.skip', 'skip']);
await arrayData.fetch({ append: false });
if (!store.hasMoreData) {
isLoading.value = false;

View File

@ -103,8 +103,7 @@ async function search() {
const staticParams = Object.entries(store.userParams).filter(
([key, value]) => value && (props.staticParams || []).includes(key)
);
store.skip = 0;
store.page = 1;
arrayData.reset(['skip', 'page']);
if (props.makeFetch)
await arrayData.applyFilter({

View File

@ -18,7 +18,8 @@ export function useArrayData(key = useRoute().meta.moduleName, userOptions) {
onMounted(() => {
setOptions();
store.skip = 0;
arrayDataStore.reset(['skip']);
const query = route.query;
const searchUrl = store.searchUrl;
if (query[searchUrl]) {
@ -153,9 +154,8 @@ export function useArrayData(key = useRoute().meta.moduleName, userOptions) {
userParams = sanitizerParams(userParams, store?.exprBuilder);
store.userParams = userParams;
store.skip = 0;
store.filter.skip = 0;
store.page = 1;
arrayDataStore.reset(['skip', 'filter.skip', 'page']);
await fetch({ append: false });
return { filter, params };
}
@ -191,7 +191,6 @@ export function useArrayData(key = useRoute().meta.moduleName, userOptions) {
store.page += 1;
await fetch({ append: true });
updateStateParams();
}
async function refresh() {

View File

@ -11,12 +11,12 @@ export const useArrayDataStore = defineStore('arrayDataStore', () => {
limit: 10,
skip: 0,
order: '',
data: ref(),
isLoading: false,
userParamsChanged: false,
exprBuilder: null,
searchUrl: 'params',
navigate: null,
page: 1,
};
function get(key) {
@ -24,7 +24,7 @@ export const useArrayDataStore = defineStore('arrayDataStore', () => {
}
function set(key) {
state.value[key] = defaultOpts;
state.value[key] = getDefaultState();
}
function clear(key) {
@ -32,12 +32,21 @@ export const useArrayDataStore = defineStore('arrayDataStore', () => {
}
function reset(key, opts = []) {
let obj = state.value[key];
if (!opts.length) obj = defaultOpts;
if (!opts.length) state.value[key] = getDefaultState();
else
for (const opt in opts) {
if (Object.hasOwn(obj, opt)) obj[opt] = defaultOpts[opt];
}
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(),
});
}
return {