Skip to content

Commit 5459639

Browse files
improve todo list functionality and add deadline calculation feature
1 parent cff19af commit 5459639

4 files changed

Lines changed: 69 additions & 19 deletions

File tree

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

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

10-
<script type="module" src="script.mjs"></script>
1111
</head>
12+
1213
<body>
1314
<div class="todo-container">
1415
<h1>My ToDo List</h1>
1516

1617
<div class="todo-input">
1718
<input type="text" id="new-task-input" placeholder="Enter a new task..." />
19+
<input type="date" id="new-task-deadline" />
1820
<button id="add-task-btn">Add</button>
1921
</div>
2022

21-
<ul id="todo-list" class="todo-list">
23+
<ul id="todo-list" class="todo-list">
2224
</ul>
2325

26+
27+
<button id="delete-completed-btn">Delete completed tasks</button>
2428
<!--
2529
This is a template for the To-do list item.
2630
It can simplify the creation of list item node in JS script.
@@ -36,5 +40,7 @@ <h1>My ToDo List</h1>
3640
</template>
3741

3842
</div>
43+
<script type="module" src="script.mjs"></script>
3944
</body>
40-
</html>
45+
46+
</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: 41 additions & 11 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,25 +8,33 @@ const todos = [];
88
window.addEventListener("load", () => {
99
document.getElementById("add-task-btn").addEventListener("click", addNewTodo);
1010

11+
// prettier-ignore
12+
document.getElementById("delete-completed-btn").addEventListener("click", () => {
13+
Todos.deleteCompleted(todos);
14+
render();
15+
});
16+
1117
// Populate sample data
12-
Todos.addTask(todos, "Wash the dishes", false);
18+
Todos.addTask(todos, "Wash the dishes", false);
1319
Todos.addTask(todos, "Do the shopping", true);
1420

1521
render();
1622
});
1723

18-
19-
// A callback that reads the task description from an input field and
24+
// A callback that reads the task description from an input field and
2025
// append a new task to the todo list.
2126
function addNewTodo() {
2227
const taskInput = document.getElementById("new-task-input");
28+
const deadlineInput = document.getElementById("new-task-deadline");
2329
const task = taskInput.value.trim();
30+
const deadline = deadlineInput.value || null;
2431
if (task) {
25-
Todos.addTask(todos, task, false);
32+
Todos.addTask(todos, task, false, deadline);
2633
render();
2734
}
2835

2936
taskInput.value = "";
37+
deadlineInput.value = "";
3038
}
3139

3240
// Note:
@@ -45,32 +53,54 @@ function render() {
4553
});
4654
}
4755

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

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

6067
li.querySelector(".description").textContent = todo.task;
68+
if (todo.deadline) {
69+
const today = new Date();
70+
const due = new Date(todo.deadline);
71+
72+
const diffMs = due - today;
73+
const diffDays = Math.ceil(diffMs / (1000 * 60 * 60 * 24));
74+
75+
let message = "";
76+
77+
if (diffDays > 0) {
78+
message = `${diffDays} days remaining`;
79+
} else if (diffDays === 0) {
80+
message = "Due today!";
81+
} else {
82+
message = `${Math.abs(diffDays)} days overdue`;
83+
}
84+
85+
const deadlineSpan = document.createElement("span");
86+
deadlineSpan.classList.add("deadline");
87+
deadlineSpan.textContent = message;
88+
li.appendChild(deadlineSpan);
89+
}
90+
6191
if (todo.completed) {
6292
li.classList.add("completed");
6393
}
6494

65-
li.querySelector('.complete-btn').addEventListener("click", () => {
95+
li.querySelector(".complete-btn").addEventListener("click", () => {
6696
Todos.toggleCompletedOnTask(todos, index);
6797
render();
6898
});
69-
70-
li.querySelector('.delete-btn').addEventListener("click", () => {
99+
100+
li.querySelector(".delete-btn").addEventListener("click", () => {
71101
Todos.deleteTask(todos, index);
72102
render();
73103
});
74104

75105
return li;
76-
}
106+
}

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@
1010
*/
1111

1212
// Append a new task to todos[]
13-
export function addTask(todos, task, completed = false) {
14-
todos.push({ task, completed });
13+
export function addTask(todos, task, completed = false, deadline) {
14+
const newTask = { task, completed };
15+
16+
if (deadline !== undefined) {
17+
newTask.deadline = deadline;
18+
}
19+
20+
todos.push(newTask);
1521
}
1622

1723
// Delete todos[taskIndex] if it exists
@@ -26,4 +32,12 @@ export function toggleCompletedOnTask(todos, taskIndex) {
2632
if (todos[taskIndex]) {
2733
todos[taskIndex].completed = !todos[taskIndex].completed;
2834
}
29-
}
35+
}
36+
// Delete all completed tasks
37+
export function deleteCompleted(todos) {
38+
for (let i = todos.length - 1; i >= 0; i--) {
39+
if (todos[i].completed) {
40+
todos.splice(i, 1);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)