Skip to content
Open
73 changes: 42 additions & 31 deletions Sprint-3/todo-list/index.html
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<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">
<head>
<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>
<script type="module" src="script.mjs"></script>
</head>
<body>
<div class="todo-container">
<h1>My ToDo List</h1>

<div class="todo-input">
<input type="text" id="new-task-input" placeholder="Enter a new task..." />
<button id="add-task-btn">Add</button>
</div>
<div class="todo-input">
<input
type="text"
id="new-task-input"
placeholder="Enter a new task..."
/>
<button id="add-task-btn">Add</button>
</div>

<ul id="todo-list" class="todo-list">
</ul>
<ul id="todo-list" class="todo-list"></ul>
<button id="delete-all-completed">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 id="todo-item-template">
<li class="todo-item"> <!-- include class "completed" if the task completed state is true -->
<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>
</div>
</li>
</template>

</div>
</body>
<template id="todo-item-template">
<li class="todo-item">
<!-- include class "completed" if the task completed state is true -->
<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>
</div>
</li>
</template>
</div>
</body>
</html>
24 changes: 14 additions & 10 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 @@ -9,14 +9,13 @@ window.addEventListener("load", () => {
document.getElementById("add-task-btn").addEventListener("click", addNewTodo);

// 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");
Expand Down Expand Up @@ -45,12 +44,11 @@ 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
Expand All @@ -62,15 +60,21 @@ function createListItem(todo, index) {
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();
});

return li;
}
}

const deleteAllCompleted = document.getElementById("delete-all-completed");
deleteAllCompleted.addEventListener("click", () => {
Todos.deleteCompleted(todos);
render();
});
12 changes: 11 additions & 1 deletion Sprint-3/todo-list/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ h1 {
.todo-input button {
padding: 10px 20px;
font-size: 16px;
background-color: #4CAF50;
background-color: #4caf50;
color: white;
border: none;
border-radius: 6px;
Expand Down Expand Up @@ -105,3 +105,13 @@ h1 {
text-decoration: line-through;
color: gray;
}

#delete-all-completed {
background-color: red;
padding: 10px 20px;
font-size: 16px;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
}
14 changes: 10 additions & 4 deletions Sprint-3/todo-list/todos.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@

// Append a new task to todos[]
export function addTask(todos, task, completed = false) {
todos.push({ task, completed });
todos.push({ task, completed }); //add one element to the array todos
}

// Delete todos[taskIndex] if it exists
export function deleteTask(todos, taskIndex) {
if (todos[taskIndex]) {
todos.splice(taskIndex, 1);
todos.splice(taskIndex, 1); //delete the selected element
}
}

// Toggle the "completed" property of todos[taskIndex] if the task exists.
export function toggleCompletedOnTask(todos, taskIndex) {
if (todos[taskIndex]) {
todos[taskIndex].completed = !todos[taskIndex].completed;
todos[taskIndex].completed = !todos[taskIndex].completed; // Toggle completed status between true and false
}
}
}

export function deleteCompleted(todoList) {
const remainingTasks = todoList.filter((todo) => !todo.completed);

todoList.splice(0, todoList.length, ...remainingTasks);
}
65 changes: 54 additions & 11 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,7 +29,6 @@ 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);
Expand All @@ -42,7 +41,6 @@ describe("addTask()", () => {
});

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

test("Delete the first task", () => {
const todos = createMockTodos();
const todosBeforeDeletion = createMockTodos();
Expand All @@ -53,7 +51,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 +64,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 +77,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 @@ -91,10 +89,57 @@ describe("deleteTask()", () => {
Todos.deleteTask(todos, -1);
expect(todos).toEqual(todosBeforeDeletion);
});

test("Delete all completed tasks", () => {
const todos = createMockTodos();
const todosBeforeDeletionCompleted = createMockTodos();
Todos.deleteCompleted(todos);
expect(todos).toHaveLength(2);
expect(todos[0]).toEqual(todosBeforeDeletionCompleted[1]);
expect(todos[1]).toEqual(todosBeforeDeletionCompleted[3]);
Comment thread
AkliluMihtsun marked this conversation as resolved.
Resolved
});
Comment thread
AkliluMihtsun marked this conversation as resolved.
Resolved
});

describe("toggleCompletedOnTask()", () => {
describe("deleteCompleted", () => {
test("Should remove completed tasks and keep only incomplete tasks", () => {
const todos = createMockTodos();
const todosBeforeDeletionCompleted = createMockTodos();

Todos.deleteCompleted(todos);

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

test("Should return an empty array when all tasks are completed", () => {
const todos = [
{ task: "Task 1 description", completed: true },
{ task: "Task 2 description", completed: true },
];

Todos.deleteCompleted(todos);

expect(todos).toHaveLength(0);
});

test("Should keep all tasks unchanged when none are completed", () => {
const todos = [
{ task: "Task 1 description", completed: false },
{ task: "Task 2 description", completed: false },
];

Todos.deleteCompleted(todos);

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

describe("toggleCompletedOnTask()", () => {
test("Expect the 'completed' property to toggle on an existing task", () => {
const todos = createMockTodos();
const taskIndex = 1;
Expand All @@ -111,13 +156,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 @@ -129,4 +173,3 @@ describe("toggleCompletedOnTask()", () => {
expect(todos).toEqual(todosBeforeToggle);
});
});

Loading