6 Commits

Author SHA1 Message Date
c628cf2069 Fix: Korrigiert Aufruf von migrateToDataDirectory - muss auf nextcloudStorage-Instanz aufgerufen werden, nicht auf TeeTracker-Instanz
Co-authored-by: trevor1969 <trevor1969@users.noreply.github.com>
2026-06-05 18:58:01 +00:00
f111153bde Fix: Event-Delegation für dynamisch erstellte Buttons (Bearbeiten/Löschen in Tee-Karten). Entfernt onclick-Attribute und ersetzt sie durch data-Attribute + Event-Delegation auf dem Container.
Co-authored-by: trevor1969 <trevor1969@users.noreply.github.com>
2026-06-05 18:50:09 +00:00
0baceb14c6 Fix: Buttons reagieren nicht auf Klicks - Event-Listener werden jetzt sofort registriert, nicht erst nach dem Laden der Daten. Entfernt onclick-Attribute aus HTML, die zu Konflikten führen können.
Co-authored-by: trevor1969 <trevor1969@users.noreply.github.com>
2026-06-05 18:48:49 +00:00
91367a7198 Fix: Verhindere Ueberschreiben von Nextcloud-Daten mit leeren Arrays
- In saveNextcloudConfig(): Nur speichern, wenn Daten vorhanden sind
- Falls keine lokalen Daten, aber Nextcloud hat Daten: behalte Nextcloud-Daten
- Verhindert Datenverlust, wenn Nextcloud bereits Daten hat

Fixes: Datenverlust bei Nextcloud-Anmeldung

Co-authored-by: trevor1969 <trevor1969@users.noreply.github.com>
2026-06-05 11:28:05 +00:00
7e1f52a5b2 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>
2026-06-05 11:26:26 +00:00
7f059c9b4c 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>
2026-06-05 10:50:20 +00:00
2 changed files with 96 additions and 41 deletions

133
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;
} }
@ -225,8 +226,11 @@ class TeeTracker {
// Initialize // Initialize
this.initStorage(); this.initStorage();
this.setupEventListeners();
this.loadData().then(() => { this.loadData().then(() => {
this.init(); this.renderAll();
this.initCharts();
this.updateSettingsStats();
}); });
} }
@ -275,17 +279,36 @@ class TeeTracker {
await this.nextcloudStorage.ensureDirectory(); await this.nextcloudStorage.ensureDirectory();
// Check if old files exist in root directory and migrate to data/ directory // Check if old files exist in root directory and migrate to data/ directory
await this.migrateToDataDirectory(); await this.nextcloudStorage.migrateToDataDirectory();
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');
// 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) {
@ -293,11 +316,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 Nextcloud loading failed, fall back to localStorage try {
if (!teas || !entries) { this.entries = JSON.parse(localEntries).map(entry => ({
this.loadFromLocalStorage(); ...entry,
teaspoons: entry.teaspoons !== undefined ? entry.teaspoons : 1
}));
} catch (error) {
console.error('Error loading local entries:', error);
this.entries = [];
}
} else {
this.entries = [];
} }
} else { } else {
// Load from localStorage // Load from localStorage
@ -603,7 +634,8 @@ class TeeTracker {
// ============================================ // ============================================
init() { init() {
this.setupEventListeners(); // Initialization is now handled in constructor
// This method is kept for backward compatibility
this.renderAll(); this.renderAll();
this.initCharts(); this.initCharts();
this.updateSettingsStats(); this.updateSettingsStats();
@ -620,6 +652,23 @@ class TeeTracker {
document.getElementById('add-tea-btn').addEventListener('click', () => this.openTeaModal()); document.getElementById('add-tea-btn').addEventListener('click', () => this.openTeaModal());
} }
// Event delegation for tea action buttons (edit/delete)
const teasListContainer = document.getElementById('teas-list');
if (teasListContainer) {
teasListContainer.addEventListener('click', (e) => {
const editBtn = e.target.closest('.tea-edit-btn');
const deleteBtn = e.target.closest('.tea-delete-btn');
if (editBtn) {
const teaId = editBtn.dataset.teaId;
if (teaId) this.editTea(teaId);
} else if (deleteBtn) {
const teaId = deleteBtn.dataset.teaId;
if (teaId) this.showDeleteTeaConfirm(teaId);
}
});
}
// Tea modal // Tea modal
if (document.getElementById('close-modal')) { if (document.getElementById('close-modal')) {
document.getElementById('close-modal').addEventListener('click', () => this.closeTeaModal()); document.getElementById('close-modal').addEventListener('click', () => this.closeTeaModal());
@ -949,30 +998,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,8 +1010,38 @@ 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!)
await this.saveData(); // This will automatically fall back to localStorage if Nextcloud has no data
await this.loadData();
// Only save to Nextcloud if we actually have data to save
// This prevents overwriting existing Nextcloud data with empty arrays
if (this.teas.length > 0 || this.entries.length > 0) {
await this.saveData();
} else {
// No data to save, but check if Nextcloud has existing data
const existingTeas = await ncStorage.loadFile(DATA_DIRECTORY + 'teas.json');
const existingEntries = await ncStorage.loadFile(DATA_DIRECTORY + 'entries.json');
// If Nextcloud has data, keep it and load it
if (existingTeas || existingEntries) {
if (existingTeas) {
this.teas = existingTeas.map(tea => ({
...tea,
organic: tea.organic !== undefined ? tea.organic : false,
rating: tea.rating !== undefined ? tea.rating : 3
}));
}
if (existingEntries) {
this.entries = existingEntries.map(entry => ({
...entry,
teaspoons: entry.teaspoons !== undefined ? entry.teaspoons : 1
}));
}
// Don't save, just use the existing Nextcloud data
}
// If Nextcloud has no data and we have no data, nothing to do
}
this.showStatusMessage('✅ Konfiguration gespeichert und Daten synchronisiert!', 'success'); this.showStatusMessage('✅ Konfiguration gespeichert und Daten synchronisiert!', 'success');
} else { } else {
@ -1270,8 +1325,8 @@ class TeeTracker {
${tea.brand ? `<div class="tea-brand">Marke: ${tea.brand}</div>` : ''} ${tea.brand ? `<div class="tea-brand">Marke: ${tea.brand}</div>` : ''}
${tea.description ? `<div class="tea-description">${tea.description}</div>` : ''} ${tea.description ? `<div class="tea-description">${tea.description}</div>` : ''}
<div class="tea-actions"> <div class="tea-actions">
<button class="tea-action-btn tea-edit-btn" onclick="window.app.editTea('${tea.id}')">Bearbeiten</button> <button class="tea-action-btn tea-edit-btn" data-tea-id="${tea.id}" data-action="edit">Bearbeiten</button>
<button class="tea-action-btn tea-delete-btn" onclick="window.app.showDeleteTeaConfirm('${tea.id}')">Löschen</button> <button class="tea-action-btn tea-delete-btn" data-tea-id="${tea.id}" data-action="delete">Löschen</button>
</div> </div>
</div> </div>
`).join(''); `).join('');
@ -1318,8 +1373,8 @@ class TeeTracker {
${tea.brand ? `<div class="tea-brand">Marke: ${tea.brand}</div>` : ''} ${tea.brand ? `<div class="tea-brand">Marke: ${tea.brand}</div>` : ''}
${tea.description ? `<div class="tea-description">${tea.description}</div>` : ''} ${tea.description ? `<div class="tea-description">${tea.description}</div>` : ''}
<div class="tea-actions"> <div class="tea-actions">
<button class="tea-action-btn tea-edit-btn" onclick="window.app.editTea('${tea.id}')">Bearbeiten</button> <button class="tea-action-btn tea-edit-btn" data-tea-id="${tea.id}" data-action="edit">Bearbeiten</button>
<button class="tea-action-btn tea-delete-btn" onclick="window.app.showDeleteTeaConfirm('${tea.id}')">Löschen</button> <button class="tea-action-btn tea-delete-btn" data-tea-id="${tea.id}" data-action="delete">Löschen</button>
</div> </div>
</div> </div>
`).join(''); `).join('');

View File

@ -386,8 +386,8 @@
<textarea id="import-data-textarea" class="modal-textarea" placeholder="{\n \"teas\": [...],\n \"entries\": [...]\n}"></textarea> <textarea id="import-data-textarea" class="modal-textarea" placeholder="{\n \"teas\": [...],\n \"entries\": [...]\n}"></textarea>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button id="import-data-btn" class="btn btn-primary" onclick="window.app.handleImportData()">📥 Importieren</button> <button id="import-data-btn" class="btn btn-primary">📥 Importieren</button>
<button id="cancel-import-btn" class="btn btn-secondary" onclick="window.app.closeImportModal()">Abbrechen</button> <button id="cancel-import-btn" class="btn btn-secondary">Abbrechen</button>
</div> </div>
</div> </div>
</div> </div>