addGoal
unknown
html
a year ago
6.8 kB
6
Indexable
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Goal - Goal-ito</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<header>
<h1>Goal-ito</h1>
</header>
<div id="addGoalForm">
<h1>Add a new goal!</h1>
<label for="goalName">What is your goal?</label>
<input type="text" id="goalName" placeholder="Enter your goal">
<label for="goalDesc">Describe your goal</label>
<textarea id="goalDesc" placeholder="Goal description"></textarea>
<label>How important is your goal?</label>
<div class="priority-options">
<button class="priority-btn" data-priority="5">Most Priority</button>
<button class="priority-btn" data-priority="4">More Priority</button>
<button class="priority-btn" data-priority="3">Priority</button>
<button class="priority-btn" data-priority="2">Less Priority</button>
<button class="priority-btn" data-priority="1">Least Priority</button>
</div>
<label for="goalDue">When is your goal due?</label>
<input type="date" id="goalDue">
<button id="saveGoalBtn">Save Goal</button>
</div>
<!-- Stats Section -->
<div class="stats-left">
<h2>Stats</h2>
<p id="statsElement">Completed: 0 tasks</p> <!-- Element for completed tasks -->
</div>
<!-- Goals List Section -->
<div id="goalsList">
<h2>Your Goals</h2>
<ul id="goalItemsContainer"></ul>
</div>
<!-- Back button to return to dashboard -->
<button id="backBtn">Back to Dashboard</button>
</div>
<script>
const saveGoalBtn = document.getElementById('saveGoalBtn');
const priorityBtns = document.querySelectorAll('.priority-btn');
const goalItemsContainer = document.getElementById('goalItemsContainer');
const backBtn = document.getElementById('backBtn');
let selectedPriority = 3; // Default priority
// Handle priority selection
priorityBtns.forEach(btn => {
btn.addEventListener('click', (e) => {
priorityBtns.forEach(button => button.classList.remove('selected'));
e.target.classList.add('selected');
selectedPriority = e.target.dataset.priority;
});
});
// Function to display and sort goals
function displayGoals() {
let goals = JSON.parse(localStorage.getItem('goals')) || [];
// Sort the goals by priority (descending)
goals.sort((a, b) => b.priority - a.priority);
// Clear the current display
goalItemsContainer.innerHTML = '';
// Render the sorted goals
goals.forEach((goal, index) => {
const goalItem = document.createElement('li');
goalItem.classList.add('goal-item');
goalItem.innerHTML = `
<h3>${goal.name} (Priority: ${goal.priority})</h3>
<p>${goal.desc}</p>
<p>Due: ${goal.due}</p>
<button class="deleteGoalBtn" data-index="${index}">Mark as Done</button>
`;
goalItemsContainer.appendChild(goalItem);
});
// Add event listeners to delete buttons
const deleteButtons = document.querySelectorAll('.deleteGoalBtn');
deleteButtons.forEach(button => {
button.addEventListener('click', (e) => {
const goalIndex = e.target.dataset.index;
deleteGoal(goalIndex, e.target); // Pass the button element for further actions
});
});
}
// Function to delete a goal and update stats
function deleteGoal(index, button) {
let goals = JSON.parse(localStorage.getItem('goals')) || [];
// Remove the goal at the specified index
goals.splice(index, 1);
// Update the localStorage with the new goals array
localStorage.setItem('goals', JSON.stringify(goals));
// Increment the completed tasks count in localStorage
let completedTasks = JSON.parse(localStorage.getItem('completedTasks')) || 0;
completedTasks++;
localStorage.setItem('completedTasks', JSON.stringify(completedTasks));
// Update the stats
updateStats();
// Disable the button to prevent multiple clicks
button.disabled = true;
// Remove the goal item from the UI
button.closest('li').remove();
}
// Function to update stats
function updateStats() {
const completedTasks = JSON.parse(localStorage.getItem('completedTasks')) || 0;
const statsElement = document.getElementById('statsElement');
statsElement.innerText = `Completed: ${completedTasks} task${completedTasks !== 1 ? 's' : ''}`;
}
// Handle saving a goal
saveGoalBtn.addEventListener('click', () => {
const goalName = document.getElementById('goalName').value;
const goalDesc = document.getElementById('goalDesc').value;
const goalDue = document.getElementById('goalDue').value;
if (goalName && goalDesc && goalDue) {
const newGoal = {
name: goalName,
desc: goalDesc,
due: goalDue,
priority: selectedPriority
};
// Store goal in local storage
let goals = JSON.parse(localStorage.getItem('goals')) || [];
goals.push(newGoal);
localStorage.setItem('goals', JSON.stringify(goals));
// Display the updated goals
displayGoals();
} else {
alert("Please fill out all fields before saving.");
}
});
// Display stored goals on page load
window.onload = () => {
displayGoals();
updateStats(); // Make sure stats are updated on load
};
// Back button to redirect to the dashboard
backBtn.addEventListener('click', () => {
window.location.href = 'dashboard.html';
});
</script>
</body>
</html>
Editor is loading...
Leave a Comment