Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions Sprint-3/todo-list/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>ToDo List</title>

<link rel="stylesheet" href="style.css" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

<script type="module" src="script.mjs"></script>
</head>

<body>
<div class="todo-container">
<h1>My ToDo List</h1>
Expand All @@ -18,23 +20,28 @@ <h1>My ToDo List</h1>
<button id="add-task-btn">Add</button>
</div>

<ul id="todo-list" class="todo-list">
</ul>
<ul id="todo-list" class="todo-list"></ul>

<!-- New button added for the exercise -->
<button id="delete-completed-btn">Delete completed tasks</button>

<!--
This is a template for the To-do list item.
It can simplify the creation of list item node in JS script.
-->
<!-- Template for todo items -->
<template id="todo-item-template">
<li class="todo-item"> <!-- include class "completed" if the task completed state is true -->
<li class="todo-item">
<span class="description">Task description</span>

<div class="actions">
<button class="complete-btn"><span class="fa-solid fa-check" aria-hidden="true"></span></button>
<button class="delete-btn"><span class="fa-solid fa-trash" aria-hidden="true"></span></button>
<button class="complete-btn">
<span class="fa-solid fa-check" aria-hidden="true"></span>
</button>

<button class="delete-btn">
<span class="fa-solid fa-trash" aria-hidden="true"></span>
</button>
</div>
</li>
</template>

</div>
</body>
</html>
</html>
27 changes: 18 additions & 9 deletions Sprint-3/todo-list/script.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Store everything imported from './todos.mjs' module as properties of an object named Todos
// Store everything imported from './todos.mjs' module as properties of an object named Todos
import * as Todos from "./todos.mjs";

// To store the todo tasks
Expand All @@ -8,19 +8,23 @@ const todos = [];
window.addEventListener("load", () => {
document.getElementById("add-task-btn").addEventListener("click", addNewTodo);

document
.getElementById("delete-completed-btn")
.addEventListener("click", deleteCompletedTodos);

// Populate sample data
Todos.addTask(todos, "Wash the dishes", false);
Todos.addTask(todos, "Wash the dishes", false);
Todos.addTask(todos, "Do the shopping", true);

render();
});


// A callback that reads the task description from an input field and
// A callback that reads the task description from an input field and
// append a new task to the todo list.
function addNewTodo() {
const taskInput = document.getElementById("new-task-input");
const task = taskInput.value.trim();

if (task) {
Todos.addTask(todos, task, false);
render();
Expand All @@ -29,6 +33,11 @@ function addNewTodo() {
taskInput.value = "";
}

function deleteCompletedTodos() {
Todos.deleteCompleted(todos);
render();
}

// Note:
// - Store the reference to the <ul> element with id "todo-list" here
// to avoid querying the DOM repeatedly inside render().
Expand All @@ -45,29 +54,29 @@ function render() {
});
}


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

// Create a <li> element for the given todo task
function createListItem(todo, index) {
const li = todoListItemTemplate.cloneNode(true); // true => Do a deep copy of the node

li.querySelector(".description").textContent = todo.task;

if (todo.completed) {
li.classList.add("completed");
}

li.querySelector('.complete-btn').addEventListener("click", () => {
li.querySelector(".complete-btn").addEventListener("click", () => {
Todos.toggleCompletedOnTask(todos, index);
render();
});
li.querySelector('.delete-btn').addEventListener("click", () => {

li.querySelector(".delete-btn").addEventListener("click", () => {
Todos.deleteTask(todos, index);
render();
});
Expand Down
9 changes: 9 additions & 0 deletions Sprint-3/todo-list/todos.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,13 @@ export function toggleCompletedOnTask(todos, taskIndex) {
if (todos[taskIndex]) {
todos[taskIndex].completed = !todos[taskIndex].completed;
}
}

// NEW FUNCTION
export function deleteCompleted(todos) {
for (let i = todos.length - 1; i >= 0; i--) {
if (todos[i].completed) {
todos.splice(i, 1);
}
}
}
45 changes: 35 additions & 10 deletions Sprint-3/todo-list/todos.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function createMockTodos() {
{ task: "Task 1 description", completed: true },
{ task: "Task 2 description", completed: false },
{ task: "Task 3 description", completed: true },
{ task: "Task 4 description", completed: false },
{ task: "Task 4 description", completed: false },
];
}

Expand All @@ -29,10 +29,10 @@ describe("addTask()", () => {
});

test("Should append a new task to the end of a ToDo list", () => {

const todos = createMockTodos();
const lengthBeforeAddition = todos.length;
Todos.addTask(todos, theTask.task, theTask.completed);

// todos should now have one more task
expect(todos).toHaveLength(lengthBeforeAddition + 1);

Expand All @@ -42,7 +42,6 @@ describe("addTask()", () => {
});

describe("deleteTask()", () => {

test("Delete the first task", () => {
const todos = createMockTodos();
const todosBeforeDeletion = createMockTodos();
Expand All @@ -53,7 +52,7 @@ describe("deleteTask()", () => {

expect(todos[0]).toEqual(todosBeforeDeletion[1]);
expect(todos[1]).toEqual(todosBeforeDeletion[2]);
expect(todos[2]).toEqual(todosBeforeDeletion[3]);
expect(todos[2]).toEqual(todosBeforeDeletion[3]);
});

test("Delete the second task (a middle task)", () => {
Expand All @@ -66,7 +65,7 @@ describe("deleteTask()", () => {

expect(todos[0]).toEqual(todosBeforeDeletion[0]);
expect(todos[1]).toEqual(todosBeforeDeletion[2]);
expect(todos[2]).toEqual(todosBeforeDeletion[3]);
expect(todos[2]).toEqual(todosBeforeDeletion[3]);
});

test("Delete the last task", () => {
Expand All @@ -79,7 +78,7 @@ describe("deleteTask()", () => {

expect(todos[0]).toEqual(todosBeforeDeletion[0]);
expect(todos[1]).toEqual(todosBeforeDeletion[1]);
expect(todos[2]).toEqual(todosBeforeDeletion[2]);
expect(todos[2]).toEqual(todosBeforeDeletion[2]);
});

test("Delete a non-existing task", () => {
Expand All @@ -94,11 +93,11 @@ describe("deleteTask()", () => {
});

describe("toggleCompletedOnTask()", () => {

test("Expect the 'completed' property to toggle on an existing task", () => {
const todos = createMockTodos();
const taskIndex = 1;
const completedStateBeforeToggle = todos[taskIndex].completed;

Todos.toggleCompletedOnTask(todos, taskIndex);
expect(todos[taskIndex].completed).toEqual(!completedStateBeforeToggle);

Expand All @@ -111,13 +110,12 @@ describe("toggleCompletedOnTask()", () => {
const todos = createMockTodos();
const todosBeforeToggle = createMockTodos();
Todos.toggleCompletedOnTask(todos, 1);
expect(todos[0]).toEqual(todosBeforeToggle[0]);

expect(todos[0]).toEqual(todosBeforeToggle[0]);
expect(todos[2]).toEqual(todosBeforeToggle[2]);
expect(todos[3]).toEqual(todosBeforeToggle[3]);
});


test("Expect no change when toggling on a non-existing task", () => {
const todos = createMockTodos();
const todosBeforeToggle = createMockTodos();
Expand All @@ -130,3 +128,30 @@ describe("toggleCompletedOnTask()", () => {
});
});

describe("deleteCompleted()", () => {
test("Delete all completed tasks", () => {
const todos = createMockTodos();

Todos.deleteCompleted(todos);

expect(todos).toEqual([
{ task: "Task 2 description", completed: false },
{ task: "Task 4 description", completed: false },
]);
});

test("Do nothing if there are no completed tasks", () => {
const todos = [
{ task: "Task A", completed: false },
{ task: "Task B", completed: false },
];

Todos.deleteCompleted(todos);

expect(todos).toEqual([
{ task: "Task A", completed: false },
{ task: "Task B", completed: false },
]);
});
});

Loading