From 40f7ccf42b6d6e9ba505c20dec199c8dff5d52cc Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Wed, 2 Oct 2024 14:58:02 +0200 Subject: [PATCH] perf: refs #7404 #7679 improve create --- src/components/CrudModel.vue | 2 +- src/pages/Item/ItemFixedPrice.vue | 123 ++++++++++++++++++++---------- 2 files changed, 84 insertions(+), 41 deletions(-) diff --git a/src/components/CrudModel.vue b/src/components/CrudModel.vue index 0386e037b..3b95f0b35 100644 --- a/src/components/CrudModel.vue +++ b/src/components/CrudModel.vue @@ -246,7 +246,7 @@ function getChanges() { for (const [i, row] of formData.value.entries()) { if (!row[pk]) { creates.push(row); - } else if (originalData.value) { + } else if (originalData.value[i]) { const data = getDifferences(originalData.value[i], row); if (!isEmpty(data)) { updates.push({ diff --git a/src/pages/Item/ItemFixedPrice.vue b/src/pages/Item/ItemFixedPrice.vue index fddf154a2..2a7c3a522 100644 --- a/src/pages/Item/ItemFixedPrice.vue +++ b/src/pages/Item/ItemFixedPrice.vue @@ -150,6 +150,14 @@ const columns = computed(() => [ icon: 'delete', action: (row) => confirmRemove(row), 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) => { try { - const { row } = props; - if (tableRef.value.CrudModelRef.getChanges().updates.length > 0) { - if (resetMinPrice) row.hasMinPrice = 0; - await upsertFixedPrice(row); + const isValid = validations({ ...props }); + if (!isValid) { + // notify(t('globals.fillRequiredFields'), 'negative'); + 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) { console.error('Error editing price', err); } @@ -264,39 +300,39 @@ function checkLastVisibleRow() { const addRow = (original = null) => { let copy = null; - if (!original) { - const today = Date.vnNew(); - const millisecsInDay = 86400000; - const daysInWeek = 7; - const nextWeek = new Date(today.getTime() + daysInWeek * millisecsInDay); + // if (!original) { + const today = Date.vnNew(); + const millisecsInDay = 86400000; + const daysInWeek = 7; + const nextWeek = new Date(today.getTime() + daysInWeek * millisecsInDay); - copy = { - id: 0, - started: today, - ended: nextWeek, - hasMinPrice: 0, - $index: 0, - }; - } else - copy = { - $index: original.$index - 1, - itemFk: original.itemFk, - name: original.name, - subName: original.subName, - value5: original.value5, - value6: original.value6, - value7: original.value7, - value8: original.value8, - value9: original.value9, - value10: original.value10, - warehouseFk: original.warehouseFk, - rate2: original.rate2, - rate3: original.rate3, - hasMinPrice: original.hasMinPrice, - minPrice: original.minPrice, - started: Date.vnNew(), - ended: Date.vnNew(), - }; + copy = { + id: 0, + started: today, + ended: nextWeek, + hasMinPrice: 0, + $index: 0, + }; + // } else + // copy = { + // $index: original.$index - 1, + // itemFk: original.itemFk, + // name: original.name, + // subName: original.subName, + // value5: original.value5, + // value6: original.value6, + // value7: original.value7, + // value8: original.value8, + // value9: original.value9, + // value10: original.value10, + // warehouseFk: original.warehouseFk, + // rate2: original.rate2, + // rate3: original.rate3, + // hasMinPrice: original.hasMinPrice, + // minPrice: original.minPrice, + // started: Date.vnNew(), + // ended: Date.vnNew(), + // }; return { original, copy }; }; @@ -436,10 +472,6 @@ function handleOnDataSave({ CrudModelRef }) { auto-load :is-editable="true" :right-search="false" - :table="{ - 'row-key': 'id', - selection: 'multiple', - }" :crud-model="{ paginate: false, }" @@ -585,7 +617,18 @@ function handleOnDataSave({ CrudModelRef }) { +