Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added user interactions #3

Merged
merged 2 commits into from
Jul 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
87 changes: 43 additions & 44 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,49 +62,48 @@ document.querySelector('.clear-completed').addEventListener('click', () => {
// Load tasks
UI.displayTasks();

//* DO IGNORE THESE IT HAS SOMETHING TO DO WITH NEXT Pull Reqest
// //after content is loaded check loacal storage if there is any completed task
// document.querySelectorAll('input[type="checkbox"]').forEach((box) => {
// const tasks = Store.getTasks();
// const taskList = document.querySelectorAll(".task");
// let nodes = Array.prototype.slice.call(taskList); //convert list in array
// for (let i = 0; i < tasks.length; i+= 1) {
// //iterate over tasks to find completed tasks
// let task = tasks[i].completed;
// if (tasks[i].index === nodes.indexOf(box.parentElement.parentElement)) {
// //condition if
// if (task) {
// //if task is completed
// box.setAttribute("checked", ""); //fill the check
// }
// localStorage.setItem("tasks", JSON.stringify(tasks)); //update storage
// }
// }
// after content is loaded check loacal storage if there is any completed task
document.querySelectorAll('input[type="checkbox"]').forEach((box) => {
const tasks = Store.getTasks();
const taskList = document.querySelectorAll('.task');
const nodes = Array.prototype.slice.call(taskList); // convert list in array
for (let i = 0; i < tasks.length; i += 1) {
// iterate over tasks to find completed tasks
const task = tasks[i].completed;
if (tasks[i].index === nodes.indexOf(box.parentElement.parentElement)) {
// condition if
if (task) {
// if task is completed
box.setAttribute('checked', ''); // fill the check
}
localStorage.setItem('tasks', JSON.stringify(tasks)); // update storage
}
}

// // Event: update storage when a task is completed
// document.querySelectorAll('input[type="checkbox"]').forEach((box) => {
// const tasks = Store.getTasks();
// const taskList = document.querySelectorAll(".task");
// box.addEventListener("change", () => {
// //same on when user take action conditions are opposite
// let nodes = Array.prototype.slice.call(taskList);
// for (let i = 0; i < tasks.length; i+= 1) {
// let completed = tasks[i].completed;
// if (tasks[i].index === nodes.indexOf(box.parentElement.parentElement)) {
// if (completed) {
// box.removeAttribute("checked");
// tasks[i].completed = false;
// }
// if (!completed) {
// box.setAttribute("checked", "");
// tasks[i].completed = true;
// }
// }
// }
// localStorage.setItem("tasks", JSON.stringify(tasks));
// Event: update storage when a task is completed
document.querySelectorAll('input[type="checkbox"]').forEach((box) => {
const tasks = Store.getTasks();
const taskList = document.querySelectorAll('.task');
box.addEventListener('change', () => {
// same on when user take action conditions are opposite
const nodes = Array.prototype.slice.call(taskList);
for (let i = 0; i < tasks.length; i += 1) {
const { completed } = tasks[i];
if (tasks[i].index === nodes.indexOf(box.parentElement.parentElement)) {
if (completed) {
box.removeAttribute('checked');
tasks[i].completed = false;
}
if (!completed) {
box.setAttribute('checked', '');
tasks[i].completed = true;
}
}
}
localStorage.setItem('tasks', JSON.stringify(tasks));

// //for reloading and updating everytime a task is completed //! Any better way?
// document.location.reload(true);
// });
// });
// });
// for reloading and updating everytime a task is completed //! Any better way?
document.location.reload(true);
});
});
});