Skip to content

Commit df6a0cf

Browse files
committed
Complete todo list app
1 parent de6889c commit df6a0cf

4 files changed

Lines changed: 73 additions & 17 deletions

File tree

Sprint-3/todo-list/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ <h1>My ToDo List</h1>
1515

1616
<div class="todo-input">
1717
<input type="text" id="new-task-input" placeholder="Enter a new task..." />
18+
<input type="date" id="deadline-input"/>
1819
<button id="add-task-btn">Add</button>
1920
</div>
20-
21+
<button id="delete-completed-btn">Delete complete tasks</button>
2122
<ul id="todo-list" class="todo-list">
2223
</ul>
2324

Sprint-3/todo-list/script.mjs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,35 @@ 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-
10+
document
11+
.getElementById("delete-completed-btn")
12+
.addEventListener("click", deleteCompletedTodos);
1113
// Populate sample data
1214
Todos.addTask(todos, "Wash the dishes", false);
1315
Todos.addTask(todos, "Do the shopping", true);
1416

1517
render();
1618
});
1719

18-
20+
function deleteCompletedTodos() {
21+
Todos.deleteCompleted(todos);
22+
render();
23+
}
1924
// 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("deadline-input");
2329
const task = taskInput.value.trim();
30+
const deadline = deadlineInput.value;
2431
if (task) {
25-
Todos.addTask(todos, task, false);
32+
Todos.addTask(todos, task, false, deadline);
33+
console.log(todos);
2634
render();
2735
}
28-
2936
taskInput.value = "";
37+
deadlineInput.value = "";
3038
}
31-
3239
// Note:
3340
// - Store the reference to the <ul> element with id "todo-list" here
3441
// to avoid querying the DOM repeatedly inside render().
@@ -45,7 +52,6 @@ function render() {
4552
});
4653
}
4754

48-
4955
// Note:
5056
// - First child of #todo-item-template is a <li> element.
5157
// We will create each ToDo list item as a clone of this node.
@@ -56,8 +62,13 @@ const todoListItemTemplate =
5662
// Create a <li> element for the given todo task
5763
function createListItem(todo, index) {
5864
const li = todoListItemTemplate.cloneNode(true); // true => Do a deep copy of the node
59-
60-
li.querySelector(".description").textContent = todo.task;
65+
const description = li.querySelector(".description");
66+
if (todo.deadline) {
67+
description.textContent = `${todo.task} - Deadline: ${todo.deadline}`;
68+
}
69+
else {
70+
description.textContent = todo.task;
71+
}
6172
if (todo.completed) {
6273
li.classList.add("completed");
6374
}

Sprint-3/todo-list/style.css

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ h1 {
3838
border: 1px solid #ccc;
3939
}
4040

41+
#deadline-input {
42+
cursor: pointer;
43+
}
44+
4145
.todo-input button {
4246
padding: 10px 20px;
4347
font-size: 16px;
@@ -52,6 +56,22 @@ h1 {
5256
background-color: #45a049;
5357
}
5458

59+
/* Delete completed tasks button */
60+
#delete-completed-btn {
61+
padding: 10px 15px;
62+
margin-bottom: 20px;
63+
background-color: #e74c3c;
64+
color: white;
65+
border: none;
66+
border-radius: 6px;
67+
cursor: pointer;
68+
font-size: 14px;
69+
}
70+
71+
#delete-completed-btn:hover {
72+
background-color: #c0392b;
73+
}
74+
5575
.todo-list {
5676
list-style-type: none;
5777
padding-left: 0;
@@ -68,6 +88,10 @@ h1 {
6888
background-color: #fff;
6989
}
7090

91+
.todo-item:hover {
92+
background-color: #f9f9f9;
93+
}
94+
7195
.description {
7296
flex: 1;
7397
margin-right: 10px;
@@ -93,15 +117,24 @@ h1 {
93117
height: 32px;
94118
}
95119

96-
.complete-btn i {
120+
.actions button:hover {
121+
background-color: #eeeeee;
122+
border-radius: 5px;
123+
}
124+
125+
.complete-btn span {
97126
color: green;
98127
}
99128

100-
.delete-btn i {
129+
.delete-btn span {
101130
color: red;
102131
}
103132

133+
.todo-item.completed {
134+
background-color: #f5f5f5;
135+
}
136+
104137
.todo-item.completed .description {
105138
text-decoration: line-through;
106139
color: gray;
107-
}
140+
}

Sprint-3/todo-list/todos.mjs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,31 @@
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 todo = { task, completed };
15+
if (deadline) {
16+
todo.deadline = deadline;
17+
}
18+
todos.push(todo);
1519
}
16-
1720
// Delete todos[taskIndex] if it exists
1821
export function deleteTask(todos, taskIndex) {
1922
if (todos[taskIndex]) {
2023
todos.splice(taskIndex, 1);
2124
}
2225
}
23-
2426
// Toggle the "completed" property of todos[taskIndex] if the task exists.
2527
export function toggleCompletedOnTask(todos, taskIndex) {
2628
if (todos[taskIndex]) {
2729
todos[taskIndex].completed = !todos[taskIndex].completed;
2830
}
29-
}
31+
}
32+
//delete complete tasks
33+
export function deleteCompleted(todos) {
34+
for (let i = todos.length - 1; i >= 0; i--) {
35+
if (todos[i].completed) {
36+
todos.splice(i, 1);
37+
}
38+
}
39+
}
40+

0 commit comments

Comments
 (0)