mirror of
https://github.com/trevor1969/teatracker.git
synced 2026-08-09 10:41:59 +00:00
Compare commits
6 Commits
a16da66c2d
...
v2.5
| Author | SHA1 | Date | |
|---|---|---|---|
| c628cf2069 | |||
| f111153bde | |||
| 0baceb14c6 | |||
| 91367a7198 | |||
| 7e1f52a5b2 | |||
| 7f059c9b4c |
129
app.js
129
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;
|
||||
}
|
||||
@ -225,8 +226,11 @@ class TeeTracker {
|
||||
|
||||
// Initialize
|
||||
this.initStorage();
|
||||
this.setupEventListeners();
|
||||
this.loadData().then(() => {
|
||||
this.init();
|
||||
this.renderAll();
|
||||
this.initCharts();
|
||||
this.updateSettingsStats();
|
||||
});
|
||||
}
|
||||
|
||||
@ -275,17 +279,36 @@ class TeeTracker {
|
||||
await this.nextcloudStorage.ensureDirectory();
|
||||
|
||||
// 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 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) {
|
||||
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) {
|
||||
@ -293,11 +316,19 @@ class TeeTracker {
|
||||
...entry,
|
||||
teaspoons: entry.teaspoons !== undefined ? entry.teaspoons : 1
|
||||
}));
|
||||
} 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 = [];
|
||||
}
|
||||
|
||||
// If Nextcloud loading failed, fall back to localStorage
|
||||
if (!teas || !entries) {
|
||||
this.loadFromLocalStorage();
|
||||
} else {
|
||||
this.entries = [];
|
||||
}
|
||||
} else {
|
||||
// Load from localStorage
|
||||
@ -603,7 +634,8 @@ class TeeTracker {
|
||||
// ============================================
|
||||
|
||||
init() {
|
||||
this.setupEventListeners();
|
||||
// Initialization is now handled in constructor
|
||||
// This method is kept for backward compatibility
|
||||
this.renderAll();
|
||||
this.initCharts();
|
||||
this.updateSettingsStats();
|
||||
@ -620,6 +652,23 @@ class TeeTracker {
|
||||
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
|
||||
if (document.getElementById('close-modal')) {
|
||||
document.getElementById('close-modal').addEventListener('click', () => this.closeTeaModal());
|
||||
@ -949,30 +998,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,8 +1010,38 @@ 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();
|
||||
|
||||
// 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');
|
||||
} else {
|
||||
@ -1270,8 +1325,8 @@ class TeeTracker {
|
||||
${tea.brand ? `<div class="tea-brand">Marke: ${tea.brand}</div>` : ''}
|
||||
${tea.description ? `<div class="tea-description">${tea.description}</div>` : ''}
|
||||
<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-delete-btn" onclick="window.app.showDeleteTeaConfirm('${tea.id}')">Löschen</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" data-tea-id="${tea.id}" data-action="delete">Löschen</button>
|
||||
</div>
|
||||
</div>
|
||||
`).join('');
|
||||
@ -1318,8 +1373,8 @@ class TeeTracker {
|
||||
${tea.brand ? `<div class="tea-brand">Marke: ${tea.brand}</div>` : ''}
|
||||
${tea.description ? `<div class="tea-description">${tea.description}</div>` : ''}
|
||||
<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-delete-btn" onclick="window.app.showDeleteTeaConfirm('${tea.id}')">Löschen</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" data-tea-id="${tea.id}" data-action="delete">Löschen</button>
|
||||
</div>
|
||||
</div>
|
||||
`).join('');
|
||||
|
||||
@ -386,8 +386,8 @@
|
||||
<textarea id="import-data-textarea" class="modal-textarea" placeholder="{\n \"teas\": [...],\n \"entries\": [...]\n}"></textarea>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button id="import-data-btn" class="btn btn-primary" onclick="window.app.handleImportData()">📥 Importieren</button>
|
||||
<button id="cancel-import-btn" class="btn btn-secondary" onclick="window.app.closeImportModal()">Abbrechen</button>
|
||||
<button id="import-data-btn" class="btn btn-primary">📥 Importieren</button>
|
||||
<button id="cancel-import-btn" class="btn btn-secondary">Abbrechen</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user