Skip to content

Commit b549937

Browse files
author
Enice-Codes
committed
completed my todolist
1 parent de6889c commit b549937

5 files changed

Lines changed: 125 additions & 34 deletions

File tree

‎Sprint-3/todo-list/02-guide_to_modularize_code.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ It focuses on:
3636
- ✅ What operations are needed to support data access and manipulation
3737

3838
### The UI Part of a Web App
39-
39+
.
4040
This is the part of the app that interacts with the user interface (UI).
4141

4242
It focuses on:

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
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..." />
18-
<button id="add-task-btn">Add</button>
19-
</div>
17+
<input type="text" id="new-task-input" placeholder="Enter a new task..." />
18+
<input type="date" id="new-task-deadline" />
19+
<button id="add-task-btn">Add</button>
20+
</div>
21+
22+
<button id="delete-completed-btn">Delete completed tasks</button>
2023

2124
<ul id="todo-list" class="todo-list">
2225
</ul>
@@ -26,15 +29,15 @@ <h1>My ToDo List</h1>
2629
It can simplify the creation of list item node in JS script.
2730
-->
2831
<template id="todo-item-template">
29-
<li class="todo-item"> <!-- include class "completed" if the task completed state is true -->
30-
<span class="description">Task description</span>
31-
<div class="actions">
32-
<button class="complete-btn"><span class="fa-solid fa-check" aria-hidden="true"></span></button>
33-
<button class="delete-btn"><span class="fa-solid fa-trash" aria-hidden="true"></span></button>
34-
</div>
35-
</li>
36-
</template>
32+
<li class="todo-item">
33+
<span class="description">Task description</span>
34+
<span class="deadline"></span>
35+
<div class="actions">
36+
<button class="complete-btn"><span class="fa-solid fa-check" aria-hidden="true"></span></button>
37+
<button class="delete-btn"><span class="fa-solid fa-trash" aria-hidden="true"></span></button>
38+
</div>
39+
</li>
40+
</template>
3741

3842
</div>
39-
</body>
40-
</html>
43+
</body>

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ 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-completed-btn").addEventListener("click", () => {
11+
Todos.deleteCompleted(todos);
12+
render();
13+
});
1014

1115
// Populate sample data
1216
Todos.addTask(todos, "Wash the dishes", false);
@@ -15,18 +19,19 @@ window.addEventListener("load", () => {
1519
render();
1620
});
1721

18-
1922
// A callback that reads the task description from an input field and
2023
// append a new task to the todo list.
2124
function addNewTodo() {
2225
const taskInput = document.getElementById("new-task-input");
26+
const deadlineInput = document.getElementById("new-task-deadline");
2327
const task = taskInput.value.trim();
28+
const deadline = deadlineInput.value || null;
2429
if (task) {
25-
Todos.addTask(todos, task, false);
30+
Todos.addTask(todos, task, false, deadline);
2631
render();
2732
}
28-
2933
taskInput.value = "";
34+
deadlineInput.value = "";
3035
}
3136

3237
// Note:
@@ -45,7 +50,6 @@ function render() {
4550
});
4651
}
4752

48-
4953
// Note:
5054
// - First child of #todo-item-template is a <li> element.
5155
// We will create each ToDo list item as a clone of this node.
@@ -58,6 +62,9 @@ function createListItem(todo, index) {
5862
const li = todoListItemTemplate.cloneNode(true); // true => Do a deep copy of the node
5963

6064
li.querySelector(".description").textContent = todo.task;
65+
if (todo.deadline) {
66+
li.querySelector(".deadline").textContent = Todos.getDeadlineStatus(todo.deadline);
67+
}
6168
if (todo.completed) {
6269
li.classList.add("completed");
6370
}

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,44 @@
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 = null) {
14+
todos.push({ task, completed, deadline });
1515
}
1616

17-
// Delete todos[taskIndex] if it exists
1817
export function deleteTask(todos, taskIndex) {
1918
if (todos[taskIndex]) {
2019
todos.splice(taskIndex, 1);
2120
}
2221
}
2322

24-
// Toggle the "completed" property of todos[taskIndex] if the task exists.
2523
export function toggleCompletedOnTask(todos, taskIndex) {
2624
if (todos[taskIndex]) {
2725
todos[taskIndex].completed = !todos[taskIndex].completed;
2826
}
27+
}
28+
29+
export function deleteCompleted(todos) {
30+
for (let i = todos.length - 1; i >= 0; i--) {
31+
if (todos[i].completed) {
32+
todos.splice(i, 1);
33+
}
34+
}
35+
}
36+
// Return a human-readable string describing time left until deadline.
37+
// Returns null if there's no deadline.
38+
export function getDeadlineStatus(deadline) {
39+
if (!deadline) return null;
40+
41+
const today = new Date();
42+
today.setHours(0, 0, 0, 0);
43+
44+
const dueDate = new Date(deadline);
45+
dueDate.setHours(0, 0, 0, 0);
46+
47+
const msPerDay = 1000 * 60 * 60 * 24;
48+
const daysLeft = Math.round((dueDate - today) / msPerDay);
49+
50+
if (daysLeft > 0) return `Due in ${daysLeft} day${daysLeft === 1 ? "" : "s"}`;
51+
if (daysLeft === 0) return "Due today";
52+
return `Overdue by ${Math.abs(daysLeft)} day${Math.abs(daysLeft) === 1 ? "" : "s"}`;
2953
}

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

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function createMockTodos() {
1818
}
1919

2020
// A mock task to simulate user input
21-
const theTask = { task: "The Task", completed: false };
21+
const theTask = { task: "The Task", completed: false, deadline: null };
2222

2323
describe("addTask()", () => {
2424
test("Add a task to an empty ToDo list", () => {
@@ -29,28 +29,22 @@ 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);
36-
// todos should now have one more task
3735
expect(todos).toHaveLength(lengthBeforeAddition + 1);
38-
39-
// New task should be appended to the todos
4036
expect(todos[todos.length - 1]).toEqual(theTask);
4137
});
4238
});
4339

4440
describe("deleteTask()", () => {
45-
4641
test("Delete the first task", () => {
4742
const todos = createMockTodos();
4843
const todosBeforeDeletion = createMockTodos();
4944
const lengthBeforeDeletion = todos.length;
5045
Todos.deleteTask(todos, 0);
5146

5247
expect(todos).toHaveLength(lengthBeforeDeletion - 1);
53-
5448
expect(todos[0]).toEqual(todosBeforeDeletion[1]);
5549
expect(todos[1]).toEqual(todosBeforeDeletion[2]);
5650
expect(todos[2]).toEqual(todosBeforeDeletion[3]);
@@ -63,7 +57,6 @@ describe("deleteTask()", () => {
6357
Todos.deleteTask(todos, 1);
6458

6559
expect(todos).toHaveLength(lengthBeforeDeletion - 1);
66-
6760
expect(todos[0]).toEqual(todosBeforeDeletion[0]);
6861
expect(todos[1]).toEqual(todosBeforeDeletion[2]);
6962
expect(todos[2]).toEqual(todosBeforeDeletion[3]);
@@ -76,7 +69,6 @@ describe("deleteTask()", () => {
7669
Todos.deleteTask(todos, todos.length - 1);
7770

7871
expect(todos).toHaveLength(lengthBeforeDeletion - 1);
79-
8072
expect(todos[0]).toEqual(todosBeforeDeletion[0]);
8173
expect(todos[1]).toEqual(todosBeforeDeletion[1]);
8274
expect(todos[2]).toEqual(todosBeforeDeletion[2]);
@@ -94,15 +86,13 @@ describe("deleteTask()", () => {
9486
});
9587

9688
describe("toggleCompletedOnTask()", () => {
97-
9889
test("Expect the 'completed' property to toggle on an existing task", () => {
9990
const todos = createMockTodos();
10091
const taskIndex = 1;
10192
const completedStateBeforeToggle = todos[taskIndex].completed;
10293
Todos.toggleCompletedOnTask(todos, taskIndex);
10394
expect(todos[taskIndex].completed).toEqual(!completedStateBeforeToggle);
10495

105-
// Toggle again
10696
Todos.toggleCompletedOnTask(todos, taskIndex);
10797
expect(todos[taskIndex].completed).toEqual(completedStateBeforeToggle);
10898
});
@@ -117,7 +107,6 @@ describe("toggleCompletedOnTask()", () => {
117107
expect(todos[3]).toEqual(todosBeforeToggle[3]);
118108
});
119109

120-
121110
test("Expect no change when toggling on a non-existing task", () => {
122111
const todos = createMockTodos();
123112
const todosBeforeToggle = createMockTodos();
@@ -130,3 +119,71 @@ describe("toggleCompletedOnTask()", () => {
130119
});
131120
});
132121

122+
describe("deleteCompleted()", () => {
123+
test("Should remove all completed tasks, keeping incomplete ones in order", () => {
124+
const todos = createMockTodos();
125+
const todosBeforeDeletion = createMockTodos();
126+
Todos.deleteCompleted(todos);
127+
128+
expect(todos).toHaveLength(2);
129+
expect(todos[0]).toEqual(todosBeforeDeletion[1]);
130+
expect(todos[1]).toEqual(todosBeforeDeletion[3]);
131+
});
132+
133+
test("Should result in an empty list if all tasks are completed", () => {
134+
const todos = [
135+
{ task: "Task A", completed: true },
136+
{ task: "Task B", completed: true },
137+
];
138+
Todos.deleteCompleted(todos);
139+
expect(todos).toHaveLength(0);
140+
});
141+
142+
test("Should leave the list unchanged if no tasks are completed", () => {
143+
const todos = [
144+
{ task: "Task A", completed: false },
145+
{ task: "Task B", completed: false },
146+
];
147+
const todosBeforeDeletion = [...todos];
148+
Todos.deleteCompleted(todos);
149+
expect(todos).toEqual(todosBeforeDeletion);
150+
});
151+
152+
test("Should do nothing on an empty ToDo list", () => {
153+
const todos = [];
154+
Todos.deleteCompleted(todos);
155+
expect(todos).toEqual([]);
156+
});
157+
});
158+
159+
describe("getDeadlineStatus()", () => {
160+
test("Should return null when there is no deadline", () => {
161+
expect(Todos.getDeadlineStatus(null)).toBeNull();
162+
});
163+
164+
test("Should return 'Due today' when deadline is today", () => {
165+
const today = new Date().toISOString().split("T")[0];
166+
expect(Todos.getDeadlineStatus(today)).toBe("Due today");
167+
});
168+
169+
test("Should return days remaining for a future deadline", () => {
170+
const future = new Date();
171+
future.setDate(future.getDate() + 3);
172+
const futureStr = future.toISOString().split("T")[0];
173+
expect(Todos.getDeadlineStatus(futureStr)).toBe("Due in 3 days");
174+
});
175+
176+
test("Should return singular 'day' when exactly 1 day left", () => {
177+
const tomorrow = new Date();
178+
tomorrow.setDate(tomorrow.getDate() + 1);
179+
const tomorrowStr = tomorrow.toISOString().split("T")[0];
180+
expect(Todos.getDeadlineStatus(tomorrowStr)).toBe("Due in 1 day");
181+
});
182+
183+
test("Should return overdue message for a past deadline", () => {
184+
const past = new Date();
185+
past.setDate(past.getDate() - 2);
186+
const pastStr = past.toISOString().split("T")[0];
187+
expect(Todos.getDeadlineStatus(pastStr)).toBe("Overdue by 2 days");
188+
});
189+
});

0 commit comments

Comments
 (0)