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) {
|
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,12 +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');
|
||||||
|
|
||||||
|
// 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 +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 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
|
||||||
@ -949,30 +977,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 +989,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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user