diff --git a/app.js b/app.js index fc826fb..64c8575 100644 --- a/app.js +++ b/app.js @@ -20,7 +20,14 @@ class TeaTracker { // Load data from localStorage loadTeas() { const data = localStorage.getItem(STORAGE_KEY_TEAS); - return data ? JSON.parse(data) : []; + if (!data) return []; + + const teas = JSON.parse(data); + // Migrate existing teas to add organic field if missing + return teas.map(tea => ({ + ...tea, + organic: tea.organic !== undefined ? tea.organic : false + })); } loadEntries() { @@ -64,6 +71,7 @@ class TeaTracker { brand: tea.brand || '', description: tea.description || '', color: tea.color || '#8B4513', + organic: tea.organic || false, createdAt: new Date().toISOString() }; this.teas.push(newTea); @@ -139,20 +147,14 @@ class TeaTracker { .reduce((sum, entry) => sum + entry.amount, 0); } - getWeekCups() { - const now = new Date(); - const weekAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000); - - return this.entries - .filter(entry => new Date(entry.date) >= weekAgo) - .reduce((sum, entry) => sum + entry.amount, 0); - } - getCupsByPeriod(period) { const now = new Date(); let startDate; switch (period) { + case 'today': + startDate = new Date(now.getFullYear(), now.getMonth(), now.getDate()); + break; case 'week': startDate = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000); break; @@ -174,6 +176,47 @@ class TeaTracker { .reduce((sum, entry) => sum + entry.amount, 0); } + getWeekCups() { + const now = new Date(); + const weekAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000); + + return this.entries + .filter(entry => new Date(entry.date) >= weekAgo) + .reduce((sum, entry) => sum + entry.amount, 0); + } + + getEntriesByPeriod(period) { + const now = new Date(); + let startDate; + + switch (period) { + case 'today': + startDate = new Date(now.getFullYear(), now.getMonth(), now.getDate()); + break; + case 'week': + startDate = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000); + break; + case 'month': + startDate = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate()); + break; + case 'year': + startDate = new Date(now.getFullYear() - 1, now.getMonth(), now.getDate()); + break; + case 'all': + startDate = new Date(0); + break; + default: + startDate = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000); + } + + return this.entries.filter(entry => new Date(entry.date) >= startDate); + } + + getCupsByPeriod(period) { + const entries = this.getEntriesByPeriod(period); + return entries.reduce((sum, entry) => sum + entry.amount, 0); + } + getCupsByTeaType(period = 'all') { const entries = this.getEntriesByPeriod(period); const typeCounts = {}; @@ -389,7 +432,7 @@ class TeaTracker { return `