-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
118 lines (87 loc) · 3.45 KB
/
scripts.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const inputElement = document.querySelector(".new-task-input");
const addTaskButton = document.querySelector(".btn-new-task");
const tasksContainer = document.querySelector(".tasks-container");
const validateInput = () => inputElement.value.trim().length > 0;
const handleAddTask = () => {
const inputIsValid = validateInput();
if (!inputIsValid) {
return inputElement.classList.add("error");
}
const taskItemContainer = document.createElement("div");
taskItemContainer.classList.add("task-item");
const taskContent = document.createElement("p");
taskContent.innerText = inputElement.value;
taskContent.addEventListener("click", () => handleClick(taskContent));
const deleteItem = document.createElement("i");
deleteItem.classList.add("fa-trash-alt");
deleteItem.classList.add("far");
deleteItem.classList.add("delete-task-button");
deleteItem.addEventListener("click", () =>
handleDeleteClick(taskItemContainer, taskContent)
);
taskItemContainer.appendChild(taskContent);
taskItemContainer.appendChild(deleteItem);
tasksContainer.appendChild(taskItemContainer);
inputElement.value = "";
updateLocalStorage();
};
const handleClick = (taskContent) => {
const tasks = tasksContainer.childNodes;
for (const task of tasks) {
const currentTaskIsBeingClicked = task.firstChild.isSameNode(taskContent);
if (currentTaskIsBeingClicked) {
task.firstChild.classList.toggle("completed");
}
}
updateLocalStorage();
};
const handleDeleteClick = (taskItemContainer, taskContent) => {
const tasks = tasksContainer.childNodes;
for (const task of tasks) {
const currentTaskIsBeingClicked = task.firstChild.isSameNode(taskContent);
if (currentTaskIsBeingClicked) {
taskItemContainer.remove();
}
}
updateLocalStorage();
};
const handleInputChange = () => {
const inputIsValid = validateInput();
if (inputIsValid) {
return inputElement.classList.remove("error");
}
};
const updateLocalStorage = () => {
const tasks = tasksContainer.childNodes;
const localStorageTasks = [...tasks].map((task) => {
const content = task.firstChild;
const isCompleted = content.classList.contains("completed");
return { description: content.innerText, isCompleted };
});
localStorage.setItem("tasks", JSON.stringify(localStorageTasks));
};
const refreshTasksUsingLocalStorage = () => {
const tasksFromLocalStorage = JSON.parse(localStorage.getItem("tasks"));
if (!tasksFromLocalStorage) return;
for (const task of tasksFromLocalStorage) {
const taskItemContainer = document.createElement("div");
taskItemContainer.classList.add("task-item");
const taskContent = document.createElement("p");
taskContent.innerText = task.description;
if (task.isCompleted) taskContent.classList.add("completed");
taskContent.addEventListener("click", () => handleClick(taskContent));
const deleteItem = document.createElement("i");
deleteItem.classList.add("fa-trash-alt");
deleteItem.classList.add("far");
deleteItem.classList.add("delete-task-button");
deleteItem.addEventListener("click", () =>
handleDeleteClick(taskItemContainer, taskContent)
);
taskItemContainer.appendChild(taskContent);
taskItemContainer.appendChild(deleteItem);
tasksContainer.appendChild(taskItemContainer);
}
};
refreshTasksUsingLocalStorage();
addTaskButton.addEventListener("click", () => handleAddTask());
inputElement.addEventListener("change", () => handleInputChange());