0
0
Fork 0

perf: refs #7404 #7679 improve create

This commit is contained in:
Javier Segarra 2024-10-02 14:58:02 +02:00
parent 90853396ba
commit 40f7ccf42b
2 changed files with 84 additions and 41 deletions

View File

@ -246,7 +246,7 @@ function getChanges() {
for (const [i, row] of formData.value.entries()) { for (const [i, row] of formData.value.entries()) {
if (!row[pk]) { if (!row[pk]) {
creates.push(row); creates.push(row);
} else if (originalData.value) { } else if (originalData.value[i]) {
const data = getDifferences(originalData.value[i], row); const data = getDifferences(originalData.value[i], row);
if (!isEmpty(data)) { if (!isEmpty(data)) {
updates.push({ updates.push({

View File

@ -150,6 +150,14 @@ const columns = computed(() => [
icon: 'delete', icon: 'delete',
action: (row) => confirmRemove(row), action: (row) => confirmRemove(row),
isPrimary: true, isPrimary: true,
// show: (row) => !validations({ row }),
// },
// {
// title: t('save'),
// icon: 'save',
// action: (row) => upsertFixedPrice(row),
// isPrimary: true,
// show: (row) => validations({ row }),
}, },
], ],
}, },
@ -221,13 +229,41 @@ const updateMinPrice = async (value, props) => {
}); });
}; };
const validations = ({ row, rowIndex, col }) => {
const isNew = !row.id;
// Si la row no tiene id significa que fue agregada con addRow y no se ha guardado en la base de datos
// Si isNew es falso no se checkea si el valor es igual a la original
if (!isNew)
// if (fixedPricesOriginalData.value[rowIndex][col.field] == row[col.field])
return false;
const requiredFields = [
'itemFk',
'started',
'ended',
'rate2',
'rate3',
'warehouseFk',
];
const isValid = requiredFields.every(
(field) => row[field] !== null && row[field] !== undefined
);
return isValid;
};
const upsertPrice = async (props, resetMinPrice = false) => { const upsertPrice = async (props, resetMinPrice = false) => {
try { try {
const { row } = props; const isValid = validations({ ...props });
if (tableRef.value.CrudModelRef.getChanges().updates.length > 0) { if (!isValid) {
if (resetMinPrice) row.hasMinPrice = 0; // notify(t('globals.fillRequiredFields'), 'negative');
await upsertFixedPrice(row); return;
} }
const { row } = props;
const changes = tableRef.value.CrudModelRef.getChanges();
if (changes?.updates?.length > 0) {
if (resetMinPrice) row.hasMinPrice = 0;
}
const data = await upsertFixedPrice(row);
tableRef.value.CrudModelRef.formData[props.rowIndex] = data;
} catch (err) { } catch (err) {
console.error('Error editing price', err); console.error('Error editing price', err);
} }
@ -264,39 +300,39 @@ function checkLastVisibleRow() {
const addRow = (original = null) => { const addRow = (original = null) => {
let copy = null; let copy = null;
if (!original) { // if (!original) {
const today = Date.vnNew(); const today = Date.vnNew();
const millisecsInDay = 86400000; const millisecsInDay = 86400000;
const daysInWeek = 7; const daysInWeek = 7;
const nextWeek = new Date(today.getTime() + daysInWeek * millisecsInDay); const nextWeek = new Date(today.getTime() + daysInWeek * millisecsInDay);
copy = { copy = {
id: 0, id: 0,
started: today, started: today,
ended: nextWeek, ended: nextWeek,
hasMinPrice: 0, hasMinPrice: 0,
$index: 0, $index: 0,
}; };
} else // } else
copy = { // copy = {
$index: original.$index - 1, // $index: original.$index - 1,
itemFk: original.itemFk, // itemFk: original.itemFk,
name: original.name, // name: original.name,
subName: original.subName, // subName: original.subName,
value5: original.value5, // value5: original.value5,
value6: original.value6, // value6: original.value6,
value7: original.value7, // value7: original.value7,
value8: original.value8, // value8: original.value8,
value9: original.value9, // value9: original.value9,
value10: original.value10, // value10: original.value10,
warehouseFk: original.warehouseFk, // warehouseFk: original.warehouseFk,
rate2: original.rate2, // rate2: original.rate2,
rate3: original.rate3, // rate3: original.rate3,
hasMinPrice: original.hasMinPrice, // hasMinPrice: original.hasMinPrice,
minPrice: original.minPrice, // minPrice: original.minPrice,
started: Date.vnNew(), // started: Date.vnNew(),
ended: Date.vnNew(), // ended: Date.vnNew(),
}; // };
return { original, copy }; return { original, copy };
}; };
@ -436,10 +472,6 @@ function handleOnDataSave({ CrudModelRef }) {
auto-load auto-load
:is-editable="true" :is-editable="true"
:right-search="false" :right-search="false"
:table="{
'row-key': 'id',
selection: 'multiple',
}"
:crud-model="{ :crud-model="{
paginate: false, paginate: false,
}" }"
@ -585,7 +617,18 @@ function handleOnDataSave({ CrudModelRef }) {
</QIcon> </QIcon>
</template> </template>
</VnTable> </VnTable>
<!-- <QPageSticky v-if="rowsSelected" :offset="[20, 80]" style="z-index: 2">
<QBtn
@click="makeInvoice(selectedRows)"
color="primary"
fab
icon="vn:invoice-in"
/>
<QTooltip>
{{ t('ticketList.createInvoice') }}
</QTooltip>
</QPageSticky>-->
<QDialog ref="editTableCellDialogRef"> <QDialog ref="editTableCellDialogRef">
<EditTableCellValueForm <EditTableCellValueForm
edit-url="FixedPrices/editFixedPrice" edit-url="FixedPrices/editFixedPrice"