Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions core/ui/modules/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

Expand Down Expand Up @@ -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');
Expand All @@ -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);
Expand Down