Skip to content

Commit 868c92b

Browse files
committed
completed mandatory task forr to do list.
1 parent 4222cda commit 868c92b

5 files changed

Lines changed: 66 additions & 25 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<h1>My ToDo List</h1>
1515

1616
<div class="todo-input">
17-
<input type="text" id="new-task-input" placeholder="Enter a new task..." />
17+
<label for="new-task-input"></label><input type="text" id="new-task-input" placeholder="Enter a new task..." />
1818
<button id="add-task-btn">Add</button>
1919
</div>
2020

@@ -34,7 +34,7 @@ <h1>My ToDo List</h1>
3434
</div>
3535
</li>
3636
</template>
37-
37+
<button id="delete-btn">Delete completed tasks</button>
3838
</div>
3939
</body>
4040
</html>

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

Lines changed: 13 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
@@ -7,16 +7,20 @@ const todos = [];
77
// Set up tasks to be performed once on page load
88
window.addEventListener("load", () => {
99
document.getElementById("add-task-btn").addEventListener("click", addNewTodo);
10+
document.getElementById("delete-btn").addEventListener("click", (e) => {
11+
console.log("Delete button clicked");
12+
Todos.deleteCompleted(todos);
13+
render();
14+
});
1015

1116
// Populate sample data
12-
Todos.addTask(todos, "Wash the dishes", false);
17+
Todos.addTask(todos, "Wash the dishes", false);
1318
Todos.addTask(todos, "Do the shopping", true);
1419

1520
render();
1621
});
1722

18-
19-
// A callback that reads the task description from an input field and
23+
// A callback that reads the task description from an input field and
2024
// append a new task to the todo list.
2125
function addNewTodo() {
2226
const taskInput = document.getElementById("new-task-input");
@@ -45,12 +49,11 @@ function render() {
4549
});
4650
}
4751

48-
4952
// Note:
5053
// - First child of #todo-item-template is a <li> element.
5154
// We will create each ToDo list item as a clone of this node.
5255
// - This variable is declared here to be close to the only function that uses it.
53-
const todoListItemTemplate =
56+
const todoListItemTemplate =
5457
document.getElementById("todo-item-template").content.firstElementChild;
5558

5659
// Create a <li> element for the given todo task
@@ -62,15 +65,15 @@ function createListItem(todo, index) {
6265
li.classList.add("completed");
6366
}
6467

65-
li.querySelector('.complete-btn').addEventListener("click", () => {
68+
li.querySelector(".complete-btn").addEventListener("click", () => {
6669
Todos.toggleCompletedOnTask(todos, index);
6770
render();
6871
});
69-
70-
li.querySelector('.delete-btn').addEventListener("click", () => {
72+
73+
li.querySelector(".delete-btn").addEventListener("click", () => {
7174
Todos.deleteTask(todos, index);
7275
render();
7376
});
7477

7578
return li;
76-
}
79+
}

‎Sprint-3/todo-list/style.css‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,31 @@ h1 {
105105
text-decoration: line-through;
106106
color: gray;
107107
}
108+
#delete-btn {
109+
background-color: #e53935;
110+
color: white;
111+
border: none;
112+
padding: 12px 20px;
113+
font-size: 1rem;
114+
font-weight: 600;
115+
border-radius: 8px;
116+
cursor: pointer;
117+
transition: background-color 0.2s ease, transform 0.2s ease;
118+
}
119+
120+
#delete-btn:hover {
121+
background-color: #c62828;
122+
}
123+
124+
#delete-btn:active {
125+
transform: scale(0.97);
126+
}
127+
128+
#delete-btn:focus {
129+
outline: 2px solid #ff8a80;
130+
outline-offset: 3px;
131+
}
132+
#delete-btn {
133+
display: block;
134+
margin: 20px auto;
135+
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
A ToDo List (todos) is expected to be represented as an array of objects in
2+
A ToDo List (todos) is expected to be represented as an array of objects in
33
the following manner:
44
55
[
@@ -26,4 +26,9 @@ export function toggleCompletedOnTask(todos, taskIndex) {
2626
if (todos[taskIndex]) {
2727
todos[taskIndex].completed = !todos[taskIndex].completed;
2828
}
29-
}
29+
} // Delete completed todos
30+
export function deleteCompleted(todoList) {
31+
const incompleteTodos = todoList.filter((todo) => !todo.completed);
32+
todoList.length = 0; //empty the original array
33+
todoList.push(...incompleteTodos); //push incompleted task
34+
}

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

Lines changed: 16 additions & 11 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", () => {
@@ -92,9 +90,18 @@ describe("deleteTask()", () => {
9290
expect(todos).toEqual(todosBeforeDeletion);
9391
});
9492
});
93+
test("Should delete all completed tasks", () => {
94+
const todos = createMockTodos();
95+
const incompleteTasks = Todos.deleteCompleted(todos);
96+
expect(incompleteTasks.length).toBe(2);
97+
incompleteTasks.forEach((incompleteTask) => {
98+
expect(incompleteTask.completed).toBe(false);
99+
});
100+
expect(incompleteTasks[0].task).toBe("Task 2 description");
101+
expect(incompleteTasks[1].task).toBe("Task 4 description");
102+
});
95103

96104
describe("toggleCompletedOnTask()", () => {
97-
98105
test("Expect the 'completed' property to toggle on an existing task", () => {
99106
const todos = createMockTodos();
100107
const taskIndex = 1;
@@ -111,13 +118,12 @@ describe("toggleCompletedOnTask()", () => {
111118
const todos = createMockTodos();
112119
const todosBeforeToggle = createMockTodos();
113120
Todos.toggleCompletedOnTask(todos, 1);
114-
115-
expect(todos[0]).toEqual(todosBeforeToggle[0]);
121+
122+
expect(todos[0]).toEqual(todosBeforeToggle[0]);
116123
expect(todos[2]).toEqual(todosBeforeToggle[2]);
117124
expect(todos[3]).toEqual(todosBeforeToggle[3]);
118125
});
119126

120-
121127
test("Expect no change when toggling on a non-existing task", () => {
122128
const todos = createMockTodos();
123129
const todosBeforeToggle = createMockTodos();
@@ -129,4 +135,3 @@ describe("toggleCompletedOnTask()", () => {
129135
expect(todos).toEqual(todosBeforeToggle);
130136
});
131137
});
132-

0 commit comments

Comments
 (0)