- Alle ?.-Operatoren durch klassische Null-Checks ersetzt - ES5/ES6 kompatibel - Funktioniert in allen modernen Browsern Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
974 lines
33 KiB
JavaScript
974 lines
33 KiB
JavaScript
// TeeTracker Application mit Firebase (Browser-kompatibel)
|
|
// =====================================================
|
|
|
|
var App = {
|
|
// Initialer Zustand
|
|
state: {
|
|
teaTypes: [],
|
|
consumption: [],
|
|
currentFilter: 'all',
|
|
currentUser: null,
|
|
loading: true
|
|
},
|
|
|
|
// Initialisierung
|
|
init: function() {
|
|
this.bindEvents();
|
|
this.setupFirebaseAuth();
|
|
this.updateDate();
|
|
},
|
|
|
|
// Firebase Auth einrichten
|
|
setupFirebaseAuth: function() {
|
|
var self = this;
|
|
firebase.auth().onAuthStateChanged(function(user) {
|
|
if (user) {
|
|
self.state.currentUser = user;
|
|
self.loadUserData();
|
|
self.hideAuthSection();
|
|
self.showSignedInUI();
|
|
} else {
|
|
self.state.currentUser = null;
|
|
self.state.teaTypes = [];
|
|
self.state.consumption = [];
|
|
self.showAuthSection();
|
|
self.showSignedOutUI();
|
|
self.renderAll();
|
|
}
|
|
});
|
|
},
|
|
|
|
// Benutzerdaten laden
|
|
loadUserData: function() {
|
|
var self = this;
|
|
if (!this.state.currentUser) return;
|
|
|
|
this.state.loading = true;
|
|
|
|
// Teesorten laden
|
|
db.collection('users/' + this.state.currentUser.uid + '/teaTypes')
|
|
.orderBy('name')
|
|
.get()
|
|
.then(function(snapshot) {
|
|
self.state.teaTypes = snapshot.docs.map(function(doc) {
|
|
return { id: doc.id, name: doc.data().name, color: doc.data().color };
|
|
});
|
|
return db.collection('users/' + self.state.currentUser.uid + '/consumption')
|
|
.orderBy('date', 'desc')
|
|
.get();
|
|
})
|
|
.then(function(snapshot) {
|
|
self.state.consumption = snapshot.docs.map(function(doc) {
|
|
return {
|
|
id: doc.id,
|
|
date: doc.data().date,
|
|
teaId: doc.data().teaId,
|
|
teaName: doc.data().teaName,
|
|
teaColor: doc.data().teaColor,
|
|
brewTime: doc.data().brewTime,
|
|
amount: doc.data().amount,
|
|
notes: doc.data().notes
|
|
};
|
|
});
|
|
self.state.loading = false;
|
|
self.renderAll();
|
|
self.setupRealtimeUpdates();
|
|
})
|
|
.catch(function(error) {
|
|
console.error('Fehler beim Laden der Daten:', error);
|
|
self.state.loading = false;
|
|
self.showToast('Fehler beim Laden der Daten', 'error');
|
|
});
|
|
},
|
|
|
|
// Echtzeit-Updates einrichten
|
|
setupRealtimeUpdates: function() {
|
|
var self = this;
|
|
if (!this.state.currentUser) return;
|
|
|
|
// Teesorten-Updates
|
|
db.collection('users/' + this.state.currentUser.uid + '/teaTypes')
|
|
.orderBy('name')
|
|
.onSnapshot(function(snapshot) {
|
|
self.state.teaTypes = snapshot.docs.map(function(doc) {
|
|
return { id: doc.id, name: doc.data().name, color: doc.data().color };
|
|
});
|
|
self.renderTeaTypes();
|
|
self.renderTrackForm();
|
|
});
|
|
|
|
// Konsum-Updates
|
|
db.collection('users/' + this.state.currentUser.uid + '/consumption')
|
|
.orderBy('date', 'desc')
|
|
.onSnapshot(function(snapshot) {
|
|
self.state.consumption = snapshot.docs.map(function(doc) {
|
|
return {
|
|
id: doc.id,
|
|
date: doc.data().date,
|
|
teaId: doc.data().teaId,
|
|
teaName: doc.data().teaName,
|
|
teaColor: doc.data().teaColor,
|
|
brewTime: doc.data().brewTime,
|
|
amount: doc.data().amount,
|
|
notes: doc.data().notes
|
|
};
|
|
});
|
|
self.renderHistory();
|
|
self.renderStats();
|
|
self.renderCharts();
|
|
});
|
|
},
|
|
|
|
// Events binden
|
|
bindEvents: function() {
|
|
var self = this;
|
|
|
|
// Google Sign-In
|
|
var googleSignIn = document.getElementById('googleSignIn');
|
|
if (googleSignIn) {
|
|
googleSignIn.addEventListener('click', function() {
|
|
self.signInWithGoogle();
|
|
});
|
|
}
|
|
|
|
// Sign Out
|
|
var signOut = document.getElementById('signOut');
|
|
if (signOut) {
|
|
signOut.addEventListener('click', function() {
|
|
self.signOut();
|
|
});
|
|
}
|
|
|
|
// Formular für neue Teesorte
|
|
var addTeaForm = document.getElementById('addTeaForm');
|
|
if (addTeaForm) {
|
|
addTeaForm.addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
self.addTeaType();
|
|
});
|
|
}
|
|
|
|
// Formular für Tracking
|
|
var trackForm = document.getElementById('trackForm');
|
|
if (trackForm) {
|
|
trackForm.addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
self.trackConsumption();
|
|
});
|
|
}
|
|
|
|
// Filter-Buttons
|
|
var filterToday = document.getElementById('filterToday');
|
|
if (filterToday) {
|
|
filterToday.addEventListener('click', function() {
|
|
self.setFilter('today');
|
|
});
|
|
}
|
|
|
|
var filterWeek = document.getElementById('filterWeek');
|
|
if (filterWeek) {
|
|
filterWeek.addEventListener('click', function() {
|
|
self.setFilter('week');
|
|
});
|
|
}
|
|
|
|
var filterMonth = document.getElementById('filterMonth');
|
|
if (filterMonth) {
|
|
filterMonth.addEventListener('click', function() {
|
|
self.setFilter('month');
|
|
});
|
|
}
|
|
|
|
var filterAll = document.getElementById('filterAll');
|
|
if (filterAll) {
|
|
filterAll.addEventListener('click', function() {
|
|
self.setFilter('all');
|
|
});
|
|
}
|
|
|
|
// Datum auf heute setzen
|
|
var trackDate = document.getElementById('trackDate');
|
|
if (trackDate) {
|
|
trackDate.value = new Date().toISOString().split('T')[0];
|
|
}
|
|
},
|
|
|
|
// Google Sign-In
|
|
signInWithGoogle: function() {
|
|
var self = this;
|
|
var provider = new firebase.auth.GoogleAuthProvider();
|
|
firebase.auth().signInWithPopup(provider)
|
|
.then(function() {
|
|
self.showToast('Erfolgreich angemeldet!', 'success');
|
|
})
|
|
.catch(function(error) {
|
|
console.error('Anmeldung fehlgeschlagen:', error);
|
|
self.showToast('Anmeldung fehlgeschlagen: ' + error.message, 'error');
|
|
});
|
|
},
|
|
|
|
// Sign Out
|
|
signOut: function() {
|
|
var self = this;
|
|
firebase.auth().signOut()
|
|
.then(function() {
|
|
self.showToast('Erfolgreich abgemeldet', 'success');
|
|
})
|
|
.catch(function(error) {
|
|
console.error('Abmeldung fehlgeschlagen:', error);
|
|
self.showToast('Abmeldung fehlgeschlagen', 'error');
|
|
});
|
|
},
|
|
|
|
// Filter setzen
|
|
setFilter: function(filter) {
|
|
this.state.currentFilter = filter;
|
|
this.renderHistory();
|
|
this.updateFilterButtons();
|
|
},
|
|
|
|
// Filter-Buttons aktualisieren
|
|
updateFilterButtons: function() {
|
|
var buttons = [
|
|
{ id: 'filterToday', filter: 'today' },
|
|
{ id: 'filterWeek', filter: 'week' },
|
|
{ id: 'filterMonth', filter: 'month' },
|
|
{ id: 'filterAll', filter: 'all' }
|
|
];
|
|
|
|
var self = this;
|
|
buttons.forEach(function(btn) {
|
|
var element = document.getElementById(btn.id);
|
|
if (element) {
|
|
if (self.state.currentFilter === btn.filter) {
|
|
element.classList.add('active');
|
|
} else {
|
|
element.classList.remove('active');
|
|
}
|
|
}
|
|
});
|
|
},
|
|
|
|
// Neue Teesorte hinzufügen
|
|
addTeaType: function() {
|
|
if (!this.state.currentUser) {
|
|
this.showToast('Bitte melden Sie sich zuerst an', 'error');
|
|
return;
|
|
}
|
|
|
|
var nameInput = document.getElementById('teaName');
|
|
var colorInput = document.getElementById('teaColor');
|
|
|
|
var name = nameInput ? nameInput.value.trim() : '';
|
|
var color = colorInput ? 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
|
|
var exists = false;
|
|
for (var i = 0; i < this.state.teaTypes.length; i++) {
|
|
if (this.state.teaTypes[i].name.toLowerCase() === name.toLowerCase()) {
|
|
exists = true;
|
|
break;
|
|
}
|
|
}
|
|
if (exists) {
|
|
this.showToast('Diese Teesorte existiert bereits', 'error');
|
|
return;
|
|
}
|
|
|
|
var self = this;
|
|
db.collection('users/' + this.state.currentUser.uid + '/teaTypes')
|
|
.add({
|
|
name: name,
|
|
color: color,
|
|
createdAt: firebase.firestore.FieldValue.serverTimestamp()
|
|
})
|
|
.then(function() {
|
|
if (nameInput) nameInput.value = '';
|
|
if (colorInput) colorInput.value = '#4CAF50';
|
|
self.showToast('Teesorte erfolgreich hinzugefügt', 'success');
|
|
})
|
|
.catch(function(error) {
|
|
console.error('Fehler beim Hinzufügen der Teesorte:', error);
|
|
self.showToast('Fehler beim Hinzufügen der Teesorte', 'error');
|
|
});
|
|
},
|
|
|
|
// Teesorte entfernen
|
|
removeTeaType: function(teaId) {
|
|
if (!this.state.currentUser) {
|
|
this.showToast('Bitte melden Sie sich zuerst an', 'error');
|
|
return;
|
|
}
|
|
|
|
var teaType = null;
|
|
for (var i = 0; i < this.state.teaTypes.length; i++) {
|
|
if (this.state.teaTypes[i].id === teaId) {
|
|
teaType = this.state.teaTypes[i];
|
|
break;
|
|
}
|
|
}
|
|
if (!teaType) return;
|
|
|
|
// Prüfen, ob die Sorte in Konsumdaten verwendet wird
|
|
var isUsed = false;
|
|
for (var i = 0; i < this.state.consumption.length; i++) {
|
|
if (this.state.consumption[i].teaId === teaId) {
|
|
isUsed = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (isUsed) {
|
|
if (!confirm('Diese Teesorte wird in Konsumdaten verwendet. Möchten Sie sie trotzdem entfernen?')) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
var self = this;
|
|
var batch = db.batch();
|
|
|
|
// Konsumdaten bereinigen
|
|
db.collection('users/' + this.state.currentUser.uid + '/consumption')
|
|
.where('teaId', '==', teaId)
|
|
.get()
|
|
.then(function(snapshot) {
|
|
snapshot.forEach(function(doc) {
|
|
batch.update(doc.ref, {
|
|
teaName: '[Gelöschte Sorte]',
|
|
teaId: null
|
|
});
|
|
});
|
|
|
|
// Teesorte löschen
|
|
var teaRef = db.collection('users/' + self.state.currentUser.uid + '/teaTypes').doc(teaId);
|
|
batch.delete(teaRef);
|
|
|
|
return batch.commit();
|
|
})
|
|
.then(function() {
|
|
self.showToast('Teesorte entfernt', 'success');
|
|
})
|
|
.catch(function(error) {
|
|
console.error('Fehler beim Entfernen der Teesorte:', error);
|
|
self.showToast('Fehler beim Entfernen der Teesorte', 'error');
|
|
});
|
|
},
|
|
|
|
// Konsum tracken
|
|
trackConsumption: function() {
|
|
if (!this.state.currentUser) {
|
|
this.showToast('Bitte melden Sie sich zuerst an', 'error');
|
|
return;
|
|
}
|
|
|
|
var dateInput = document.getElementById('trackDate');
|
|
var teaSelect = document.getElementById('trackTea');
|
|
var amountInput = document.getElementById('trackAmount');
|
|
var notesInput = document.getElementById('trackNotes');
|
|
|
|
var date = dateInput ? dateInput.value : '';
|
|
var teaId = teaSelect ? teaSelect.value : '';
|
|
var amount = amountInput ? parseInt(amountInput.value) || 1 : 1;
|
|
var notes = notesInput ? notesInput.value.trim() : '';
|
|
|
|
// Brühzeit
|
|
var brewTimeRadio = document.querySelector('input[name="brewTime"]:checked');
|
|
var brewTime = brewTimeRadio ? 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;
|
|
}
|
|
|
|
var teaType = null;
|
|
for (var i = 0; i < this.state.teaTypes.length; i++) {
|
|
if (this.state.teaTypes[i].id === teaId) {
|
|
teaType = this.state.teaTypes[i];
|
|
break;
|
|
}
|
|
}
|
|
if (!teaType) {
|
|
this.showToast('Ungültige Teesorte', 'error');
|
|
return;
|
|
}
|
|
|
|
var self = this;
|
|
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()
|
|
})
|
|
.then(function() {
|
|
if (dateInput) dateInput.valueAsDate = new Date();
|
|
if (teaSelect) teaSelect.selectedIndex = 0;
|
|
if (amountInput) amountInput.value = 1;
|
|
if (notesInput) notesInput.value = '';
|
|
if (brewTimeRadio) brewTimeRadio.checked = true;
|
|
self.showToast('Tee-Konsum erfolgreich getrackt!', 'success');
|
|
})
|
|
.catch(function(error) {
|
|
console.error('Fehler beim Tracken:', error);
|
|
self.showToast('Fehler beim Tracken', 'error');
|
|
});
|
|
},
|
|
|
|
// Konsum-Eintrag entfernen
|
|
removeConsumption: function(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;
|
|
}
|
|
|
|
var self = this;
|
|
db.collection('users/' + this.state.currentUser.uid + '/consumption')
|
|
.doc(entryId)
|
|
.delete()
|
|
.then(function() {
|
|
self.showToast('Eintrag entfernt', 'success');
|
|
})
|
|
.catch(function(error) {
|
|
console.error('Fehler beim Entfernen des Eintrags:', error);
|
|
self.showToast('Fehler beim Entfernen des Eintrags', 'error');
|
|
});
|
|
},
|
|
|
|
// Teetypen rendern
|
|
renderTeaTypes: function() {
|
|
var teaList = document.getElementById('teaList');
|
|
|
|
if (!teaList) return;
|
|
|
|
if (this.state.loading) {
|
|
teaList.innerHTML = '<div class="empty-state"><div class="icon">Loading...</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">No Tea</div><p>Keine Teesorten hinterlegt</p><p>Add a new tea type above</p></div>';
|
|
return;
|
|
}
|
|
|
|
if (!this.state.currentUser) {
|
|
teaList.innerHTML = '<div class="empty-state"><div class="icon">Lock</div><p>Please log in to see your tea types</p></div>';
|
|
return;
|
|
}
|
|
|
|
var html = '';
|
|
for (var i = 0; i < this.state.teaTypes.length; i++) {
|
|
var tea = this.state.teaTypes[i];
|
|
html += '<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="Remove">X</button>' +
|
|
'</div>' +
|
|
'</div>';
|
|
}
|
|
teaList.innerHTML = html;
|
|
},
|
|
|
|
// Track-Formular rendern
|
|
renderTrackForm: function() {
|
|
var teaSelect = document.getElementById('trackTea');
|
|
|
|
if (!teaSelect) return;
|
|
|
|
if (!this.state.currentUser) {
|
|
teaSelect.innerHTML = '<option value="" selected disabled>Please log in first</option>';
|
|
return;
|
|
}
|
|
|
|
if (this.state.teaTypes.length === 0) {
|
|
teaSelect.innerHTML = '<option value="" selected disabled>No tea types available</option>';
|
|
return;
|
|
}
|
|
|
|
var options = '';
|
|
for (var i = 0; i < this.state.teaTypes.length; i++) {
|
|
var tea = this.state.teaTypes[i];
|
|
options += '<option value="' + tea.id + '">Tea: ' + this.escapeHtml(tea.name) + '</option>';
|
|
}
|
|
|
|
options = '<option value="" selected disabled>Select tea type</option>' + options;
|
|
teaSelect.innerHTML = options;
|
|
},
|
|
|
|
// Konsum-Historie rendern
|
|
renderHistory: function() {
|
|
var historyList = document.getElementById('historyList');
|
|
|
|
if (!historyList) return;
|
|
|
|
if (!this.state.currentUser) {
|
|
historyList.innerHTML = '<div class="empty-state"><div class="icon">Lock</div><p>Please log in to see your history</p></div>';
|
|
return;
|
|
}
|
|
|
|
var filteredConsumption = this.state.consumption.slice();
|
|
|
|
// Filter anwenden
|
|
var today = new Date().toISOString().split('T')[0];
|
|
var weekAgo = new Date();
|
|
weekAgo.setDate(weekAgo.getDate() - 7);
|
|
var weekAgoStr = weekAgo.toISOString().split('T')[0];
|
|
var monthAgo = new Date();
|
|
monthAgo.setMonth(monthAgo.getMonth() - 1);
|
|
var monthAgoStr = monthAgo.toISOString().split('T')[0];
|
|
|
|
switch (this.state.currentFilter) {
|
|
case 'today':
|
|
filteredConsumption = filteredConsumption.filter(function(c) {
|
|
return c.date === today;
|
|
});
|
|
break;
|
|
case 'week':
|
|
filteredConsumption = filteredConsumption.filter(function(c) {
|
|
return c.date >= weekAgoStr;
|
|
});
|
|
break;
|
|
case 'month':
|
|
filteredConsumption = filteredConsumption.filter(function(c) {
|
|
return c.date >= monthAgoStr;
|
|
});
|
|
break;
|
|
}
|
|
|
|
// Sortieren nach Datum (neueste zuerst)
|
|
filteredConsumption.sort(function(a, b) {
|
|
return new Date(b.date) - new Date(a.date);
|
|
});
|
|
|
|
if (filteredConsumption.length === 0) {
|
|
historyList.innerHTML = '<div class="empty-state"><div class="icon">Coffee</div><p>No entries found</p><p>Track your first tea!</p></div>';
|
|
return;
|
|
}
|
|
|
|
var html = '';
|
|
for (var i = 0; i < filteredConsumption.length; i++) {
|
|
var entry = filteredConsumption[i];
|
|
var date = new Date(entry.date);
|
|
var dateStr = date.toLocaleDateString('de-DE', {
|
|
weekday: 'short',
|
|
day: '2-digit',
|
|
month: '2-digit',
|
|
year: 'numeric'
|
|
});
|
|
|
|
var brewTimeLabel = this.getBrewTimeLabel(entry.brewTime);
|
|
var brewTimeClass = entry.brewTime;
|
|
|
|
html += '<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">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="Remove">X</button>' +
|
|
'</div>' +
|
|
'</div>';
|
|
}
|
|
historyList.innerHTML = html;
|
|
},
|
|
|
|
// Statistiken rendern
|
|
renderStats: function() {
|
|
if (!this.state.currentUser) {
|
|
if (document.getElementById('today-count')) document.getElementById('today-count').textContent = '?';
|
|
if (document.getElementById('month-count')) document.getElementById('month-count').textContent = '?';
|
|
if (document.getElementById('year-count')) document.getElementById('year-count').textContent = '?';
|
|
if (document.getElementById('favorite-tea')) document.getElementById('favorite-tea').textContent = '?';
|
|
if (document.getElementById('favorite-count')) document.getElementById('favorite-count').textContent = '?';
|
|
return;
|
|
}
|
|
|
|
var today = new Date().toISOString().split('T')[0];
|
|
var currentMonth = new Date().getMonth();
|
|
var currentYear = new Date().getFullYear();
|
|
|
|
// Heute
|
|
var todayConsumption = this.state.consumption.filter(function(c) {
|
|
return c.date === today;
|
|
});
|
|
var todayCount = todayConsumption.reduce(function(sum, c) {
|
|
return sum + c.amount;
|
|
}, 0);
|
|
if (document.getElementById('today-count')) document.getElementById('today-count').textContent = todayCount;
|
|
|
|
// Dieser Monat
|
|
var monthConsumption = this.state.consumption.filter(function(c) {
|
|
var entryDate = new Date(c.date);
|
|
return entryDate.getMonth() === currentMonth && entryDate.getFullYear() === currentYear;
|
|
});
|
|
var monthCount = monthConsumption.reduce(function(sum, c) {
|
|
return sum + c.amount;
|
|
}, 0);
|
|
if (document.getElementById('month-count')) document.getElementById('month-count').textContent = monthCount;
|
|
|
|
// Dieses Jahr
|
|
var yearConsumption = this.state.consumption.filter(function(c) {
|
|
var entryDate = new Date(c.date);
|
|
return entryDate.getFullYear() === currentYear;
|
|
});
|
|
var yearCount = yearConsumption.reduce(function(sum, c) {
|
|
return sum + c.amount;
|
|
}, 0);
|
|
if (document.getElementById('year-count')) document.getElementById('year-count').textContent = yearCount;
|
|
|
|
// Lieblingssorte
|
|
var yearTeaCounts = {};
|
|
yearConsumption.forEach(function(c) {
|
|
var teaName = c.teaName || 'Unknown';
|
|
yearTeaCounts[teaName] = (yearTeaCounts[teaName] || 0) + c.amount;
|
|
});
|
|
|
|
var sortedTeas = Object.entries(yearTeaCounts).sort(function(a, b) {
|
|
return b[1] - a[1];
|
|
});
|
|
|
|
if (sortedTeas.length > 0) {
|
|
if (document.getElementById('favorite-tea')) document.getElementById('favorite-tea').textContent = this.escapeHtml(sortedTeas[0][0]);
|
|
if (document.getElementById('favorite-count')) document.getElementById('favorite-count').textContent = sortedTeas[0][1];
|
|
} else {
|
|
if (document.getElementById('favorite-tea')) document.getElementById('favorite-tea').textContent = '-';
|
|
if (document.getElementById('favorite-count')) document.getElementById('favorite-count').textContent = '0';
|
|
}
|
|
},
|
|
|
|
// Charts rendern
|
|
renderCharts: function() {
|
|
if (!this.state.currentUser) {
|
|
this.clearCharts();
|
|
return;
|
|
}
|
|
this.renderTodayChart();
|
|
this.renderMonthChart();
|
|
this.renderYearChart();
|
|
},
|
|
|
|
// Heute-Chart
|
|
renderTodayChart: function() {
|
|
var today = new Date().toISOString().split('T')[0];
|
|
var todayConsumption = this.state.consumption.filter(function(c) {
|
|
return c.date === today;
|
|
});
|
|
|
|
// Nach Teesorte gruppieren
|
|
var teaCounts = {};
|
|
var teaColors = {};
|
|
|
|
todayConsumption.forEach(function(c) {
|
|
var teaName = c.teaName || 'Unknown';
|
|
teaCounts[teaName] = (teaCounts[teaName] || 0) + c.amount;
|
|
teaColors[teaName] = c.teaColor || '#CCCCCC';
|
|
});
|
|
|
|
var teaNames = Object.keys(teaCounts);
|
|
var counts = Object.values(teaCounts);
|
|
var colors = teaNames.map(function(name) {
|
|
return teaColors[name];
|
|
});
|
|
|
|
var ctx = document.getElementById('todayChart');
|
|
if (!ctx) return;
|
|
|
|
if (this.todayChart) {
|
|
this.todayChart.destroy();
|
|
}
|
|
|
|
this.todayChart = new Chart(ctx.getContext('2d'), {
|
|
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: function() {
|
|
var currentMonth = new Date().getMonth();
|
|
var currentYear = new Date().getFullYear();
|
|
|
|
var monthConsumption = this.state.consumption.filter(function(c) {
|
|
var entryDate = new Date(c.date);
|
|
return entryDate.getMonth() === currentMonth && entryDate.getFullYear() === currentYear;
|
|
});
|
|
|
|
// Nach Tag gruppieren
|
|
var dayCounts = {};
|
|
monthConsumption.forEach(function(c) {
|
|
var day = new Date(c.date).getDate();
|
|
dayCounts[day] = (dayCounts[day] || 0) + c.amount;
|
|
});
|
|
|
|
// Alle Tage des Monats
|
|
var daysInMonth = new Date(currentYear, currentMonth + 1, 0).getDate();
|
|
var labels = [];
|
|
var data = [];
|
|
for (var day = 1; day <= daysInMonth; day++) {
|
|
labels.push(day.toString());
|
|
data.push(dayCounts[day] || 0);
|
|
}
|
|
|
|
var ctx = document.getElementById('monthChart');
|
|
if (!ctx) return;
|
|
|
|
if (this.monthChart) {
|
|
this.monthChart.destroy();
|
|
}
|
|
|
|
this.monthChart = new Chart(ctx.getContext('2d'), {
|
|
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: function() {
|
|
var currentYear = new Date().getFullYear();
|
|
|
|
var yearConsumption = this.state.consumption.filter(function(c) {
|
|
var entryDate = new Date(c.date);
|
|
return entryDate.getFullYear() === currentYear;
|
|
});
|
|
|
|
// Nach Teesorte gruppieren
|
|
var teaCounts = {};
|
|
var teaColors = {};
|
|
|
|
yearConsumption.forEach(function(c) {
|
|
var teaName = c.teaName || 'Unknown';
|
|
teaCounts[teaName] = (teaCounts[teaName] || 0) + c.amount;
|
|
teaColors[teaName] = c.teaColor || '#CCCCCC';
|
|
});
|
|
|
|
var teaNames = Object.keys(teaCounts);
|
|
var counts = Object.values(teaCounts);
|
|
var colors = teaNames.map(function(name) {
|
|
return teaColors[name];
|
|
});
|
|
|
|
var ctx = document.getElementById('yearChart');
|
|
if (!ctx) return;
|
|
|
|
if (this.yearChart) {
|
|
this.yearChart.destroy();
|
|
}
|
|
|
|
this.yearChart = new Chart(ctx.getContext('2d'), {
|
|
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
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
},
|
|
|
|
// Charts leeren
|
|
clearCharts: function() {
|
|
['todayChart', 'monthChart', 'yearChart'].forEach(function(chartId) {
|
|
if (this[chartId]) {
|
|
this[chartId].destroy();
|
|
this[chartId] = null;
|
|
}
|
|
}.bind(this));
|
|
},
|
|
|
|
// Brühzeit-Label
|
|
getBrewTimeLabel: function(brewTime) {
|
|
var labels = {
|
|
'kurz': 'Kurz',
|
|
'normal': 'Normal',
|
|
'lang': 'Lang'
|
|
};
|
|
return labels[brewTime] || brewTime;
|
|
},
|
|
|
|
// HTML escape
|
|
escapeHtml: function(text) {
|
|
if (!text) return '';
|
|
var div = document.createElement('div');
|
|
div.textContent = text;
|
|
return div.innerHTML;
|
|
},
|
|
|
|
// Auth-Bereich verstecken
|
|
hideAuthSection: function() {
|
|
var authSection = document.getElementById('auth-section');
|
|
if (authSection) {
|
|
authSection.classList.add('hidden');
|
|
}
|
|
},
|
|
|
|
// Auth-Bereich anzeigen
|
|
showAuthSection: function() {
|
|
var authSection = document.getElementById('auth-section');
|
|
if (authSection) {
|
|
authSection.classList.remove('hidden');
|
|
}
|
|
},
|
|
|
|
// Angemeldete UI anzeigen
|
|
showSignedInUI: function() {
|
|
var signInBtn = document.getElementById('googleSignIn');
|
|
var signOutBtn = document.getElementById('signOut');
|
|
var 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: function() {
|
|
var signInBtn = document.getElementById('googleSignIn');
|
|
var signOutBtn = document.getElementById('signOut');
|
|
var 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>';
|
|
}
|
|
},
|
|
|
|
// Alle Render-Funktionen
|
|
renderAll: function() {
|
|
this.renderTeaTypes();
|
|
this.renderTrackForm();
|
|
this.renderHistory();
|
|
this.renderStats();
|
|
this.renderCharts();
|
|
},
|
|
|
|
// Toast-Nachricht anzeigen
|
|
showToast: function(message, type) {
|
|
// Entferne bestehende Toasts
|
|
var existingToast = document.querySelector('.toast');
|
|
if (existingToast) {
|
|
existingToast.remove();
|
|
}
|
|
|
|
var toast = document.createElement('div');
|
|
toast.className = 'toast ' + (type || 'success');
|
|
toast.textContent = message;
|
|
document.body.appendChild(toast);
|
|
|
|
setTimeout(function() {
|
|
toast.style.animation = 'slideIn 0.3s ease-out reverse';
|
|
setTimeout(function() {
|
|
toast.remove();
|
|
}, 300);
|
|
}, 3000);
|
|
},
|
|
|
|
// Datum aktualisieren (Fußzeile)
|
|
updateDate: function() {
|
|
var yearSpan = document.getElementById('current-year');
|
|
if (yearSpan) {
|
|
yearSpan.textContent = new Date().getFullYear();
|
|
}
|
|
}
|
|
};
|
|
|
|
// Initialisierung beim Laden der Seite
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
App.init();
|
|
});
|