Skip to content

Commit 8f25d6d

Browse files
committed
to-do list
1 parent 4222cda commit 8f25d6d

4 files changed

Lines changed: 86 additions & 52 deletions

File tree

‎Sprint-3/todo-list/index.html‎

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,52 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8" />
5-
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
6-
<title>ToDo List</title>
7-
<link rel="stylesheet" href="style.css" />
8-
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>ToDo List</title>
7+
<link rel="stylesheet" href="style.css" />
8+
<link
9+
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
10+
rel="stylesheet"
11+
/>
912

10-
<script type="module" src="script.mjs"></script>
11-
</head>
12-
<body>
13-
<div class="todo-container">
14-
<h1>My ToDo List</h1>
13+
<script type="module" src="script.mjs"></script>
14+
</head>
15+
<body>
16+
<div class="todo-container">
17+
<h1>My ToDo List</h1>
1518

16-
<div class="todo-input">
17-
<input type="text" id="new-task-input" placeholder="Enter a new task..." />
18-
<button id="add-task-btn">Add</button>
19-
</div>
19+
<div class="todo-input">
20+
<input
21+
type="text"
22+
id="new-task-input"
23+
placeholder="Enter a new task..."
24+
/>
25+
<button id="add-task-btn">Add</button>
26+
</div>
27+
28+
<button id="delete-completed-btn">Delete completed tasks</button>
2029

21-
<ul id="todo-list" class="todo-list">
22-
</ul>
30+
<ul id="todo-list" class="todo-list"></ul>
2331

24-
<!--
32+
<!--
2533
This is a template for the To-do list item.
2634
It can simplify the creation of list item node in JS script.
2735
-->
28-
<template id="todo-item-template">
29-
<li class="todo-item"> <!-- include class "completed" if the task completed state is true -->
30-
<span class="description">Task description</span>
31-
<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-
</div>
35-
</li>
36-
</template>
37-
38-
</div>
39-
</body>
36+
<template id="todo-item-template">
37+
<li class="todo-item">
38+
<!-- include class "completed" if the task completed state is true -->
39+
<span class="description">Task description</span>
40+
<div class="actions">
41+
<button class="complete-btn">
42+
<span class="fa-solid fa-check" aria-hidden="true"></span>
43+
</button>
44+
<button class="delete-btn">
45+
<span class="fa-solid fa-trash" aria-hidden="true"></span>
46+
</button>
47+
</div>
48+
</li>
49+
</template>
50+
</div>
51+
</body>
4052
</html>

‎Sprint-3/todo-list/script.mjs‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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
@@ -8,15 +8,18 @@ const todos = [];
88
window.addEventListener("load", () => {
99
document.getElementById("add-task-btn").addEventListener("click", addNewTodo);
1010

11+
document
12+
.getElementById("delete-completed-btn")
13+
.addEventListener("click", deleteCompletedTodos);
14+
1115
// Populate sample data
12-
Todos.addTask(todos, "Wash the dishes", false);
16+
Todos.addTask(todos, "Wash the dishes", false);
1317
Todos.addTask(todos, "Do the shopping", true);
1418

1519
render();
1620
});
1721

18-
19-
// A callback that reads the task description from an input field and
22+
// A callback that reads the task description from an input field and
2023
// append a new task to the todo list.
2124
function addNewTodo() {
2225
const taskInput = document.getElementById("new-task-input");
@@ -29,6 +32,10 @@ function addNewTodo() {
2932
taskInput.value = "";
3033
}
3134

35+
function deleteCompletedTodos() {
36+
Todos.deleteCompleted(todos);
37+
render();
38+
}
3239
// Note:
3340
// - Store the reference to the <ul> element with id "todo-list" here
3441
// to avoid querying the DOM repeatedly inside render().
@@ -45,12 +52,11 @@ function render() {
4552
});
4653
}
4754

48-
4955
// Note:
5056
// - First child of #todo-item-template is a <li> element.
5157
// We will create each ToDo list item as a clone of this node.
5258
// - This variable is declared here to be close to the only function that uses it.
53-
const todoListItemTemplate =
59+
const todoListItemTemplate =
5460
document.getElementById("todo-item-template").content.firstElementChild;
5561

5662
// Create a <li> element for the given todo task
@@ -62,15 +68,15 @@ function createListItem(todo, index) {
6268
li.classList.add("completed");
6369
}
6470

65-
li.querySelector('.complete-btn').addEventListener("click", () => {
71+
li.querySelector(".complete-btn").addEventListener("click", () => {
6672
Todos.toggleCompletedOnTask(todos, index);
6773
render();
6874
});
69-
70-
li.querySelector('.delete-btn').addEventListener("click", () => {
75+
76+
li.querySelector(".delete-btn").addEventListener("click", () => {
7177
Todos.deleteTask(todos, index);
7278
render();
7379
});
7480

7581
return li;
76-
}
82+
}

‎Sprint-3/todo-list/todos.mjs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,12 @@ export function toggleCompletedOnTask(todos, taskIndex) {
2626
if (todos[taskIndex]) {
2727
todos[taskIndex].completed = !todos[taskIndex].completed;
2828
}
29-
}
29+
}
30+
31+
export function deleteCompleted(todoList) {
32+
for (let i = todoList.length - 1; i >= 0; i--) {
33+
if (todoList[i].completed) {
34+
todoList.splice(i, 1);
35+
}
36+
}
37+
}

‎Sprint-3/todo-list/todos.test.mjs‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function createMockTodos() {
1313
{ task: "Task 1 description", completed: true },
1414
{ task: "Task 2 description", completed: false },
1515
{ task: "Task 3 description", completed: true },
16-
{ task: "Task 4 description", completed: false },
16+
{ task: "Task 4 description", completed: false },
1717
];
1818
}
1919

@@ -29,7 +29,6 @@ describe("addTask()", () => {
2929
});
3030

3131
test("Should append a new task to the end of a ToDo list", () => {
32-
3332
const todos = createMockTodos();
3433
const lengthBeforeAddition = todos.length;
3534
Todos.addTask(todos, theTask.task, theTask.completed);
@@ -42,7 +41,6 @@ describe("addTask()", () => {
4241
});
4342

4443
describe("deleteTask()", () => {
45-
4644
test("Delete the first task", () => {
4745
const todos = createMockTodos();
4846
const todosBeforeDeletion = createMockTodos();
@@ -53,7 +51,7 @@ describe("deleteTask()", () => {
5351

5452
expect(todos[0]).toEqual(todosBeforeDeletion[1]);
5553
expect(todos[1]).toEqual(todosBeforeDeletion[2]);
56-
expect(todos[2]).toEqual(todosBeforeDeletion[3]);
54+
expect(todos[2]).toEqual(todosBeforeDeletion[3]);
5755
});
5856

5957
test("Delete the second task (a middle task)", () => {
@@ -66,7 +64,7 @@ describe("deleteTask()", () => {
6664

6765
expect(todos[0]).toEqual(todosBeforeDeletion[0]);
6866
expect(todos[1]).toEqual(todosBeforeDeletion[2]);
69-
expect(todos[2]).toEqual(todosBeforeDeletion[3]);
67+
expect(todos[2]).toEqual(todosBeforeDeletion[3]);
7068
});
7169

7270
test("Delete the last task", () => {
@@ -79,7 +77,7 @@ describe("deleteTask()", () => {
7977

8078
expect(todos[0]).toEqual(todosBeforeDeletion[0]);
8179
expect(todos[1]).toEqual(todosBeforeDeletion[1]);
82-
expect(todos[2]).toEqual(todosBeforeDeletion[2]);
80+
expect(todos[2]).toEqual(todosBeforeDeletion[2]);
8381
});
8482

8583
test("Delete a non-existing task", () => {
@@ -94,7 +92,6 @@ describe("deleteTask()", () => {
9492
});
9593

9694
describe("toggleCompletedOnTask()", () => {
97-
9895
test("Expect the 'completed' property to toggle on an existing task", () => {
9996
const todos = createMockTodos();
10097
const taskIndex = 1;
@@ -111,13 +108,12 @@ describe("toggleCompletedOnTask()", () => {
111108
const todos = createMockTodos();
112109
const todosBeforeToggle = createMockTodos();
113110
Todos.toggleCompletedOnTask(todos, 1);
114-
115-
expect(todos[0]).toEqual(todosBeforeToggle[0]);
111+
112+
expect(todos[0]).toEqual(todosBeforeToggle[0]);
116113
expect(todos[2]).toEqual(todosBeforeToggle[2]);
117114
expect(todos[3]).toEqual(todosBeforeToggle[3]);
118115
});
119116

120-
121117
test("Expect no change when toggling on a non-existing task", () => {
122118
const todos = createMockTodos();
123119
const todosBeforeToggle = createMockTodos();
@@ -130,3 +126,15 @@ describe("toggleCompletedOnTask()", () => {
130126
});
131127
});
132128

129+
describe("deleteCompleted()", () => {
130+
test("Deletes all completed tasks", () => {
131+
const todos = createMockTodos();
132+
133+
Todos.deleteCompleted(todos);
134+
135+
expect(todos).toEqual([
136+
{ task: "Task 2 description", completed: false },
137+
{ task: "Task 4 description", completed: false },
138+
]);
139+
});
140+
});

0 commit comments

Comments
 (0)