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

64
app.js
View File

@ -280,6 +280,7 @@ 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
if (teas) { if (teas) {
this.teas = teas.map(tea => ({ this.teas = teas.map(tea => ({
...tea, ...tea,
@ -295,9 +296,38 @@ class TeeTracker {
})); }));
} }
// If Nextcloud loading failed, fall back to localStorage // If any data is missing from Nextcloud, load from localStorage
if (!teas || !entries) { // This prevents data loss when switching to Nextcloud
if (!teas && !entries) {
// Both Nextcloud files are missing, load everything from localStorage
this.loadFromLocalStorage(); 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 { } else {
// Load from localStorage // Load from localStorage
@ -949,30 +979,6 @@ class TeeTracker {
return; 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 // Save config WITHOUT password for security
// Password will be requested each time or stored in sessionStorage // Password will be requested each time or stored in sessionStorage
const config = { baseUrl: url, username, path }; const config = { baseUrl: url, username, path };
@ -985,7 +991,11 @@ class TeeTracker {
this.nextcloudStorage = ncStorage; this.nextcloudStorage = ncStorage;
this.useNextcloud = true; 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(); await this.saveData();
this.showStatusMessage('✅ Konfiguration gespeichert und Daten synchronisiert!', 'success'); this.showStatusMessage('✅ Konfiguration gespeichert und Daten synchronisiert!', 'success');