From 7f059c9b4c2572b65b373c4f285e6aa11a0f349f Mon Sep 17 00:00:00 2001 From: Vibe Nuage Agent Date: Fri, 5 Jun 2026 10:50:20 +0000 Subject: [PATCH] Fix: Lade Nextcloud-Daten vor dem Speichern, um Datenverlust zu vermeiden - Aendert loadData(): Laedt zuerst Nextcloud-Daten und faellt nur bei fehlenden Dateien auf localStorage zurueck - Aendert saveNextcloudConfig(): Laedt Daten von Nextcloud BEVOR sie gespeichert werden, um Ueberschreiben bestehender Nextcloud-Daten zu verhindern - Behebt das Problem, dass lokale Daten die Nextcloud-Daten ueberschreiben Fixes: Datenverlust bei Nextcloud-Anmeldung Co-authored-by: trevor1969 --- app.js | 64 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/app.js b/app.js index b92fc65..b9cb07b 100644 --- a/app.js +++ b/app.js @@ -280,6 +280,7 @@ class TeeTracker { const teas = await this.nextcloudStorage.loadFile(DATA_DIRECTORY + 'teas.json'); const entries = await this.nextcloudStorage.loadFile(DATA_DIRECTORY + 'entries.json'); + // Load from Nextcloud if available if (teas) { this.teas = teas.map(tea => ({ ...tea, @@ -295,9 +296,38 @@ class TeeTracker { })); } - // If Nextcloud loading failed, fall back to localStorage - if (!teas || !entries) { + // If any data is missing from Nextcloud, load from localStorage + // This prevents data loss when switching to Nextcloud + if (!teas && !entries) { + // Both Nextcloud files are missing, load everything from localStorage this.loadFromLocalStorage(); + } else if (!teas) { + // Only teas.json is missing from Nextcloud, load teas from localStorage + const localTeas = localStorage.getItem(STORAGE_KEY_TEAS); + if (localTeas) { + try { + this.teas = JSON.parse(localTeas).map(tea => ({ + ...tea, + organic: tea.organic !== undefined ? tea.organic : false, + rating: tea.rating !== undefined ? tea.rating : 3 + })); + } catch (error) { + console.error('Error loading local teas:', error); + } + } + } else if (!entries) { + // Only entries.json is missing from Nextcloud, load entries from localStorage + const localEntries = localStorage.getItem(STORAGE_KEY_ENTRIES); + if (localEntries) { + try { + this.entries = JSON.parse(localEntries).map(entry => ({ + ...entry, + teaspoons: entry.teaspoons !== undefined ? entry.teaspoons : 1 + })); + } catch (error) { + console.error('Error loading local entries:', error); + } + } } } else { // Load from localStorage @@ -949,30 +979,6 @@ class TeeTracker { return; } - // Load existing data from localStorage before switching to Nextcloud - // This prevents data loss when connecting to Nextcloud for the first time - const existingTeas = localStorage.getItem(STORAGE_KEY_TEAS); - const existingEntries = localStorage.getItem(STORAGE_KEY_ENTRIES); - - if (existingTeas) { - try { - this.teas = JSON.parse(existingTeas).map(tea => ({ - ...tea, - organic: tea.organic !== undefined ? tea.organic : false - })); - } catch (error) { - console.error('Error loading existing teas:', error); - } - } - - if (existingEntries) { - try { - this.entries = JSON.parse(existingEntries); - } catch (error) { - console.error('Error loading existing entries:', error); - } - } - // Save config WITHOUT password for security // Password will be requested each time or stored in sessionStorage const config = { baseUrl: url, username, path }; @@ -985,7 +991,11 @@ class TeeTracker { this.nextcloudStorage = ncStorage; this.useNextcloud = true; - // Save current data to Nextcloud + // Load data from Nextcloud first (this is the critical fix!) + // This will automatically fall back to localStorage if Nextcloud has no data + await this.loadData(); + + // Save the loaded/merged data to Nextcloud await this.saveData(); this.showStatusMessage('✅ Konfiguration gespeichert und Daten synchronisiert!', 'success');