From dd19f0e3520034011b6d7e6ae0469306b0774c7a Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 27 May 2026 13:05:20 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20Refactor=20applyBatchUpdate=20in?= =?UTF-8?q?to=20smaller=20helper=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extracts validation, update preparation, and local grid update logic from the applyBatchUpdate function in core/ui/modules/sidebar.js into separate helper functions to improve maintainability and readability. --- core/ui/modules/sidebar.js | 48 +++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/core/ui/modules/sidebar.js b/core/ui/modules/sidebar.js index cffa2d4..6bf5bdf 100644 --- a/core/ui/modules/sidebar.js +++ b/core/ui/modules/sidebar.js @@ -342,12 +342,9 @@ export function updateBatchSidebar() { } } -export async function applyBatchUpdate() { - if (state.selectedCells.length === 0) return; - - const inputs = document.querySelectorAll('.batch-input'); - // 1. Validation and Setup Phase +// Validation and Setup +function validateBatchInputs(inputs, state, updateStatus) { const inputsByCol = new Map(); for (const input of inputs) { const colIdx = parseInt(input.dataset.colidx, 10); @@ -359,15 +356,16 @@ export async function applyBatchUpdate() { } catch (e) { const colDef = state.tableColumns[colIdx]; updateStatus(`Invalid JSON for patch in ${colDef.name}`); - return; + return null; // Indicates validation failure } } } + return inputsByCol; +} +function prepareBatchUpdates(selectedCells, inputsByCol, state) { const updates = []; - - // 2. Processing Phase - for (const cell of state.selectedCells) { + for (const cell of selectedCells) { const input = inputsByCol.get(cell.colIdx); if (!input) continue; @@ -407,6 +405,30 @@ export async function applyBatchUpdate() { colIdx: cell.colIdx // Local metadata }); } + return updates; +} + +function applyLocalGridUpdates(updates, state, getRowDataOffset) { + const hasPatch = updates.some(u => u.operation === 'json_patch'); + + if (!hasPatch) { + for (const u of updates) { + state.gridData[u.rowIdx][u.colIdx + getRowDataOffset()] = u.value; + } + } +} + +export async function applyBatchUpdate() { + if (state.selectedCells.length === 0) return; + + const inputs = document.querySelectorAll('.batch-input'); + + // 1. Validation and Setup Phase + const inputsByCol = validateBatchInputs(inputs, state, updateStatus); + if (!inputsByCol) return; // Validation failed + + // 2. Processing Phase + const updates = prepareBatchUpdates(state.selectedCells, inputsByCol, state); if (updates.length === 0) { updateStatus('No values entered for batch update'); @@ -429,13 +451,7 @@ export async function applyBatchUpdate() { await backendApi.updateCellBatch(state.selectedTable, backendUpdates, label); // Update local grid data - const hasPatch = updates.some(u => u.operation === 'json_patch'); - - if (!hasPatch) { - for (const u of updates) { - state.gridData[u.rowIdx][u.colIdx + getRowDataOffset()] = u.value; - } - } + applyLocalGridUpdates(updates, state, getRowDataOffset); // Refresh grid and sidebar await loadTableData(false);