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 <trevor1969@users.noreply.github.com>
This commit is contained in:
Vibe Nuage Agent
2026-06-05 11:26:26 +00:00
parent 7f059c9b4c
commit 7e1f52a5b2

68
app.js
View File

@ -135,7 +135,8 @@ class NextcloudStorage {
if (response.ok) { if (response.ok) {
const text = await response.text(); 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) { } else if (response.status === 404) {
return null; return null;
} }
@ -280,13 +281,31 @@ class TeeTracker {
const teas = await this.nextcloudStorage.loadFile(DATA_DIRECTORY + 'teas.json'); const teas = await this.nextcloudStorage.loadFile(DATA_DIRECTORY + 'teas.json');
const entries = await this.nextcloudStorage.loadFile(DATA_DIRECTORY + 'entries.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) { if (teas) {
this.teas = teas.map(tea => ({ this.teas = teas.map(tea => ({
...tea, ...tea,
organic: tea.organic !== undefined ? tea.organic : false, organic: tea.organic !== undefined ? tea.organic : false,
rating: tea.rating !== undefined ? tea.rating : 3 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) { if (entries) {
@ -294,40 +313,19 @@ class TeeTracker {
...entry, ...entry,
teaspoons: entry.teaspoons !== undefined ? entry.teaspoons : 1 teaspoons: entry.teaspoons !== undefined ? entry.teaspoons : 1
})); }));
} } else if (localEntries) {
// Nextcloud has no entries, use local data
// If any data is missing from Nextcloud, load from localStorage try {
// This prevents data loss when switching to Nextcloud this.entries = JSON.parse(localEntries).map(entry => ({
if (!teas && !entries) { ...entry,
// Both Nextcloud files are missing, load everything from localStorage teaspoons: entry.teaspoons !== undefined ? entry.teaspoons : 1
this.loadFromLocalStorage(); }));
} else if (!teas) { } catch (error) {
// Only teas.json is missing from Nextcloud, load teas from localStorage console.error('Error loading local entries:', error);
const localTeas = localStorage.getItem(STORAGE_KEY_TEAS); this.entries = [];
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 {
this.entries = [];
} }
} else { } else {
// Load from localStorage // Load from localStorage