mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 11:55:23 +02:00
fix: restore row status on undo and improve change detection
This commit is contained in:
+32
-4
@@ -321,8 +321,23 @@ export default function App() {
|
||||
const currentPending = pendingRows[articleNo];
|
||||
const originalData = currentPending?.originalData ?? appState.data[rowIndex];
|
||||
|
||||
// Check if the new state is actually different from the original (not the current)
|
||||
const isActuallyModified = JSON.stringify(updatedRow) !== JSON.stringify(originalData);
|
||||
const isRowDifferent = (rowA: any[], rowB: any[]) => {
|
||||
if (!rowA || !rowB) return rowA !== rowB;
|
||||
const length = Math.max(rowA.length, rowB.length);
|
||||
for (let i = 0; i < length; i++) {
|
||||
const a = rowA[i];
|
||||
const b = rowB[i];
|
||||
if (a === b) continue;
|
||||
const normA = (a === null || a === undefined || a === '' || a === false) ? null : a;
|
||||
const normB = (b === null || b === undefined || b === '' || b === false) ? null : b;
|
||||
if (normA === normB) continue;
|
||||
if (String(a) === String(b)) continue;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const isActuallyModified = isRowDifferent(updatedRow, originalData);
|
||||
|
||||
if (isActuallyModified) {
|
||||
setAppState(prev => {
|
||||
@@ -355,7 +370,11 @@ export default function App() {
|
||||
setAppState(prev => {
|
||||
const newData = [...prev.data];
|
||||
newData[rowIndex] = updatedRow;
|
||||
const stillHasChanges = Object.keys(pendingRows).filter(k => k !== articleNo).length > 0;
|
||||
|
||||
// Use a more reliable way to check if there are still other pending rows
|
||||
const otherPendingCount = Object.keys(pendingRows).filter(k => k !== articleNo).length;
|
||||
const stillHasChanges = otherPendingCount > 0;
|
||||
|
||||
return { ...prev, data: newData, hasUnsavedChanges: stillHasChanges };
|
||||
});
|
||||
}
|
||||
@@ -435,6 +454,8 @@ export default function App() {
|
||||
setUndoHistory(prev => {
|
||||
const newState = {
|
||||
data: JSON.parse(JSON.stringify(appState.data)), // Deep copy
|
||||
rowStatuses: { ...rowStatuses },
|
||||
pendingRows: { ...pendingRows },
|
||||
message
|
||||
};
|
||||
// Keep last 50 steps
|
||||
@@ -447,11 +468,18 @@ export default function App() {
|
||||
if (undoHistory.length === 0) return;
|
||||
|
||||
const [lastAction, ...remainingHistory] = undoHistory;
|
||||
|
||||
// Restore data
|
||||
setAppState(prev => ({
|
||||
...prev,
|
||||
data: lastAction.data,
|
||||
hasUnsavedChanges: true
|
||||
hasUnsavedChanges: Object.keys(lastAction.pendingRows || {}).length > 0
|
||||
}));
|
||||
|
||||
// Restore statuses and pending state
|
||||
if (lastAction.rowStatuses) setRowStatuses(lastAction.rowStatuses);
|
||||
if (lastAction.pendingRows) setPendingRows(lastAction.pendingRows);
|
||||
|
||||
setUndoHistory(remainingHistory);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user