fix: refs #8388 reload on delete
gitea/salix-front/pipeline/pr-dev This commit is unstable Details

This commit is contained in:
Jorge Penadés 2025-05-15 13:29:40 +02:00
parent 67f8d718e8
commit 79e87833b9
1 changed files with 31 additions and 26 deletions

View File

@ -131,11 +131,10 @@ async function fetch(data) {
const rows = keyData ? data[keyData] : data; const rows = keyData ? data[keyData] : data;
resetData(rows); resetData(rows);
emit('onFetch', rows); emit('onFetch', rows);
$props.insertOnLoad && await insert(); $props.insertOnLoad && (await insert());
return rows; return rows;
} }
function resetData(data) { function resetData(data) {
if (!data) return; if (!data) return;
if (data && Array.isArray(data)) { if (data && Array.isArray(data)) {
@ -146,15 +145,22 @@ function resetData(data) {
formData.value = JSON.parse(JSON.stringify(data)); formData.value = JSON.parse(JSON.stringify(data));
if (watchChanges.value) watchChanges.value(); //destroy watcher if (watchChanges.value) watchChanges.value(); //destroy watcher
watchChanges.value = watch(formData, (nVal) => { watchChanges.value = watch(
hasChanges.value = false; formData,
const filteredNewData = nVal.filter(row => !isRowEmpty(row) || row[$props.primaryKey]); (nVal) => {
const filteredOriginal = originalData.value.filter(row => row[$props.primaryKey]); hasChanges.value = false;
const filteredNewData = nVal.filter(
(row) => !isRowEmpty(row) || row[$props.primaryKey],
);
const filteredOriginal = originalData.value.filter(
(row) => row[$props.primaryKey],
);
const changes = getDifferences(filteredOriginal, filteredNewData); const changes = getDifferences(filteredOriginal, filteredNewData);
hasChanges.value = !isEmpty(changes); hasChanges.value = !isEmpty(changes);
},
}, { deep: true }); { deep: true },
);
} }
async function reset() { async function reset() {
await fetch(originalData.value); await fetch(originalData.value);
@ -183,9 +189,8 @@ async function onSubmit() {
}); });
} }
isLoading.value = true; isLoading.value = true;
await saveChanges($props.saveFn ? formData.value : null); await saveChanges($props.saveFn ? formData.value : null);
} }
async function onSubmitAndGo() { async function onSubmitAndGo() {
@ -194,10 +199,10 @@ async function onSubmitAndGo() {
} }
async function saveChanges(data) { async function saveChanges(data) {
formData.value = formData.value.filter(row => formData.value = formData.value.filter(
row[$props.primaryKey] || !isRowEmpty(row) (row) => row[$props.primaryKey] || !isRowEmpty(row),
); );
if ($props.saveFn) { if ($props.saveFn) {
$props.saveFn(data, getChanges); $props.saveFn(data, getChanges);
isLoading.value = false; isLoading.value = false;
@ -228,31 +233,29 @@ async function saveChanges(data) {
} }
async function insert(pushData = { ...$props.dataRequired, ...$props.dataDefault }) { async function insert(pushData = { ...$props.dataRequired, ...$props.dataDefault }) {
formData.value = formData.value.filter(row => !isRowEmpty(row)); formData.value = formData.value.filter((row) => !isRowEmpty(row));
const lastRow = formData.value.at(-1); const lastRow = formData.value.at(-1);
const isLastRowEmpty = lastRow ? isRowEmpty(lastRow) : false; const isLastRowEmpty = lastRow ? isRowEmpty(lastRow) : false;
if (formData.value.length && isLastRowEmpty) return; if (formData.value.length && isLastRowEmpty) return;
const $index = formData.value.length ? formData.value.at(-1).$index + 1 : 0; const $index = formData.value.length ? formData.value.at(-1).$index + 1 : 0;
const nRow = Object.assign({ $index }, pushData); const nRow = Object.assign({ $index }, pushData);
formData.value.push(nRow); formData.value.push(nRow);
const hasChange = Object.keys(nRow).some(key => !isChange(nRow, key)); const hasChange = Object.keys(nRow).some((key) => !isChange(nRow, key));
if (hasChange) hasChanges.value = true; if (hasChange) hasChanges.value = true;
} }
function isRowEmpty(row) { function isRowEmpty(row) {
return Object.keys(row).every(key => isChange(row, key)); return Object.keys(row).every((key) => isChange(row, key));
} }
function isChange(row, key) {
function isChange(row,key){
return !row[key] || key == '$index' || Object.hasOwn($props.dataRequired || {}, key); return !row[key] || key == '$index' || Object.hasOwn($props.dataRequired || {}, key);
} }
async function remove(data) { async function remove(data) {
if (!data.length) if (!data.length)
return quasar.notify({ return quasar.notify({
@ -270,7 +273,9 @@ async function remove(data) {
(form) => !preRemove.some((index) => index == form.$index), (form) => !preRemove.some((index) => index == form.$index),
); );
formData.value = newData; formData.value = newData;
hasChanges.value = JSON.stringify(removeIndexField(formData.value)) !== JSON.stringify(removeIndexField(originalData.value)); hasChanges.value =
JSON.stringify(removeIndexField(formData.value)) !==
JSON.stringify(removeIndexField(originalData.value));
} }
if (ids.length) { if (ids.length) {
quasar quasar
@ -286,7 +291,7 @@ async function remove(data) {
}) })
.onOk(async () => { .onOk(async () => {
newData = newData.filter((form) => !ids.some((id) => id == form[pk])); newData = newData.filter((form) => !ids.some((id) => id == form[pk]));
fetch(newData); await reload();
}); });
} }