-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
76 lines (67 loc) · 2.38 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const addTasks = document.querySelector(".add-tasks");
const tasksList = document.querySelector(".tasks");
const showPending = document.querySelector(".showPending");
const showCompleted = document.querySelector(".showCompleted");
const title = document.querySelector(".title");
const tasks = [];
// no arrow function because this is referenced inside
function addTask(e) {
e.preventDefault();
const text = this.querySelector('[name="task"]').value;
const color = this.querySelector('[name="color"]').value;
const id = Date.now();
const task = {
id,
text,
color,
completed: false,
};
tasks.push(task);
populateList();
this.reset();
}
function populateList(showPendingTasks = true) {
// set the title
title.innerHTML = showPendingTasks ? "Pending Tasks" : "Completed Tasks";
tasksList.innerHTML = tasks
.filter(task => {
// return where completed is false
if (showPendingTasks) return !task.completed;
else return task.completed; // return where completed is true
})
.map(task => {
return `
<li style="background:${task.color}">
<p>${task.text}</p>
${
showPendingTasks
? `<button id=${task.id} class="completed">✔</button>`
: ""
}
<button id=${task.id} class="remove">✖</button>
</li>`;
})
.join("");
}
function handleClick(e) {
// make sure the user has clicked the tick or cross button
if (!e.target.matches("button")) return;
// find out which button and list element was clicked
const action = e.target.classList.value;
const id = e.target.id;
const index = tasks.findIndex(task => task.id == id);
// console.log(id);
// take action
if (action === "completed") {
tasks[index].completed = !tasks[index].completed;
} else if (action === "remove") {
tasks.splice(index, 1);
}
// re render the list
populateList();
}
// event listeners
addTasks.addEventListener("submit", addTask);
tasksList.addEventListener("click", handleClick);
showPending.addEventListener("click", populateList);
showCompleted.addEventListener("click", () => populateList(false));