Skip to content

Commit 6651c62

Browse files
fix: move clear completed logic to script.mjs
1 parent 2295841 commit 6651c62

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

Sprint-3/todo-list/index.html

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,29 @@ <h1>My ToDo List</h1>
1818
<button id="add-task-btn">Add</button>
1919
</div>
2020

21-
<ul id="todo-list" class="todo-list">
22-
</ul>
21+
<ul id="todo-list" class="todo-list"></ul>
22+
23+
<!-- ✅ NEW BUTTON -->
24+
<button id="delete-completed-btn">Delete completed tasks</button>
2325

2426
<!--
2527
This is a template for the To-do list item.
2628
It can simplify the creation of list item node in JS script.
2729
-->
2830
<template id="todo-item-template">
29-
<li class="todo-item"> <!-- include class "completed" if the task completed state is true -->
31+
<li class="todo-item">
3032
<span class="description">Task description</span>
3133
<div class="actions">
32-
<button class="complete-btn"><span class="fa-solid fa-check" aria-hidden="true"></span></button>
33-
<button class="delete-btn"><span class="fa-solid fa-trash" aria-hidden="true"></span></button>
34+
<button class="complete-btn">
35+
<span class="fa-solid fa-check" aria-hidden="true"></span>
36+
</button>
37+
<button class="delete-btn">
38+
<span class="fa-solid fa-trash" aria-hidden="true"></span>
39+
</button>
3440
</div>
3541
</li>
3642
</template>
3743

3844
</div>
3945
</body>
40-
</html>
46+
</html>

Sprint-3/todo-list/script.mjs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
1-
// Store everything imported from './todos.mjs' module as properties of an object named Todos
1+
// Store everything imported from './todos.mjs' module as properties of an object named Todos
22
import * as Todos from "./todos.mjs";
33

44
// To store the todo tasks
55
const todos = [];
66

7+
// Store DOM references after page load
8+
let todoListEl;
9+
let todoListItemTemplate;
10+
711
// Set up tasks to be performed once on page load
812
window.addEventListener("load", () => {
13+
todoListEl = document.getElementById("todo-list");
14+
todoListItemTemplate =
15+
document.getElementById("todo-item-template").content.firstElementChild;
16+
917
document.getElementById("add-task-btn").addEventListener("click", addNewTodo);
18+
document.getElementById("delete-completed-btn").addEventListener("click", removeCompletedTodos);
1019

1120
// Populate sample data
12-
Todos.addTask(todos, "Wash the dishes", false);
21+
Todos.addTask(todos, "Wash the dishes", false);
1322
Todos.addTask(todos, "Do the shopping", true);
1423

1524
render();
1625
});
1726

18-
19-
// A callback that reads the task description from an input field and
27+
// A callback that reads the task description from an input field and
2028
// append a new task to the todo list.
2129
function addNewTodo() {
2230
const taskInput = document.getElementById("new-task-input");
2331
const task = taskInput.value.trim();
32+
2433
if (task) {
2534
Todos.addTask(todos, task, false);
2635
render();
@@ -29,11 +38,11 @@ function addNewTodo() {
2938
taskInput.value = "";
3039
}
3140

32-
// Note:
33-
// - Store the reference to the <ul> element with id "todo-list" here
34-
// to avoid querying the DOM repeatedly inside render().
35-
// - This variable is declared here to be close to the only function that uses it.
36-
const todoListEl = document.getElementById("todo-list");
41+
// Remove all completed tasks
42+
function removeCompletedTodos() {
43+
Todos.deleteCompleted(todos);
44+
render();
45+
}
3746

3847
// Render the whole todo list
3948
function render() {
@@ -45,29 +54,22 @@ function render() {
4554
});
4655
}
4756

48-
49-
// Note:
50-
// - First child of #todo-item-template is a <li> element.
51-
// We will create each ToDo list item as a clone of this node.
52-
// - This variable is declared here to be close to the only function that uses it.
53-
const todoListItemTemplate =
54-
document.getElementById("todo-item-template").content.firstElementChild;
55-
5657
// Create a <li> element for the given todo task
5758
function createListItem(todo, index) {
58-
const li = todoListItemTemplate.cloneNode(true); // true => Do a deep copy of the node
59+
const li = todoListItemTemplate.cloneNode(true);
5960

6061
li.querySelector(".description").textContent = todo.task;
62+
6163
if (todo.completed) {
6264
li.classList.add("completed");
6365
}
6466

65-
li.querySelector('.complete-btn').addEventListener("click", () => {
67+
li.querySelector(".complete-btn").addEventListener("click", () => {
6668
Todos.toggleCompletedOnTask(todos, index);
6769
render();
6870
});
69-
70-
li.querySelector('.delete-btn').addEventListener("click", () => {
71+
72+
li.querySelector(".delete-btn").addEventListener("click", () => {
7173
Todos.deleteTask(todos, index);
7274
render();
7375
});

0 commit comments

Comments
 (0)