Skip to content

Commit c448b9d

Browse files
Complete feature/sprint-3-todo-list
1 parent cfd674c commit c448b9d

File tree

6 files changed

+103
-54
lines changed

6 files changed

+103
-54
lines changed

Sprint-3/todo-list/index.html

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,55 @@
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>
2027

21-
<ul id="todo-list" class="todo-list">
22-
</ul>
28+
<ul id="todo-list" class="todo-list"></ul>
2329

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

Sprint-3/todo-list/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "module",
77
"scripts": {
88
"serve": "http-server",
9-
"test": "NODE_OPTIONS=--experimental-vm-modules jest"
9+
"test": "set NODE_OPTIONS=--experimental-vm-modules && jest"
1010
},
1111
"repository": {
1212
"type": "git",

Sprint-3/todo-list/script.mjs

Lines changed: 15 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
@@ -9,14 +9,13 @@ window.addEventListener("load", () => {
99
document.getElementById("add-task-btn").addEventListener("click", addNewTodo);
1010

1111
// Populate sample data
12-
Todos.addTask(todos, "Wash the dishes", false);
12+
Todos.addTask(todos, "Wash the dishes", false);
1313
Todos.addTask(todos, "Do the shopping", true);
1414

1515
render();
1616
});
1717

18-
19-
// A callback that reads the task description from an input field and
18+
// A callback that reads the task description from an input field and
2019
// append a new task to the todo list.
2120
function addNewTodo() {
2221
const taskInput = document.getElementById("new-task-input");
@@ -45,12 +44,11 @@ function render() {
4544
});
4645
}
4746

48-
4947
// Note:
5048
// - First child of #todo-item-template is a <li> element.
5149
// We will create each ToDo list item as a clone of this node.
5250
// - This variable is declared here to be close to the only function that uses it.
53-
const todoListItemTemplate =
51+
const todoListItemTemplate =
5452
document.getElementById("todo-item-template").content.firstElementChild;
5553

5654
// Create a <li> element for the given todo task
@@ -62,15 +60,22 @@ function createListItem(todo, index) {
6260
li.classList.add("completed");
6361
}
6462

65-
li.querySelector('.complete-btn').addEventListener("click", () => {
63+
li.querySelector(".complete-btn").addEventListener("click", () => {
6664
Todos.toggleCompletedOnTask(todos, index);
6765
render();
6866
});
69-
70-
li.querySelector('.delete-btn').addEventListener("click", () => {
67+
68+
li.querySelector(".delete-btn").addEventListener("click", () => {
7169
Todos.deleteTask(todos, index);
7270
render();
7371
});
7472

7573
return li;
76-
}
74+
}
75+
76+
document
77+
.getElementById("delete-completed-btn")
78+
.addEventListener("click", () => {
79+
Todos.deleteCompleted(todos);
80+
render();
81+
});

Sprint-3/todo-list/style.css

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ h1 {
4141
.todo-input button {
4242
padding: 10px 20px;
4343
font-size: 16px;
44-
background-color: #4CAF50;
44+
background-color: #4caf50;
4545
color: white;
4646
border: none;
4747
border-radius: 6px;
@@ -105,3 +105,17 @@ h1 {
105105
text-decoration: line-through;
106106
color: gray;
107107
}
108+
109+
.delete-completed-btn {
110+
padding: 10px 20px;
111+
font-size: 16px;
112+
background-color: #e10526;
113+
color: white;
114+
border: none;
115+
border-radius: 6px;
116+
cursor: pointer;
117+
}
118+
119+
.delete-completed-btn:hover {
120+
background-color: #be0521;
121+
}

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+
// Delete all the completed tasks from todoList
32+
33+
export function deleteCompleted(todoList) {
34+
for (let i = todoList.length - 1; i >= 0; i--) {
35+
if (todoList[i].completed) todoList.splice(i, 1);
36+
}
37+
}

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

Lines changed: 17 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,14 @@ describe("toggleCompletedOnTask()", () => {
130126
});
131127
});
132128

129+
describe("deleteCompleted()", () => {
130+
test("Delete the completed tasks", () => {
131+
const todos = createMockTodos();
132+
const todosAfterDeletion = createMockTodos().filter(
133+
(todo) => !todo.completed
134+
);
135+
Todos.deleteCompleted(todos);
136+
expect(todos).toHaveLength(2);
137+
expect(todos).toEqual(todosAfterDeletion);
138+
});
139+
});

0 commit comments

Comments
 (0)