From d98044692fd8005a63784f4932b76c51035093df Mon Sep 17 00:00:00 2001 From: suryacp23 Date: Wed, 29 Jul 2026 17:04:00 +0530 Subject: [PATCH] edit option for the populated same product --- src/components/forms/DynamicForm.tsx | 6 ++--- src/components/forms/fields/OrderGrid.tsx | 27 ++++++++++++++++--- .../forms/fields/StorePotentialGrid.tsx | 15 ++++++++--- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/components/forms/DynamicForm.tsx b/src/components/forms/DynamicForm.tsx index 186e9c8..c99ed00 100644 --- a/src/components/forms/DynamicForm.tsx +++ b/src/components/forms/DynamicForm.tsx @@ -385,11 +385,11 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: if (cat && prod) { const key = `${cat}::${prod}`; - if (indexMap.has(key)) { + if (indexMap.has(key) && currentBags > 0) { const targetIdx = indexMap.get(key)!; const existingRow = merged[targetIdx]; const existingBags = Number(getGridVal(existingRow, ['bags', 'quantity'])) || 0; - const addedBags = currentBags > 0 ? currentBags : 1; + const addedBags = currentBags; const newBags = existingBags + addedBags; const skuVal = Number(getGridVal(existingRow, ['sku'], ['code']) || currentSku || 0); @@ -400,7 +400,7 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: row_kgs: skuVal * newBags, }; continue; - } else { + } else if (!indexMap.has(key)) { indexMap.set(key, merged.length); } } diff --git a/src/components/forms/fields/OrderGrid.tsx b/src/components/forms/fields/OrderGrid.tsx index 95639b7..13cc458 100644 --- a/src/components/forms/fields/OrderGrid.tsx +++ b/src/components/forms/fields/OrderGrid.tsx @@ -78,12 +78,12 @@ export function OrderGrid({ if (cat && prod) { const key = `${cat}::${prod}`; - if (indexMap.has(key)) { + if (indexMap.has(key) && currentBags > 0) { const targetIdx = indexMap.get(key)!; const existingRow = merged[targetIdx]; const existingBags = Number(existingRow[bagsColId] ?? existingRow.bags ?? existingRow.quantity ?? 0); - const addedBags = currentBags > 0 ? currentBags : 1; + const addedBags = currentBags; const newBags = existingBags + addedBags; const skuVal = Number(existingRow.sku || r.sku || 0); @@ -97,7 +97,7 @@ export function OrderGrid({ merged[targetIdx] = updatedRow; continue; - } else { + } else if (!indexMap.has(key)) { indexMap.set(key, merged.length); } } @@ -206,6 +206,7 @@ export function OrderGrid({ const isCatField = isCategoryColumn(fieldId, colName); const isProdField = isProductColumn(fieldId, colName); + const isBagsField = colDef?.data_type === 'number' || fieldId.includes('bags') || fieldId.includes('quantity') || colName.toLowerCase().includes('bags'); if (isCatField) { const catVal = String(val ?? ''); @@ -222,6 +223,10 @@ export function OrderGrid({ row['br'] = ''; row['sku_code'] = ''; row['skucode'] = ''; + row['bags'] = ''; + row['quantity'] = ''; + if (bagsCol) row[bagsCol.id] = ''; + row['row_kgs'] = 0; } else if (isProdField) { const currentCat = String(row['category'] || row['product_category'] || (catCol ? row[catCol.id] : '') || ''); const prodOptions = getProductsForCategory(currentCat); @@ -261,6 +266,20 @@ export function OrderGrid({ row['product_category'] = cat; if (catCol) row[catCol.id] = cat; } + + const bagsVal = Number(row.bags ?? row.quantity ?? (bagsCol ? row[bagsCol.id] : 0)) || 0; + row['row_kgs'] = Number(sku || 0) * bagsVal; + } else if (isBagsField) { + const parsedVal = val !== '' && val !== null && val !== undefined ? val : ''; + const numericVal = Number(val) || 0; + + row[fieldId] = parsedVal; + row['bags'] = parsedVal; + row['quantity'] = parsedVal; + if (bagsCol) row[bagsCol.id] = parsedVal; + + const skuVal = Number(row.sku || 0); + row['row_kgs'] = skuVal * numericVal; } else { row[fieldId] = val; } @@ -278,7 +297,7 @@ export function OrderGrid({ }); onChange(cleanRows); - }, [rows, columns, visibleColumns, prodNameCol, catCol, getProductsForCategory, onChange]); + }, [rows, columns, visibleColumns, prodNameCol, catCol, bagsCol, getProductsForCategory, onChange]); const { calculatedBags, calculatedKgs } = useMemo(() => { let bagsAcc = 0; diff --git a/src/components/forms/fields/StorePotentialGrid.tsx b/src/components/forms/fields/StorePotentialGrid.tsx index fa8f47f..c275a05 100644 --- a/src/components/forms/fields/StorePotentialGrid.tsx +++ b/src/components/forms/fields/StorePotentialGrid.tsx @@ -66,12 +66,12 @@ export function StorePotentialGrid({ const currentBags = Number(r[bagsColId] ?? r.bags ?? r.quantity ?? 0); if (cat) { - if (indexMap.has(cat)) { + if (indexMap.has(cat) && currentBags > 0) { const targetIdx = indexMap.get(cat)!; const existingRow = merged[targetIdx]; const existingBags = Number(existingRow[bagsColId] ?? existingRow.bags ?? existingRow.quantity ?? 0); - const addedBags = currentBags > 0 ? currentBags : 1; + const addedBags = currentBags; const newBags = existingBags + addedBags; const updatedRow: Record = { @@ -83,7 +83,7 @@ export function StorePotentialGrid({ merged[targetIdx] = updatedRow; continue; - } else { + } else if (!indexMap.has(cat)) { indexMap.set(cat, merged.length); } } @@ -124,19 +124,26 @@ export function StorePotentialGrid({ const colDef = columns.find((c) => c.id === fieldId); const colName = colDef?.name || ''; const isCatField = isCategoryColumn(fieldId, colName); + const isBagsField = colDef?.data_type === 'number' || fieldId.includes('bags') || fieldId.includes('quantity') || colName.toLowerCase().includes('bags'); if (isCatField) { const catVal = String(val ?? ''); row[fieldId] = catVal; row['category'] = catVal; row['product_category'] = catVal; + } else if (isBagsField) { + const parsedVal = val !== '' && val !== null && val !== undefined ? val : ''; + row[fieldId] = parsedVal; + row['bags'] = parsedVal; + row['quantity'] = parsedVal; + if (bagsCol) row[bagsCol.id] = parsedVal; } else { row[fieldId] = val; } next[rowIdx] = row; onChange(next); - }, [rows, columns, onChange]); + }, [rows, columns, bagsCol, onChange]); return (