From 8c80cf036df136f8947daa4880a5c14f628e0114 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Mon, 6 Jan 2025 10:41:26 +0700 Subject: [PATCH] fix: select worker does not update select product worker --- src/components/05_quotation/ProductItem.vue | 41 +++++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/components/05_quotation/ProductItem.vue b/src/components/05_quotation/ProductItem.vue index ac0f0949..4c685312 100644 --- a/src/components/05_quotation/ProductItem.vue +++ b/src/components/05_quotation/ProductItem.vue @@ -212,34 +212,43 @@ function handleCheck( index: number, data: QuotationPayload['productServiceList'][number], ) { + const equals = data.amount === data.workerIndex.length; const target = data.workerIndex.indexOf(index); if (target > -1) { - data.amount -= 1; data.workerIndex.splice(target, 1); + if (equals) data.amount -= 1; } else { data.workerIndex.push(index); - data.amount += 1; + if (equals) data.amount += 1; } + data.amount = Math.max(data.workerIndex.length, data.amount); } watch( () => props.employeeRows, - (a, b) => { - if (a === undefined || b === undefined) return; + (current, before) => { + if (current === undefined || before === undefined) return; - const removed = b.findIndex( - (c) => !a.find((d) => JSON.stringify(c) === JSON.stringify(d)), - ); + rows.value.forEach((items) => { + const mapping = items.workerIndex.map((v) => before[v]); + const incoming = current.filter( + (lhs) => + !before.find((rhs) => { + console.log(lhs, rhs); + return JSON.stringify(lhs) === JSON.stringify(rhs); + }), + ); + const selected = mapping.concat(incoming); - if (a.length < b.length) { - rows.value.forEach((p) => { - const maxValue = Math.max(...p.workerIndex); - if (removed !== -1) { - p.workerIndex = p.workerIndex.map((i) => (i > removed ? i - 1 : i)); - } - p.workerIndex = p.workerIndex.filter((i) => i !== maxValue); - }); - } + items.workerIndex = selected + .map((lhs) => + current.findIndex((rhs) => { + return JSON.stringify(lhs) === JSON.stringify(rhs); + }), + ) + .filter((v) => v !== -1); + items.amount = items.workerIndex.length; + }); }, );