Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const state = {
+ goals: JSON.parse(localStorage.getItem('goals')) || [],
+ timeEntries: JSON.parse(localStorage.getItem('timeEntries')) || []
+ };
+
+ const addGoalBtn = document.getElementById('addGoalBtn');
+ const addGoalModal = document.getElementById('addGoalModal');
+ const newGoalForm = document.getElementById('newGoalForm');
+ const goalsList = document.getElementById('goalsList');
+ const noGoalsMessage = document.getElementById('noGoalsMessage');
+ const viewStatsBtn = document.getElementById('viewStatsBtn');
+ const statsModal = document.getElementById('statsModal');
+
+ function saveToLocalStorage() {
+ localStorage.setItem('goals', JSON.stringify(state.goals));
+ localStorage.setItem('timeEntries', JSON.stringify(state.timeEntries));
+ }
+
+ function renderGoals() {
+ goalsList.innerHTML = '';
+ if (state.goals.length === 0) {
+ noGoalsMessage.classList.remove('hidden');
+ } else {
+ noGoalsMessage.classList.add('hidden');
+ state.goals.forEach(goal => {
+ const goalCard = createGoalCard(goal);
+ goalsList.appendChild(goalCard);
+ });
+ }
+ }
+
+ function createGoalCard(goal) {
+ const card = document.createElement('div');
+ card.className = 'goal-card';
+ card.style.borderLeft = `4px solid ${goal.color}`;
+
+ const today = new Date().toISOString().split('T')[0];
+ const todayTime = state.timeEntries
+ .filter(entry => entry.goalId === goal.id && entry.date === today)
+ .reduce((acc, entry) => acc + entry.hours, 0);
+
+ card.innerHTML = `
+
${goal.name}
+
Target: ${goal.targetHours} hours/day
+
Today: ${todayTime} hours
+ `;
+ return card;
+ }
+
+ addGoalBtn.addEventListener('click', () => {
+ addGoalModal.classList.remove('hidden');
+ });
+
+ newGoalForm.addEventListener('submit', (e) => {
+ e.preventDefault();
+ const newGoal = {
+ id: Date.now().toString(),
+ name: document.getElementById('goalName').value,
+ targetHours: parseFloat(document.getElementById('targetHours').value),
+ color: document.getElementById('goalColor').value
+ };
+ state.goals.push(newGoal);
+ saveToLocalStorage();
+ renderGoals();
+ addGoalModal.classList.add('hidden');
+ newGoalForm.reset();
+ });
+
+ document.querySelectorAll('.cancel-btn').forEach(btn => {
+ btn.addEventListener('click', () => {
+ addGoalModal.classList.add('hidden');
+ statsModal.classList.add('hidden');
+ });
+ });
+
+ window.addTime = (goalId) => {
+ const hours = parseFloat(prompt('Enter hours spent (0.5 - 24):'));
+ if (hours && hours >= 0.5 && hours <= 24) {
+ state.timeEntries.push({
+ goalId,
+ date: new Date().toISOString().split('T')[0],
+ hours
+ });
+ saveToLocalStorage();
+ renderGoals();
+ }
+ };
+
+ window.deleteGoal = (goalId) => {
+ if (confirm('Are you sure you want to delete this goal?')) {
+ state.goals = state.goals.filter(goal => goal.id !== goalId);
+ state.timeEntries = state.timeEntries.filter(entry => entry.goalId !== goalId);
+ saveToLocalStorage();
+ renderGoals();
+ }
+ };
+
+ viewStatsBtn.addEventListener('click', () => {
+ const statsContent = document.getElementById('statsContent');
+ statsContent.innerHTML = generateStats();
+ statsModal.classList.remove('hidden');
+ });
+
+ function generateStats() {
+ if (state.goals.length === 0) return '
No goals to display statistics for.
';
+
+ let statsHTML = '
';
+ state.goals.forEach(goal => {
+ const totalTime = state.timeEntries
+ .filter(entry => entry.goalId === goal.id)
+ .reduce((acc, entry) => acc + entry.hours, 0);
+
+ statsHTML += `
+
+
${goal.name}
+
Total time: ${totalTime.toFixed(1)} hours
+
Daily average: ${(totalTime / 7).toFixed(1)} hours
+
+ `;
+ });
+ statsHTML += '
';
+ return statsHTML;
+ }
+
+ renderGoals();
+ });