Feature: Backup- und Import-Funktion hinzugefügt
- Backup-Button zum Herunterladen aller Daten als JSON - Import-Funktion zum Wiederherstellen aus JSON-Backup - Neue Backup-Sektion in der UI - Keine Optional-Chaining-Operatoren verwendet Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
11
index.html
11
index.html
@ -144,6 +144,17 @@
|
||||
</div>
|
||||
<div id="historyList" class="history-list"></div>
|
||||
</section>
|
||||
<!-- Backup Section -->
|
||||
<section class="backup-section">
|
||||
<h2>💾 Backup & Wiederherstellung</h2>
|
||||
<div class="backup-controls">
|
||||
<button id="btnBackup" class="btn btn-primary">📥 Backup herunterladen (JSON)</button>
|
||||
<label class="btn btn-secondary" for="fileImport">📤 Backup wiederherstellen
|
||||
<input type="file" id="fileImport" accept=".json" style="display:none">
|
||||
</label>
|
||||
<span id="backupStatus" class="backup-status"></span>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<footer class="footer">
|
||||
|
||||
111
script.js
111
script.js
@ -144,6 +144,19 @@ const App = {
|
||||
// Datum auf heute setzen
|
||||
el = document.getElementById('trackDate');
|
||||
if (el) el.setAttribute('value', new Date().toISOString().split('T')[0]);
|
||||
|
||||
// Backup-Button
|
||||
el = document.getElementById('btnBackup');
|
||||
if (el) el.addEventListener('click', () => { this.backupData(); });
|
||||
|
||||
// Import-Datei
|
||||
el = document.getElementById('fileImport');
|
||||
if (el) el.addEventListener('change', (e) => {
|
||||
if (e.target.files && e.target.files[0]) {
|
||||
this.importData(e.target.files[0]);
|
||||
e.target.value = '';
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Google Sign-In
|
||||
@ -900,6 +913,104 @@ const App = {
|
||||
if (yearSpan) {
|
||||
yearSpan.textContent = new Date().getFullYear();
|
||||
}
|
||||
},
|
||||
|
||||
// Backup: Alle Daten als JSON herunterladen
|
||||
backupData() {
|
||||
if (!this.state.currentUser) {
|
||||
this.showToast('Bitte melden Sie sich zuerst an', 'error');
|
||||
return;
|
||||
}
|
||||
var data = {
|
||||
app: 'TeeTracker',
|
||||
version: '1.0',
|
||||
exportDate: new Date().toISOString(),
|
||||
userId: this.state.currentUser.uid,
|
||||
userEmail: this.state.currentUser.email,
|
||||
teaTypes: this.state.teaTypes,
|
||||
consumption: this.state.consumption
|
||||
};
|
||||
var json = JSON.stringify(data, null, 2);
|
||||
var blob = new Blob([json], { type: 'application/json' });
|
||||
var url = URL.createObjectURL(blob);
|
||||
var a = document.createElement('a');
|
||||
var dateStr = new Date().toISOString().split('T')[0];
|
||||
a.href = url;
|
||||
a.download = 'teetracker_backup_' + dateStr + '.json';
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
var status = document.getElementById('backupStatus');
|
||||
if (status) {
|
||||
status.textContent = 'Backup heruntergeladen: ' + this.state.consumption.length + ' Einträge, ' + this.state.teaTypes.length + ' Teesorten';
|
||||
}
|
||||
this.showToast('Backup erfolgreich heruntergeladen!', 'success');
|
||||
},
|
||||
|
||||
// Import: Daten aus JSON-Datei wiederherstellen
|
||||
importData(file) {
|
||||
if (!this.state.currentUser) {
|
||||
this.showToast('Bitte melden Sie sich zuerst an', 'error');
|
||||
return;
|
||||
}
|
||||
var self = this;
|
||||
var reader = new FileReader();
|
||||
reader.onload = function(e) {
|
||||
try {
|
||||
var data = JSON.parse(e.target.result);
|
||||
if (!data.teaTypes || !data.consumption) {
|
||||
throw new Error('Ungültiges Backup-Format');
|
||||
}
|
||||
if (!confirm('Backup importieren? ' + data.consumption.length + ' Einträge und ' + data.teaTypes.length + ' Teesorten werden in Firebase geschrieben. Bestehende Daten bleiben erhalten.')) {
|
||||
return;
|
||||
}
|
||||
var batch = db.batch();
|
||||
var teaTypesRef = db.collection('users/' + self.state.currentUser.uid + '/teaTypes');
|
||||
var consumptionRef = db.collection('users/' + self.state.currentUser.uid + '/consumption');
|
||||
|
||||
// Teesorten importieren
|
||||
data.teaTypes.forEach(function(tea) {
|
||||
var ref = teaTypesRef.doc();
|
||||
batch.set(ref, {
|
||||
name: tea.name,
|
||||
color: tea.color || '#4CAF50',
|
||||
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
|
||||
importedFrom: data.exportDate
|
||||
});
|
||||
});
|
||||
|
||||
// Konsumdaten importieren
|
||||
data.consumption.forEach(function(c) {
|
||||
var ref = consumptionRef.doc();
|
||||
batch.set(ref, {
|
||||
date: c.date,
|
||||
teaName: c.teaName,
|
||||
teaColor: c.teaColor || '#CCCCCC',
|
||||
brewTime: c.brewTime || 'normal',
|
||||
amount: c.amount || 1,
|
||||
notes: c.notes || '',
|
||||
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
|
||||
importedFrom: data.exportDate
|
||||
});
|
||||
});
|
||||
|
||||
batch.commit().then(function() {
|
||||
var status = document.getElementById('backupStatus');
|
||||
if (status) {
|
||||
status.textContent = 'Import erfolgreich: ' + data.consumption.length + ' Einträge importiert';
|
||||
}
|
||||
self.showToast('Backup erfolgreich importiert!', 'success');
|
||||
}).catch(function(error) {
|
||||
console.error('Import-Fehler:', error);
|
||||
self.showToast('Fehler beim Import: ' + error.message, 'error');
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Backup-Parsing-Fehler:', error);
|
||||
self.showToast('Ungültige Backup-Datei: ' + error.message, 'error');
|
||||
}
|
||||
};
|
||||
reader.readAsText(file);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
907
script_backup.js
Normal file
907
script_backup.js
Normal file
@ -0,0 +1,907 @@
|
||||
// TeeTracker Application mit Firebase
|
||||
// ======================================
|
||||
|
||||
const App = {
|
||||
// Initialer Zustand
|
||||
state: {
|
||||
teaTypes: [],
|
||||
consumption: [],
|
||||
currentFilter: 'all',
|
||||
currentUser: null,
|
||||
loading: true
|
||||
},
|
||||
|
||||
// Initialisierung
|
||||
init() {
|
||||
this.bindEvents();
|
||||
this.setupFirebaseAuth();
|
||||
this.updateDate();
|
||||
},
|
||||
|
||||
// Firebase Auth einrichten
|
||||
setupFirebaseAuth() {
|
||||
firebase.auth().onAuthStateChanged((user) => {
|
||||
if (user) {
|
||||
this.state.currentUser = user;
|
||||
this.loadUserData();
|
||||
this.hideAuthSection();
|
||||
this.showSignedInUI();
|
||||
} else {
|
||||
this.state.currentUser = null;
|
||||
this.state.teaTypes = [];
|
||||
this.state.consumption = [];
|
||||
this.showAuthSection();
|
||||
this.showSignedOutUI();
|
||||
this.renderAll();
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Benutzerdaten laden
|
||||
async loadUserData() {
|
||||
if (!this.state.currentUser) return;
|
||||
|
||||
try {
|
||||
this.state.loading = true;
|
||||
|
||||
// Teesorten laden
|
||||
const teaTypesSnapshot = await db
|
||||
.collection(`users/${this.state.currentUser.uid}/teaTypes`)
|
||||
.orderBy('name')
|
||||
.get();
|
||||
|
||||
this.state.teaTypes = teaTypesSnapshot.docs.map(doc => ({
|
||||
id: doc.id,
|
||||
...doc.data()
|
||||
}));
|
||||
|
||||
// Konsumdaten laden
|
||||
const consumptionSnapshot = await db
|
||||
.collection(`users/${this.state.currentUser.uid}/consumption`)
|
||||
.orderBy('date', 'desc')
|
||||
.get();
|
||||
|
||||
this.state.consumption = consumptionSnapshot.docs.map(doc => ({
|
||||
id: doc.id,
|
||||
...doc.data()
|
||||
}));
|
||||
|
||||
this.state.loading = false;
|
||||
this.renderAll();
|
||||
|
||||
// Echtzeit-Updates abonnieren
|
||||
this.setupRealtimeUpdates();
|
||||
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Laden der Daten:', error);
|
||||
this.state.loading = false;
|
||||
this.showToast('Fehler beim Laden der Daten', 'error');
|
||||
}
|
||||
},
|
||||
|
||||
// Echtzeit-Updates einrichten
|
||||
setupRealtimeUpdates() {
|
||||
if (!this.state.currentUser) return;
|
||||
|
||||
// Teesorten-Updates
|
||||
db.collection(`users/${this.state.currentUser.uid}/teaTypes`)
|
||||
.orderBy('name')
|
||||
.onSnapshot((snapshot) => {
|
||||
this.state.teaTypes = snapshot.docs.map(doc => ({
|
||||
id: doc.id,
|
||||
...doc.data()
|
||||
}));
|
||||
this.renderTeaTypes();
|
||||
this.renderTrackForm();
|
||||
});
|
||||
|
||||
// Konsum-Updates
|
||||
db.collection(`users/${this.state.currentUser.uid}/consumption`)
|
||||
.orderBy('date', 'desc')
|
||||
.onSnapshot((snapshot) => {
|
||||
this.state.consumption = snapshot.docs.map(doc => ({
|
||||
id: doc.id,
|
||||
...doc.data()
|
||||
}));
|
||||
this.renderHistory();
|
||||
this.renderStats();
|
||||
this.renderCharts();
|
||||
});
|
||||
},
|
||||
|
||||
// Events binden
|
||||
bindEvents() {
|
||||
// Google Sign-In
|
||||
document.getElementById('googleSignIn')?.addEventListener('click', () => {
|
||||
this.signInWithGoogle();
|
||||
});
|
||||
|
||||
// Sign Out
|
||||
document.getElementById('signOut')?.addEventListener('click', () => {
|
||||
this.signOut();
|
||||
});
|
||||
|
||||
// Formular für neue Teesorte
|
||||
document.getElementById('addTeaForm')?.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
this.addTeaType();
|
||||
});
|
||||
|
||||
// Formular für Tracking
|
||||
document.getElementById('trackForm')?.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
this.trackConsumption();
|
||||
});
|
||||
|
||||
// Filter-Buttons
|
||||
document.getElementById('filterToday')?.addEventListener('click', () => {
|
||||
this.setFilter('today');
|
||||
});
|
||||
|
||||
document.getElementById('filterWeek')?.addEventListener('click', () => {
|
||||
this.setFilter('week');
|
||||
});
|
||||
|
||||
document.getElementById('filterMonth')?.addEventListener('click', () => {
|
||||
this.setFilter('month');
|
||||
});
|
||||
|
||||
document.getElementById('filterAll')?.addEventListener('click', () => {
|
||||
this.setFilter('all');
|
||||
});
|
||||
|
||||
// Datum auf heute setzen
|
||||
document.getElementById('trackDate')?.setAttribute('value', new Date().toISOString().split('T')[0]);
|
||||
},
|
||||
|
||||
// Google Sign-In
|
||||
signInWithGoogle() {
|
||||
const provider = new firebase.auth.GoogleAuthProvider();
|
||||
firebase.auth().signInWithPopup(provider)
|
||||
.then((result) => {
|
||||
this.showToast('Erfolgreich angemeldet!', 'success');
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Anmeldung fehlgeschlagen:', error);
|
||||
this.showToast('Anmeldung fehlgeschlagen: ' + error.message, 'error');
|
||||
});
|
||||
},
|
||||
|
||||
// Sign Out
|
||||
signOut() {
|
||||
firebase.auth().signOut()
|
||||
.then(() => {
|
||||
this.showToast('Erfolgreich abgemeldet', 'success');
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Abmeldung fehlgeschlagen:', error);
|
||||
this.showToast('Abmeldung fehlgeschlagen', 'error');
|
||||
});
|
||||
},
|
||||
|
||||
// Auth-Bereich verstecken
|
||||
hideAuthSection() {
|
||||
const authSection = document.getElementById('auth-section');
|
||||
if (authSection) {
|
||||
authSection.classList.add('hidden');
|
||||
}
|
||||
},
|
||||
|
||||
// Auth-Bereich anzeigen
|
||||
showAuthSection() {
|
||||
const authSection = document.getElementById('auth-section');
|
||||
if (authSection) {
|
||||
authSection.classList.remove('hidden');
|
||||
}
|
||||
},
|
||||
|
||||
// Angemeldete UI anzeigen
|
||||
showSignedInUI() {
|
||||
const signInBtn = document.getElementById('googleSignIn');
|
||||
const signOutBtn = document.getElementById('signOut');
|
||||
const authStatus = document.getElementById('auth-status');
|
||||
|
||||
if (signInBtn) signInBtn.classList.add('hidden');
|
||||
if (signOutBtn) signOutBtn.classList.remove('hidden');
|
||||
if (authStatus) {
|
||||
authStatus.innerHTML = `<span style="color: #4CAF50">Angemeldet als: ${this.state.currentUser?.displayName || this.state.currentUser?.email}</span>`;
|
||||
}
|
||||
},
|
||||
|
||||
// Abgemeldete UI anzeigen
|
||||
showSignedOutUI() {
|
||||
const signInBtn = document.getElementById('googleSignIn');
|
||||
const signOutBtn = document.getElementById('signOut');
|
||||
const authStatus = document.getElementById('auth-status');
|
||||
|
||||
if (signInBtn) signInBtn.classList.remove('hidden');
|
||||
if (signOutBtn) signOutBtn.classList.add('hidden');
|
||||
if (authStatus) {
|
||||
authStatus.innerHTML = '<span style="color: #F44336">Nicht angemeldet</span>';
|
||||
}
|
||||
},
|
||||
|
||||
// Filter setzen
|
||||
setFilter(filter) {
|
||||
this.state.currentFilter = filter;
|
||||
this.renderHistory();
|
||||
this.updateFilterButtons();
|
||||
},
|
||||
|
||||
// Filter-Buttons aktualisieren
|
||||
updateFilterButtons() {
|
||||
const buttons = [
|
||||
{ id: 'filterToday', filter: 'today' },
|
||||
{ id: 'filterWeek', filter: 'week' },
|
||||
{ id: 'filterMonth', filter: 'month' },
|
||||
{ id: 'filterAll', filter: 'all' }
|
||||
];
|
||||
|
||||
buttons.forEach(btn => {
|
||||
const element = document.getElementById(btn.id);
|
||||
if (element) {
|
||||
if (this.state.currentFilter === btn.filter) {
|
||||
element.classList.add('active');
|
||||
} else {
|
||||
element.classList.remove('active');
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Neue Teesorte hinzufügen
|
||||
async addTeaType() {
|
||||
if (!this.state.currentUser) {
|
||||
this.showToast('Bitte melden Sie sich zuerst an', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
const nameInput = document.getElementById('teaName');
|
||||
const colorInput = document.getElementById('teaColor');
|
||||
|
||||
const name = nameInput?.value?.trim();
|
||||
const color = colorInput?.value || '#4CAF50';
|
||||
|
||||
if (!name) {
|
||||
this.showToast('Bitte geben Sie einen Namen für die Teesorte ein', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
// Prüfen, ob die Sorte bereits existiert
|
||||
const exists = this.state.teaTypes.some(t => t.name.toLowerCase() === name.toLowerCase());
|
||||
if (exists) {
|
||||
this.showToast('Diese Teesorte existiert bereits', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const docRef = await db
|
||||
.collection(`users/${this.state.currentUser.uid}/teaTypes`)
|
||||
.add({
|
||||
name: name,
|
||||
color: color,
|
||||
createdAt: firebase.firestore.FieldValue.serverTimestamp()
|
||||
});
|
||||
|
||||
nameInput.value = '';
|
||||
colorInput.value = '#4CAF50';
|
||||
this.showToast('Teesorte erfolgreich hinzugefügt', 'success');
|
||||
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Hinzufügen der Teesorte:', error);
|
||||
this.showToast('Fehler beim Hinzufügen der Teesorte', 'error');
|
||||
}
|
||||
},
|
||||
|
||||
// Teesorte entfernen
|
||||
async removeTeaType(teaId) {
|
||||
if (!this.state.currentUser) {
|
||||
this.showToast('Bitte melden Sie sich zuerst an', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
const teaType = this.state.teaTypes.find(t => t.id === teaId);
|
||||
if (!teaType) return;
|
||||
|
||||
// Prüfen, ob die Sorte in Konsumdaten verwendet wird
|
||||
const isUsed = this.state.consumption.some(c => c.teaId === teaId);
|
||||
|
||||
if (isUsed) {
|
||||
if (!confirm('Diese Teesorte wird in Konsumdaten verwendet. Möchten Sie sie trotzdem entfernen?')) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// Konsumdaten bereinigen
|
||||
const consumptionRefs = await db
|
||||
.collection(`users/${this.state.currentUser.uid}/consumption`)
|
||||
.where('teaId', '==', teaId)
|
||||
.get();
|
||||
|
||||
const batch = db.batch();
|
||||
consumptionRefs.forEach(doc => {
|
||||
batch.update(doc.ref, {
|
||||
teaName: '[Gelöschte Sorte]',
|
||||
teaId: null
|
||||
});
|
||||
});
|
||||
|
||||
// Teesorte löschen
|
||||
const teaRef = db.collection(`users/${this.state.currentUser.uid}/teaTypes`).doc(teaId);
|
||||
batch.delete(teaRef);
|
||||
|
||||
await batch.commit();
|
||||
this.showToast('Teesorte entfernt', 'success');
|
||||
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Entfernen der Teesorte:', error);
|
||||
this.showToast('Fehler beim Entfernen der Teesorte', 'error');
|
||||
}
|
||||
},
|
||||
|
||||
// Konsum tracken
|
||||
async trackConsumption() {
|
||||
if (!this.state.currentUser) {
|
||||
this.showToast('Bitte melden Sie sich zuerst an', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
const dateInput = document.getElementById('trackDate');
|
||||
const teaSelect = document.getElementById('trackTea');
|
||||
const amountInput = document.getElementById('trackAmount');
|
||||
const notesInput = document.getElementById('trackNotes');
|
||||
|
||||
const date = dateInput?.value;
|
||||
const teaId = teaSelect?.value;
|
||||
const amount = parseInt(amountInput?.value) || 1;
|
||||
const notes = notesInput?.value?.trim() || '';
|
||||
|
||||
// Brühzeit
|
||||
const brewTimeRadio = document.querySelector('input[name="brewTime"]:checked');
|
||||
const brewTime = brewTimeRadio?.value || 'normal';
|
||||
|
||||
if (!date) {
|
||||
this.showToast('Bitte wählen Sie ein Datum aus', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!teaId) {
|
||||
this.showToast('Bitte wählen Sie eine Teesorte aus', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
const teaType = this.state.teaTypes.find(t => t.id === teaId);
|
||||
if (!teaType) {
|
||||
this.showToast('Ungültige Teesorte', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await db
|
||||
.collection(`users/${this.state.currentUser.uid}/consumption`)
|
||||
.add({
|
||||
date: date,
|
||||
teaId: teaId,
|
||||
teaName: teaType.name,
|
||||
teaColor: teaType.color,
|
||||
brewTime: brewTime,
|
||||
amount: amount,
|
||||
notes: notes,
|
||||
createdAt: firebase.firestore.FieldValue.serverTimestamp()
|
||||
});
|
||||
|
||||
// Formular zurücksetzen
|
||||
dateInput.valueAsDate = new Date();
|
||||
teaSelect.selectedIndex = 0;
|
||||
amountInput.value = 1;
|
||||
notesInput.value = '';
|
||||
brewTimeRadio.checked = true;
|
||||
|
||||
this.showToast('Tee-Konsum erfolgreich getrackt!', 'success');
|
||||
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Tracken:', error);
|
||||
this.showToast('Fehler beim Tracken', 'error');
|
||||
}
|
||||
},
|
||||
|
||||
// Konsum-Eintrag entfernen
|
||||
async removeConsumption(entryId) {
|
||||
if (!this.state.currentUser) {
|
||||
this.showToast('Bitte melden Sie sich zuerst an', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!confirm('Möchten Sie diesen Eintrag wirklich entfernen?')) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await db
|
||||
.collection(`users/${this.state.currentUser.uid}/consumption`)
|
||||
.doc(entryId)
|
||||
.delete();
|
||||
|
||||
this.showToast('Eintrag entfernt', 'success');
|
||||
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Entfernen des Eintrags:', error);
|
||||
this.showToast('Fehler beim Entfernen des Eintrags', 'error');
|
||||
}
|
||||
},
|
||||
|
||||
// Alle Render-Funktionen
|
||||
renderAll() {
|
||||
this.renderTeaTypes();
|
||||
this.renderTrackForm();
|
||||
this.renderHistory();
|
||||
this.renderStats();
|
||||
this.renderCharts();
|
||||
},
|
||||
|
||||
// Teetypen rendern
|
||||
renderTeaTypes() {
|
||||
const teaList = document.getElementById('teaList');
|
||||
|
||||
if (!teaList) return;
|
||||
|
||||
if (this.state.loading) {
|
||||
teaList.innerHTML = '<div class="empty-state"><div class="icon">⏳</div><p>Daten werden geladen...</p></div>';
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.state.teaTypes.length === 0 && this.state.currentUser) {
|
||||
teaList.innerHTML = `
|
||||
<div class="empty-state">
|
||||
<div class="icon">🍃</div>
|
||||
<p>Keine Teesorten hinterlegt</p>
|
||||
<p>Fügen Sie oben eine neue Teesorte hinzu</p>
|
||||
</div>
|
||||
`;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.state.currentUser) {
|
||||
teaList.innerHTML = `
|
||||
<div class="empty-state">
|
||||
<div class="icon">🔒</div>
|
||||
<p>Bitte melden Sie sich an, um Ihre Teesorten zu sehen</p>
|
||||
</div>
|
||||
`;
|
||||
return;
|
||||
}
|
||||
|
||||
teaList.innerHTML = this.state.teaTypes.map(tea => `
|
||||
<div class="tea-item" data-tea-id="${tea.id}">
|
||||
<div class="tea-info">
|
||||
<div class="tea-color" style="background-color: ${tea.color}"></div>
|
||||
<span class="tea-name">${this.escapeHtml(tea.name)}</span>
|
||||
</div>
|
||||
<div class="tea-actions">
|
||||
<button class="btn btn-danger btn-icon" onclick="App.removeTeaType('${tea.id}')"
|
||||
title="Entfernen">×</button>
|
||||
</div>
|
||||
</div>
|
||||
`).join('');
|
||||
},
|
||||
|
||||
// Track-Formular rendern
|
||||
renderTrackForm() {
|
||||
const teaSelect = document.getElementById('trackTea');
|
||||
|
||||
if (!teaSelect) return;
|
||||
|
||||
if (!this.state.currentUser) {
|
||||
teaSelect.innerHTML = '<option value="" selected disabled>Bitte melden Sie sich an</option>';
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.state.teaTypes.length === 0) {
|
||||
teaSelect.innerHTML = '<option value="" selected disabled>Keine Teesorten verfügbar</option>';
|
||||
return;
|
||||
}
|
||||
|
||||
const options = this.state.teaTypes.map(tea =>
|
||||
`<option value="${tea.id}" style="color: ${tea.color}">🎨 ${this.escapeHtml(tea.name)}</option>`
|
||||
);
|
||||
|
||||
// Standard-Option
|
||||
options.unshift('<option value="" selected disabled>Teesorte auswählen</option>');
|
||||
|
||||
teaSelect.innerHTML = options.join('');
|
||||
},
|
||||
|
||||
// Konsum-Historie rendern
|
||||
renderHistory() {
|
||||
const historyList = document.getElementById('historyList');
|
||||
|
||||
if (!historyList) return;
|
||||
|
||||
if (!this.state.currentUser) {
|
||||
historyList.innerHTML = `
|
||||
<div class="empty-state">
|
||||
<div class="icon">🔒</div>
|
||||
<p>Bitte melden Sie sich an, um Ihre Historie zu sehen</p>
|
||||
</div>
|
||||
`;
|
||||
return;
|
||||
}
|
||||
|
||||
let filteredConsumption = [...this.state.consumption];
|
||||
|
||||
// Filter anwenden
|
||||
const today = new Date().toISOString().split('T')[0];
|
||||
const weekAgo = new Date();
|
||||
weekAgo.setDate(weekAgo.getDate() - 7);
|
||||
const weekAgoStr = weekAgo.toISOString().split('T')[0];
|
||||
const monthAgo = new Date();
|
||||
monthAgo.setMonth(monthAgo.getMonth() - 1);
|
||||
const monthAgoStr = monthAgo.toISOString().split('T')[0];
|
||||
|
||||
switch (this.state.currentFilter) {
|
||||
case 'today':
|
||||
filteredConsumption = filteredConsumption.filter(c => c.date === today);
|
||||
break;
|
||||
case 'week':
|
||||
filteredConsumption = filteredConsumption.filter(c => c.date >= weekAgoStr);
|
||||
break;
|
||||
case 'month':
|
||||
filteredConsumption = filteredConsumption.filter(c => c.date >= monthAgoStr);
|
||||
break;
|
||||
}
|
||||
|
||||
// Sortieren nach Datum (neueste zuerst)
|
||||
filteredConsumption.sort((a, b) => new Date(b.date) - new Date(a.date));
|
||||
|
||||
if (filteredConsumption.length === 0) {
|
||||
historyList.innerHTML = `
|
||||
<div class="empty-state">
|
||||
<div class="icon">☕</div>
|
||||
<p>Keine Einträge gefunden</p>
|
||||
<p>Tracken Sie Ihren ersten Tee!</p>
|
||||
</div>
|
||||
`;
|
||||
return;
|
||||
}
|
||||
|
||||
historyList.innerHTML = filteredConsumption.map(entry => {
|
||||
const date = new Date(entry.date);
|
||||
const dateStr = date.toLocaleDateString('de-DE', {
|
||||
weekday: 'short',
|
||||
day: '2-digit',
|
||||
month: '2-digit',
|
||||
year: 'numeric'
|
||||
});
|
||||
|
||||
const brewTimeLabel = this.getBrewTimeLabel(entry.brewTime);
|
||||
const brewTimeClass = entry.brewTime;
|
||||
|
||||
return `
|
||||
<div class="history-entry" data-entry-id="${entry.id}">
|
||||
<div class="entry-info">
|
||||
<div class="entry-date">${dateStr}</div>
|
||||
<div class="entry-details">
|
||||
<span class="entry-amount">${entry.amount}x ${this.escapeHtml(entry.teaName)}</span>
|
||||
<span class="brew-time-badge ${brewTimeClass}">${brewTimeLabel}</span>
|
||||
${entry.notes ? `<span class="entry-notes">📝 ${this.escapeHtml(entry.notes)}</span>` : ''}
|
||||
</div>
|
||||
</div>
|
||||
<div class="entry-actions">
|
||||
<button class="btn btn-danger btn-icon" onclick="App.removeConsumption('${entry.id}')"
|
||||
title="Entfernen">×</button>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}).join('');
|
||||
},
|
||||
|
||||
// Statistiken rendern
|
||||
renderStats() {
|
||||
if (!this.state.currentUser) {
|
||||
document.getElementById('today-count')?.textContent = '?';
|
||||
document.getElementById('month-count')?.textContent = '?';
|
||||
document.getElementById('year-count')?.textContent = '?';
|
||||
document.getElementById('favorite-tea')?.textContent = '?';
|
||||
document.getElementById('favorite-count')?.textContent = '?';
|
||||
return;
|
||||
}
|
||||
|
||||
const today = new Date().toISOString().split('T')[0];
|
||||
const currentMonth = new Date().getMonth();
|
||||
const currentYear = new Date().getFullYear();
|
||||
|
||||
// Heute
|
||||
const todayConsumption = this.state.consumption.filter(c => c.date === today);
|
||||
const todayCount = todayConsumption.reduce((sum, c) => sum + c.amount, 0);
|
||||
document.getElementById('today-count')?.textContent = todayCount;
|
||||
|
||||
// Dieser Monat
|
||||
const monthConsumption = this.state.consumption.filter(c => {
|
||||
const entryDate = new Date(c.date);
|
||||
return entryDate.getMonth() === currentMonth && entryDate.getFullYear() === currentYear;
|
||||
});
|
||||
const monthCount = monthConsumption.reduce((sum, c) => sum + c.amount, 0);
|
||||
document.getElementById('month-count')?.textContent = monthCount;
|
||||
|
||||
// Dieses Jahr
|
||||
const yearConsumption = this.state.consumption.filter(c => {
|
||||
const entryDate = new Date(c.date);
|
||||
return entryDate.getFullYear() === currentYear;
|
||||
});
|
||||
const yearCount = yearConsumption.reduce((sum, c) => sum + c.amount, 0);
|
||||
document.getElementById('year-count')?.textContent = yearCount;
|
||||
|
||||
// Lieblingssorte (meist konsumiert in diesem Jahr)
|
||||
const yearTeaCounts = {};
|
||||
yearConsumption.forEach(c => {
|
||||
const teaName = c.teaName || 'Unbekannt';
|
||||
yearTeaCounts[teaName] = (yearTeaCounts[teaName] || 0) + c.amount;
|
||||
});
|
||||
|
||||
const sortedTeas = Object.entries(yearTeaCounts).sort((a, b) => b[1] - a[1]);
|
||||
if (sortedTeas.length > 0) {
|
||||
document.getElementById('favorite-tea')?.textContent = this.escapeHtml(sortedTeas[0][0]);
|
||||
document.getElementById('favorite-count')?.textContent = sortedTeas[0][1];
|
||||
} else {
|
||||
document.getElementById('favorite-tea')?.textContent = '-';
|
||||
document.getElementById('favorite-count')?.textContent = '0';
|
||||
}
|
||||
},
|
||||
|
||||
// Charts rendern
|
||||
renderCharts() {
|
||||
if (!this.state.currentUser) {
|
||||
// Charts leeren
|
||||
this.clearCharts();
|
||||
return;
|
||||
}
|
||||
this.renderTodayChart();
|
||||
this.renderMonthChart();
|
||||
this.renderYearChart();
|
||||
},
|
||||
|
||||
// Charts leeren
|
||||
clearCharts() {
|
||||
['todayChart', 'monthChart', 'yearChart'].forEach(chartId => {
|
||||
const ctx = document.getElementById(chartId);
|
||||
if (ctx && this[chartId]) {
|
||||
this[chartId].destroy();
|
||||
this[chartId] = null;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Heute-Chart
|
||||
renderTodayChart() {
|
||||
const today = new Date().toISOString().split('T')[0];
|
||||
const todayConsumption = this.state.consumption.filter(c => c.date === today);
|
||||
|
||||
// Nach Teesorte gruppieren
|
||||
const teaCounts = {};
|
||||
const teaColors = {};
|
||||
|
||||
todayConsumption.forEach(c => {
|
||||
const teaName = c.teaName || 'Unbekannt';
|
||||
teaCounts[teaName] = (teaCounts[teaName] || 0) + c.amount;
|
||||
teaColors[teaName] = c.teaColor || '#CCCCCC';
|
||||
});
|
||||
|
||||
const teaNames = Object.keys(teaCounts);
|
||||
const counts = Object.values(teaCounts);
|
||||
const colors = teaNames.map(name => teaColors[name]);
|
||||
|
||||
const ctx = document.getElementById('todayChart')?.getContext('2d');
|
||||
|
||||
if (!ctx) return;
|
||||
|
||||
// Zerstören Sie das vorherige Chart, falls vorhanden
|
||||
if (this.todayChart) {
|
||||
this.todayChart.destroy();
|
||||
}
|
||||
|
||||
this.todayChart = new Chart(ctx, {
|
||||
type: 'doughnut',
|
||||
data: {
|
||||
labels: teaNames,
|
||||
datasets: [{
|
||||
data: counts,
|
||||
backgroundColor: colors,
|
||||
borderWidth: 1
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: true,
|
||||
plugins: {
|
||||
legend: {
|
||||
position: 'bottom',
|
||||
labels: {
|
||||
padding: 10,
|
||||
font: {
|
||||
size: 11
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Monat-Chart
|
||||
renderMonthChart() {
|
||||
const currentMonth = new Date().getMonth();
|
||||
const currentYear = new Date().getFullYear();
|
||||
|
||||
const monthConsumption = this.state.consumption.filter(c => {
|
||||
const entryDate = new Date(c.date);
|
||||
return entryDate.getMonth() === currentMonth && entryDate.getFullYear() === currentYear;
|
||||
});
|
||||
|
||||
// Nach Tag gruppieren
|
||||
const dayCounts = {};
|
||||
monthConsumption.forEach(c => {
|
||||
const day = new Date(c.date).getDate();
|
||||
dayCounts[day] = (dayCounts[day] || 0) + c.amount;
|
||||
});
|
||||
|
||||
// Alle Tage des Monats
|
||||
const daysInMonth = new Date(currentYear, currentMonth + 1, 0).getDate();
|
||||
const labels = Array.from({ length: daysInMonth }, (_, i) => (i + 1).toString());
|
||||
const data = labels.map(day => dayCounts[parseInt(day)] || 0);
|
||||
|
||||
const ctx = document.getElementById('monthChart')?.getContext('2d');
|
||||
|
||||
if (!ctx) return;
|
||||
|
||||
if (this.monthChart) {
|
||||
this.monthChart.destroy();
|
||||
}
|
||||
|
||||
this.monthChart = new Chart(ctx, {
|
||||
type: 'bar',
|
||||
data: {
|
||||
labels: labels,
|
||||
datasets: [{
|
||||
label: 'Tassen pro Tag',
|
||||
data: data,
|
||||
backgroundColor: 'rgba(76, 175, 80, 0.7)',
|
||||
borderColor: 'rgba(76, 175, 80, 1)',
|
||||
borderWidth: 1
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: true,
|
||||
plugins: {
|
||||
legend: {
|
||||
display: false
|
||||
}
|
||||
},
|
||||
scales: {
|
||||
y: {
|
||||
beginAtZero: true,
|
||||
ticks: {
|
||||
stepSize: 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Jahr-Chart
|
||||
renderYearChart() {
|
||||
const currentYear = new Date().getFullYear();
|
||||
|
||||
const yearConsumption = this.state.consumption.filter(c => {
|
||||
const entryDate = new Date(c.date);
|
||||
return entryDate.getFullYear() === currentYear;
|
||||
});
|
||||
|
||||
// Nach Teesorte gruppieren
|
||||
const teaCounts = {};
|
||||
const teaColors = {};
|
||||
|
||||
yearConsumption.forEach(c => {
|
||||
const teaName = c.teaName || 'Unbekannt';
|
||||
teaCounts[teaName] = (teaCounts[teaName] || 0) + c.amount;
|
||||
teaColors[teaName] = c.teaColor || '#CCCCCC';
|
||||
});
|
||||
|
||||
const teaNames = Object.keys(teaCounts);
|
||||
const counts = Object.values(teaCounts);
|
||||
const colors = teaNames.map(name => teaColors[name]);
|
||||
|
||||
const ctx = document.getElementById('yearChart')?.getContext('2d');
|
||||
|
||||
if (!ctx) return;
|
||||
|
||||
if (this.yearChart) {
|
||||
this.yearChart.destroy();
|
||||
}
|
||||
|
||||
this.yearChart = new Chart(ctx, {
|
||||
type: 'bar',
|
||||
data: {
|
||||
labels: teaNames,
|
||||
datasets: [{
|
||||
label: 'Tassen',
|
||||
data: counts,
|
||||
backgroundColor: colors,
|
||||
borderWidth: 1
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
responsive: true,
|
||||
maintainAspectRatio: true,
|
||||
indexAxis: 'y',
|
||||
plugins: {
|
||||
legend: {
|
||||
display: false
|
||||
}
|
||||
},
|
||||
scales: {
|
||||
x: {
|
||||
beginAtZero: true,
|
||||
ticks: {
|
||||
stepSize: 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Brühzeit-Label
|
||||
getBrewTimeLabel(brewTime) {
|
||||
const labels = {
|
||||
'kurz': 'Kurz',
|
||||
'normal': 'Normal',
|
||||
'lang': 'Lang'
|
||||
};
|
||||
return labels[brewTime] || brewTime;
|
||||
},
|
||||
|
||||
// HTML escape
|
||||
escapeHtml(text) {
|
||||
if (!text) return '';
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
},
|
||||
|
||||
// Toast-Nachricht anzeigen
|
||||
showToast(message, type = 'success') {
|
||||
// Entferne bestehende Toasts
|
||||
const existingToast = document.querySelector('.toast');
|
||||
if (existingToast) {
|
||||
existingToast.remove();
|
||||
}
|
||||
|
||||
const toast = document.createElement('div');
|
||||
toast.className = `toast ${type}`;
|
||||
toast.textContent = message;
|
||||
document.body.appendChild(toast);
|
||||
|
||||
setTimeout(() => {
|
||||
toast.style.animation = 'slideIn 0.3s ease-out reverse';
|
||||
setTimeout(() => {
|
||||
toast.remove();
|
||||
}, 300);
|
||||
}, 3000);
|
||||
},
|
||||
|
||||
// Datum aktualisieren (Fußzeile)
|
||||
updateDate() {
|
||||
const yearSpan = document.getElementById('current-year');
|
||||
if (yearSpan) {
|
||||
yearSpan.textContent = new Date().getFullYear();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Initialisierung beim Laden der Seite
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
App.init();
|
||||
});
|
||||
18
styles.css
18
styles.css
@ -655,3 +655,21 @@ section h2 {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
/* Backup Section */
|
||||
.backup-section {
|
||||
background: var(--card-background);
|
||||
}
|
||||
|
||||
.backup-controls {
|
||||
display: flex;
|
||||
gap: 15px;
|
||||
align-items: center;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.backup-status {
|
||||
font-size: 0.9rem;
|
||||
color: var(--text-secondary);
|
||||
margin-left: 10px;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user