perf: redirect transition list to card
gitea/salix-front/pipeline/pr-test There was a failure building this commit Details

This commit is contained in:
Alex Moreno 2025-01-07 08:21:09 +01:00
parent cc6ee7ce30
commit 76b9fbc267
1 changed files with 22 additions and 15 deletions

View File

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