Skip to content

Commit 8354a9b

Browse files
committed
Refactor: completedTask function and added comment
1 parent fc21436 commit 8354a9b

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed

script.js

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,34 @@ const filterSelected = document.querySelector("#filter-list");
77
const clearAllTaskBtn = document.querySelector("#clearAllTaskBtn");
88
const taskCompletedIcon = "fa-solid fa-circle-check pendingSvg";
99
const taskUncompletedIcon = "fa-regular fa-circle pendingSvg";
10+
11+
// task counter for stating the number of pending tasks
1012
let taskCount = 3;
13+
14+
// this is message is to shown when there's no tasks inside container
1115
const noTasksMessage = `<div class="completed">
1216
<p>Add a task to plan your day.</p>
1317
</div>`;
1418

1519
// if user clicked on uncheck or check icon, format the task accordingly
1620
const completedTask = (task) => {
17-
1821
const uncheckIcon = task.querySelector("i.pendingSvg");
1922
const editIcon = task.querySelector("i.editSvg");
2023
const taskId = Number.parseInt(task.getAttribute("taskid"));
24+
const isTaskCompleted = uncheckIcon.className === taskUncompletedIcon;
2125

22-
if(uncheckIcon.className === taskUncompletedIcon)
23-
{
24-
// update icon, text and disable edit
25-
uncheckIcon.className = taskCompletedIcon;
26-
editIcon.style.display = "none";
27-
task.classList.add("checked");
28-
29-
// update task counts
30-
taskCount--;
31-
updateTaskCount();
26+
// Update icon, text, and edit display using a conditional operator
27+
uncheckIcon.className = isTaskCompleted ? taskCompletedIcon : taskUncompletedIcon;
28+
editIcon.style.display = isTaskCompleted ? "none" : "block";
29+
task.classList.toggle("checked", isTaskCompleted);
3230

33-
// update the localStorage data
34-
updateTaskStatus(true, taskId);
35-
}
36-
else
37-
{
38-
// update icon, text and enable edit
39-
uncheckIcon.className = taskUncompletedIcon;
40-
editIcon.style.display = "block";
41-
task.classList.remove("checked");
31+
// Update the localStorage data
32+
updateTaskStatus(isTaskCompleted, taskId);
4233

43-
// update task counts
44-
taskCount++;
45-
updateTaskCount();
46-
47-
// update the localStorage data
48-
updateTaskStatus(false, taskId);
49-
}
50-
}
34+
// Update task count
35+
taskCount += isTaskCompleted ? -1 : 1;
36+
updateTaskCount();
37+
};
5138

5239
// edit the task text when clicked on editIcon
5340
const editTaskText = (task) => {
@@ -140,7 +127,7 @@ taskContainer.addEventListener("click", (e) => {
140127
completedTask(taskElement);
141128
break;
142129
case 'task':
143-
// does nothing for now
130+
// do nothing :)
144131
break;
145132
case 'edit':
146133
editTaskText(taskElement);
@@ -175,7 +162,7 @@ const updateNoTaskMessage = () => {
175162

176163
// if there's nothing in task-container, show the message
177164
if(taskContainer.childElementCount === 0)
178-
taskContainer.setHTML(noTasksMessage);
165+
taskContainer.innerHTML = noTasksMessage;
179166
else if(taskCount === 1)
180167
{
181168
try {
@@ -184,7 +171,7 @@ const updateNoTaskMessage = () => {
184171
removeMessage.remove();
185172
}
186173
catch(error) {
187-
// do nothing :)
174+
console.log(`${error}`);
188175
}
189176
}
190177
}

0 commit comments

Comments
 (0)