diff --git a/script.js b/script.js index b2b9695..0d33734 100644 --- a/script.js +++ b/script.js @@ -1,7 +1,7 @@ -// TeeTracker Application mit Firebase -// ====================================== +// TeeTracker Application mit Firebase (Browser-kompatibel) +// ===================================================== -const App = { +var App = { // Initialer Zustand state: { teaTypes: [], @@ -12,235 +12,235 @@ const App = { }, // Initialisierung - init() { + init: function() { this.bindEvents(); this.setupFirebaseAuth(); this.updateDate(); }, // Firebase Auth einrichten - setupFirebaseAuth() { - firebase.auth().onAuthStateChanged((user) => { + setupFirebaseAuth: function() { + var self = this; + firebase.auth().onAuthStateChanged(function(user) { if (user) { - this.state.currentUser = user; - this.loadUserData(); - this.hideAuthSection(); - this.showSignedInUI(); + self.state.currentUser = user; + self.loadUserData(); + self.hideAuthSection(); + self.showSignedInUI(); } else { - this.state.currentUser = null; - this.state.teaTypes = []; - this.state.consumption = []; - this.showAuthSection(); - this.showSignedOutUI(); - this.renderAll(); + self.state.currentUser = null; + self.state.teaTypes = []; + self.state.consumption = []; + self.showAuthSection(); + self.showSignedOutUI(); + self.renderAll(); } }); }, // Benutzerdaten laden - async loadUserData() { + loadUserData: function() { + var self = this; 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() - })); + this.state.loading = true; - // 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'); - } + // 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() { + setupRealtimeUpdates: function() { + var self = this; if (!this.state.currentUser) return; // Teesorten-Updates - db.collection(`users/${this.state.currentUser.uid}/teaTypes`) + 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(); + .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`) + 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(); + .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() { + bindEvents: function() { + var self = this; + // Google Sign-In - document.getElementById('googleSignIn')?.addEventListener('click', () => { - this.signInWithGoogle(); - }); + var googleSignIn = document.getElementById('googleSignIn'); + if (googleSignIn) { + googleSignIn.addEventListener('click', function() { + self.signInWithGoogle(); + }); + } // Sign Out - document.getElementById('signOut')?.addEventListener('click', () => { - this.signOut(); - }); + var signOut = document.getElementById('signOut'); + if (signOut) { + signOut.addEventListener('click', function() { + self.signOut(); + }); + } // Formular für neue Teesorte - document.getElementById('addTeaForm')?.addEventListener('submit', (e) => { - e.preventDefault(); - this.addTeaType(); - }); + var addTeaForm = document.getElementById('addTeaForm'); + if (addTeaForm) { + addTeaForm.addEventListener('submit', function(e) { + e.preventDefault(); + self.addTeaType(); + }); + } // Formular für Tracking - document.getElementById('trackForm')?.addEventListener('submit', (e) => { - e.preventDefault(); - this.trackConsumption(); - }); + var trackForm = document.getElementById('trackForm'); + if (trackForm) { + trackForm.addEventListener('submit', function(e) { + e.preventDefault(); + self.trackConsumption(); + }); + } // Filter-Buttons - document.getElementById('filterToday')?.addEventListener('click', () => { - this.setFilter('today'); - }); + var filterToday = document.getElementById('filterToday'); + if (filterToday) { + filterToday.addEventListener('click', function() { + self.setFilter('today'); + }); + } - document.getElementById('filterWeek')?.addEventListener('click', () => { - this.setFilter('week'); - }); + var filterWeek = document.getElementById('filterWeek'); + if (filterWeek) { + filterWeek.addEventListener('click', function() { + self.setFilter('week'); + }); + } - document.getElementById('filterMonth')?.addEventListener('click', () => { - this.setFilter('month'); - }); + var filterMonth = document.getElementById('filterMonth'); + if (filterMonth) { + filterMonth.addEventListener('click', function() { + self.setFilter('month'); + }); + } - document.getElementById('filterAll')?.addEventListener('click', () => { - this.setFilter('all'); - }); + var filterAll = document.getElementById('filterAll'); + if (filterAll) { + filterAll.addEventListener('click', function() { + self.setFilter('all'); + }); + } // Datum auf heute setzen - document.getElementById('trackDate')?.setAttribute('value', new Date().toISOString().split('T')[0]); + var trackDate = document.getElementById('trackDate'); + if (trackDate) { + trackDate.value = new Date().toISOString().split('T')[0]; + } }, // Google Sign-In - signInWithGoogle() { - const provider = new firebase.auth.GoogleAuthProvider(); + signInWithGoogle: function() { + var self = this; + var provider = new firebase.auth.GoogleAuthProvider(); firebase.auth().signInWithPopup(provider) - .then((result) => { - this.showToast('Erfolgreich angemeldet!', 'success'); + .then(function() { + self.showToast('Erfolgreich angemeldet!', 'success'); }) - .catch((error) => { + .catch(function(error) { console.error('Anmeldung fehlgeschlagen:', error); - this.showToast('Anmeldung fehlgeschlagen: ' + error.message, 'error'); + self.showToast('Anmeldung fehlgeschlagen: ' + error.message, 'error'); }); }, // Sign Out - signOut() { + signOut: function() { + var self = this; firebase.auth().signOut() - .then(() => { - this.showToast('Erfolgreich abgemeldet', 'success'); + .then(function() { + self.showToast('Erfolgreich abgemeldet', 'success'); }) - .catch((error) => { + .catch(function(error) { console.error('Abmeldung fehlgeschlagen:', error); - this.showToast('Abmeldung fehlgeschlagen', 'error'); + self.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 = `Angemeldet als: ${this.state.currentUser?.displayName || this.state.currentUser?.email}`; - } - }, - - // 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 = 'Nicht angemeldet'; - } - }, - // Filter setzen - setFilter(filter) { + setFilter: function(filter) { this.state.currentFilter = filter; this.renderHistory(); this.updateFilterButtons(); }, // Filter-Buttons aktualisieren - updateFilterButtons() { - const buttons = [ + updateFilterButtons: function() { + var 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); + var self = this; + buttons.forEach(function(btn) { + var element = document.getElementById(btn.id); if (element) { - if (this.state.currentFilter === btn.filter) { + if (self.state.currentFilter === btn.filter) { element.classList.add('active'); } else { element.classList.remove('active'); @@ -250,17 +250,17 @@ const App = { }, // Neue Teesorte hinzufügen - async addTeaType() { + addTeaType: function() { if (!this.state.currentUser) { this.showToast('Bitte melden Sie sich zuerst an', 'error'); return; } - const nameInput = document.getElementById('teaName'); - const colorInput = document.getElementById('teaColor'); + var nameInput = document.getElementById('teaName'); + var colorInput = document.getElementById('teaColor'); - const name = nameInput?.value?.trim(); - const color = colorInput?.value || '#4CAF50'; + 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'); @@ -268,98 +268,117 @@ const App = { } // Prüfen, ob die Sorte bereits existiert - const exists = this.state.teaTypes.some(t => t.name.toLowerCase() === name.toLowerCase()); + 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; } - 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'); - } + 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 - async removeTeaType(teaId) { + removeTeaType: function(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); + 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 - const isUsed = this.state.consumption.some(c => c.teaId === teaId); - + 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; } } - try { - // Konsumdaten bereinigen - const consumptionRefs = await db - .collection(`users/${this.state.currentUser.uid}/consumption`) - .where('teaId', '==', teaId) - .get(); + var self = this; + var batch = db.batch(); - const batch = db.batch(); - consumptionRefs.forEach(doc => { - batch.update(doc.ref, { - teaName: '[Gelöschte Sorte]', - teaId: null + // 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'); }); - - // 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() { + trackConsumption: function() { 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'); + var dateInput = document.getElementById('trackDate'); + var teaSelect = document.getElementById('trackTea'); + var amountInput = document.getElementById('trackAmount'); + var notesInput = document.getElementById('trackNotes'); - const date = dateInput?.value; - const teaId = teaSelect?.value; - const amount = parseInt(amountInput?.value) || 1; - const notes = notesInput?.value?.trim() || ''; + 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 - const brewTimeRadio = document.querySelector('input[name="brewTime"]:checked'); - const brewTime = brewTimeRadio?.value || 'normal'; + 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'); @@ -371,43 +390,46 @@ const App = { return; } - const teaType = this.state.teaTypes.find(t => t.id === teaId); + 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; } - 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'); - } + 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 - async removeConsumption(entryId) { + removeConsumption: function(entryId) { if (!this.state.currentUser) { this.showToast('Bitte melden Sie sich zuerst an', 'error'); return; @@ -417,242 +439,230 @@ const App = { 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(); + 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() { - const teaList = document.getElementById('teaList'); + renderTeaTypes: function() { + var teaList = document.getElementById('teaList'); if (!teaList) return; if (this.state.loading) { - teaList.innerHTML = '
Daten werden geladen...
Daten werden geladen...
Keine Teesorten hinterlegt
-Fügen Sie oben eine neue Teesorte hinzu
-Keine Teesorten hinterlegt
Add a new tea type above
Bitte melden Sie sich an, um Ihre Teesorten zu sehen
-Please log in to see your tea types
Bitte melden Sie sich an, um Ihre Historie zu sehen
-Please log in to see your history
Keine Einträge gefunden
-Tracken Sie Ihren ersten Tee!
-No entries found
Track your first tea!