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
22import * as Todos from "./todos.mjs" ;
33
44// To store the todo tasks
@@ -8,25 +8,33 @@ const todos = [];
88window . 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.
2126function 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
5764function 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+ }
0 commit comments