mirror of
https://github.com/trevor1969/teatracker.git
synced 2026-08-09 10:41:59 +00:00
Compare commits
3 Commits
a16da66c2d
...
91367a7198
| Author | SHA1 | Date | |
|---|---|---|---|
| 91367a7198 | |||
| 7e1f52a5b2 | |||
| 7f059c9b4c |
98
app.js
98
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,12 +281,31 @@ class TeeTracker {
|
||||
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 +313,19 @@ class TeeTracker {
|
||||
...entry,
|
||||
teaspoons: entry.teaspoons !== undefined ? entry.teaspoons : 1
|
||||
}));
|
||||
}
|
||||
|
||||
// If Nextcloud loading failed, fall back to localStorage
|
||||
if (!teas || !entries) {
|
||||
this.loadFromLocalStorage();
|
||||
} 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
|
||||
@ -949,30 +977,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 +989,38 @@ class TeeTracker {
|
||||
this.nextcloudStorage = ncStorage;
|
||||
this.useNextcloud = true;
|
||||
|
||||
// Save current data to Nextcloud
|
||||
await this.saveData();
|
||||
// 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 {
|
||||
|
||||
Reference in New Issue
Block a user