mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 12:35:25 +02:00
Fix save changes looping issue and authorize all Supabase API calls
This commit is contained in:
+41
-20
@@ -111,7 +111,7 @@ export default function App() {
|
||||
}
|
||||
|
||||
console.log('Fetching synced data from Supabase...');
|
||||
const syncedData = await getAllSyncedRows();
|
||||
const syncedData = await getAllSyncedRows(session?.access_token);
|
||||
|
||||
const articleNoIdx = COLUMNS.ARTICLE_NO;
|
||||
const processedRows = rows.map(row => {
|
||||
@@ -267,7 +267,7 @@ export default function App() {
|
||||
|
||||
// 3. Post-export: Reset pending statuses in Supabase
|
||||
console.log('Resetting pending statuses in Supabase...');
|
||||
resetAllPendingRows().then(success => {
|
||||
resetAllPendingRows(session?.access_token).then(success => {
|
||||
if (success) {
|
||||
console.log('Successfully reset all pending statuses');
|
||||
setRowStatuses({}); // Clear local statuses
|
||||
@@ -319,27 +319,47 @@ export default function App() {
|
||||
console.log('[handleSaveAll] No entries to save, returning');
|
||||
return;
|
||||
}
|
||||
|
||||
setIsSavingAll(true);
|
||||
let allSuccess = true;
|
||||
for (const [articleNo, { newData, originalData, articleName }] of entries) {
|
||||
console.log('[handleSaveAll] Saving article:', articleNo);
|
||||
const success = await saveRowToSupabase(articleNo, newData);
|
||||
console.log('[handleSaveAll] Save result for', articleNo, ':', success);
|
||||
if (success) {
|
||||
// Also save to history
|
||||
await saveHistoryEntry(articleNo, articleName, originalData, newData, session?.user?.email || 'unknown');
|
||||
let failedArticles: string[] = [];
|
||||
const token = session?.access_token;
|
||||
|
||||
try {
|
||||
for (const [articleNo, { newData, originalData, articleName }] of entries) {
|
||||
console.log('[handleSaveAll] Saving article:', articleNo);
|
||||
const success = await saveRowToSupabase(articleNo, newData, token);
|
||||
console.log('[handleSaveAll] Save result for', articleNo, ':', success);
|
||||
|
||||
setRowStatuses(prev => ({ ...prev, [articleNo]: 'saved' }));
|
||||
setPendingRows(prev => { const n = { ...prev }; delete n[articleNo]; return n; });
|
||||
} else {
|
||||
setRowStatuses(prev => ({ ...prev, [articleNo]: 'error' }));
|
||||
allSuccess = false;
|
||||
if (success) {
|
||||
// Also save to history
|
||||
await saveHistoryEntry(articleNo, articleName, originalData, newData, session?.user?.email || 'unknown', token);
|
||||
|
||||
setRowStatuses(prev => ({ ...prev, [articleNo]: 'saved' }));
|
||||
setPendingRows(prev => {
|
||||
const n = { ...prev };
|
||||
delete n[articleNo];
|
||||
return n;
|
||||
});
|
||||
} else {
|
||||
setRowStatuses(prev => ({ ...prev, [articleNo]: 'error' }));
|
||||
failedArticles.push(articleNo);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('[handleSaveAll] Finished loop. Failed:', failedArticles.length);
|
||||
|
||||
if (failedArticles.length > 0) {
|
||||
alert(`Failed to save ${failedArticles.length} items: ${failedArticles.join(', ')}. Please try again.`);
|
||||
} else {
|
||||
setAppState(prev => ({ ...prev, hasUnsavedChanges: false }));
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[handleSaveAll] Critical error:', err);
|
||||
alert('A critical error occurred while saving. Please check your connection and try again.');
|
||||
} finally {
|
||||
setIsSavingAll(false);
|
||||
console.log('[handleSaveAll] isSavingAll set to false');
|
||||
}
|
||||
console.log('[handleSaveAll] Finished, allSuccess:', allSuccess);
|
||||
if (allSuccess) setAppState(prev => ({ ...prev, hasUnsavedChanges: false }));
|
||||
setIsSavingAll(false);
|
||||
console.log('[handleSaveAll] isSavingAll set to false');
|
||||
};
|
||||
|
||||
const captureState = (message: string) => {
|
||||
@@ -506,6 +526,7 @@ export default function App() {
|
||||
<HistoryView
|
||||
headers={appState.headers}
|
||||
data={appState.data}
|
||||
sessionToken={session?.access_token}
|
||||
onRevert={async (articleNo, revertedData, historyId) => {
|
||||
// Find the row in appState.data and update it
|
||||
const rowIndex = appState.data.findIndex(r => String(r[COLUMNS.ARTICLE_NO]) === articleNo);
|
||||
@@ -528,7 +549,7 @@ export default function App() {
|
||||
}));
|
||||
// Delete the history entry after revert
|
||||
if (historyId) {
|
||||
await deleteHistoryEntry(String(historyId));
|
||||
await deleteHistoryEntry(String(historyId), session?.access_token);
|
||||
}
|
||||
}
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user