diff --git a/src/composables/useArrayData.js b/src/composables/useArrayData.js index fd6e3a9b3..d09030de1 100644 --- a/src/composables/useArrayData.js +++ b/src/composables/useArrayData.js @@ -124,8 +124,11 @@ export function useArrayData(key = useRoute().meta.moduleName, userOptions) { const { limit } = filter; store.hasMoreData = limit && response.data.length >= limit; + if (!append && !isDialogOpened() && updateRouter) { + const res = updateStateParams(response.data); + if (res?.redirect) return; + } processData(response.data, { map: !!store.mapKey, append }); - if (!append && !isDialogOpened()) updateRouter && updateStateParams(); store.isLoading = false; canceller = null; @@ -259,7 +262,7 @@ export function useArrayData(key = useRoute().meta.moduleName, userOptions) { if (Object.values(store.userParams).length) await fetch({}); } - function updateStateParams() { + function updateStateParams(data) { if (!route?.path) return; const newUrl = { path: route.path, query: { ...(route.query ?? {}) } }; if (store?.searchUrl) @@ -276,15 +279,15 @@ export function useArrayData(key = useRoute().meta.moduleName, userOptions) { const { path } = matches.at(-1); const to = - store?.data?.length === 1 - ? path.replace(/\/(list|:id)|-list/, `/${store.data[0].id}`) + data?.length === 1 + ? path.replace(/\/(list|:id)|-list/, `/${data[0].id}`) : path.replace(/:id.*/, ''); if (route.path != to) { const pushUrl = { path: to }; if (to.endsWith('/list') || to.endsWith('/')) pushUrl.query = newUrl.query; - return router.push(pushUrl); + return router.push(pushUrl) && { redirect: true }; } } @@ -292,28 +295,32 @@ export function useArrayData(key = useRoute().meta.moduleName, userOptions) { } function processData(data, { map = true, append = true }) { + let newData; + let newMap; if (!append) { - store.data = []; - store.map = new Map(); + newData = []; + newMap = new Map(); } - if (!Array.isArray(data)) store.data = data; - else if (!map && append) for (const row of data) store.data.push(row); + if (!Array.isArray(data)) newData = data; + else if (!map && append) for (const row of data) newData.push(row); else for (const row of data) { const key = row[store.mapKey]; const val = { ...row, key }; - if (key && store.map.has(key)) { + if (key && newMap.has(key)) { const { position } = store.map.get(key); val.position = position; - store.map.set(key, val); - store.data[position] = val; + newMap.set(key, val); + newData[position] = val; } else { - val.position = store.map.size; - store.map.set(key, val); - store.data.push(val); + val.position = newMap.size; + newMap.set(key, val); + newData.push(val); } } + store.data = data; + store.map = map; } const totalRows = computed(() => (store.data && store.data.length) || 0);