From 7e1f52a5b2d14a0fc9749d3a93908aacfb5aeff7 Mon Sep 17 00:00:00 2001 From: Vibe Nuage Agent Date: Fri, 5 Jun 2026 11:26:26 +0000 Subject: [PATCH] Fix: Behandle leere Nextcloud-Dateien korrekt - Aendert loadFile(): Gibt leeres Array zurueck, wenn Datei existiert aber leer ist - Verhindert, dass leere Dateien als null interpretiert werden und lokale Daten oder leere Arrays geladen werden Fixes: Datenverlust bei Nextcloud-Anmeldung mit leeren Dateien Co-authored-by: trevor1969 --- app.js | 68 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/app.js b/app.js index b9cb07b..f1180cf 100644 --- a/app.js +++ b/app.js @@ -135,7 +135,8 @@ class NextcloudStorage { if (response.ok) { const text = await response.text(); - return text ? JSON.parse(text) : null; + // Return empty array if file is empty, otherwise parse JSON + return text.trim() === '' ? [] : (text ? JSON.parse(text) : null); } else if (response.status === 404) { return null; } @@ -280,13 +281,31 @@ 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 + // Always load from localStorage first as backup + const localTeas = localStorage.getItem(STORAGE_KEY_TEAS); + const localEntries = localStorage.getItem(STORAGE_KEY_ENTRIES); + + // Load from Nextcloud if available, otherwise use local data if (teas) { this.teas = teas.map(tea => ({ ...tea, organic: tea.organic !== undefined ? tea.organic : false, rating: tea.rating !== undefined ? tea.rating : 3 })); + } else if (localTeas) { + // Nextcloud has no teas, use local data + 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); + this.teas = []; + } + } else { + this.teas = []; } if (entries) { @@ -294,40 +313,19 @@ class TeeTracker { ...entry, teaspoons: entry.teaspoons !== undefined ? entry.teaspoons : 1 })); - } - - // 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 if (localEntries) { + // Nextcloud has no entries, use local data + 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); + this.entries = []; } + } else { + this.entries = []; } } else { // Load from localStorage