2
2
const todoInput = document . querySelector ( '.todo-input' ) ;
3
3
const todoButton = document . querySelector ( '.todo-button' ) ;
4
4
const todoList = document . querySelector ( '.todo-list' ) ;
5
+ const filterOption = document . querySelector ( '.filter-todo' ) ;
5
6
6
7
// Event Listeners
8
+ document . addEventListener ( 'DOMContentLoaded' , getTodos ) ;
7
9
todoButton . addEventListener ( 'click' , addTodo ) ;
8
10
todoList . addEventListener ( 'click' , deleteCheck ) ;
11
+ filterOption . addEventListener ( 'change' , filterTodo ) ; // Changed from 'click' to 'change'
9
12
10
13
// Functions
11
14
function addTodo ( event ) {
@@ -21,6 +24,9 @@ function addTodo(event) {
21
24
newToDo . innerText = todoInput . value ;
22
25
todoDiv . appendChild ( newToDo ) ;
23
26
27
+ //add todo to local storage
28
+ saveLocalTodos ( todoInput . value ) ;
29
+
24
30
// Check Mark button
25
31
const completedButton = document . createElement ( 'button' ) ;
26
32
completedButton . innerHTML = '<i class="fas fa-check"></i>' ;
@@ -49,7 +55,7 @@ function deleteCheck(e){
49
55
50
56
//fallout animation
51
57
todo . classList . add ( 'fall' ) ;
52
-
58
+ removeLocalTodos ( todo ) ;
53
59
//wait for a brief moment before removing the todo
54
60
setTimeout ( function ( ) {
55
61
todo . remove ( ) ;
@@ -62,3 +68,89 @@ function deleteCheck(e){
62
68
todo . classList . toggle ( 'completed' ) ;
63
69
}
64
70
}
71
+
72
+ function filterTodo ( e ) {
73
+ const todos = todoList . childNodes ;
74
+ todos . forEach ( function ( todo ) {
75
+ switch ( e . target . value ) {
76
+ case "all" :
77
+ todo . style . display = 'flex' ;
78
+ break ;
79
+ case "completed" :
80
+ if ( todo . classList . contains ( 'completed' ) ) {
81
+ todo . style . display = 'flex' ;
82
+ } else {
83
+ todo . style . display = 'none' ;
84
+ }
85
+ break ;
86
+ case "uncompleted" :
87
+ if ( ! todo . classList . contains ( 'completed' ) ) {
88
+ todo . style . display = 'flex' ;
89
+ } else {
90
+ todo . style . display = 'none' ;
91
+ }
92
+ break ;
93
+ }
94
+ } ) ;
95
+ }
96
+
97
+ function saveLocalTodos ( todo ) {
98
+ let todos ;
99
+ if ( localStorage . getItem ( 'todos' ) === null ) {
100
+ todos = [ ] ;
101
+ }
102
+ else {
103
+ todos = JSON . parse ( localStorage . getItem ( 'todos' ) ) ; // assign the parsed data back to todos
104
+ }
105
+ todos . push ( todo ) ;
106
+ localStorage . setItem ( 'todos' , JSON . stringify ( todos ) ) ;
107
+ }
108
+
109
+ function getTodos ( ) {
110
+ let todos ;
111
+ if ( localStorage . getItem ( 'todos' ) === null ) {
112
+ todos = [ ] ;
113
+ }
114
+ else {
115
+ todos = JSON . parse ( localStorage . getItem ( 'todos' ) ) ;
116
+ }
117
+ todos . forEach ( function ( todoItem ) { // rename the parameter to todoItem
118
+ // Create a div in HTML
119
+ const todoDiv = document . createElement ( 'div' ) ;
120
+ todoDiv . classList . add ( 'todo' ) ;
121
+
122
+ // Now adding LI
123
+ const newToDo = document . createElement ( 'li' ) ;
124
+ newToDo . classList . add ( 'todo-item' ) ;
125
+ newToDo . innerText = todoItem ; // use todoItem here
126
+ todoDiv . appendChild ( newToDo ) ;
127
+
128
+ // Check Mark button
129
+ const completedButton = document . createElement ( 'button' ) ;
130
+ completedButton . innerHTML = '<i class="fas fa-check"></i>' ;
131
+ completedButton . classList . add ( 'complete-btn' ) ;
132
+ todoDiv . appendChild ( completedButton ) ;
133
+
134
+ // Trash button
135
+ const trashButton = document . createElement ( 'button' ) ;
136
+ trashButton . innerHTML = '<i class="fas fa-trash"></i>' ;
137
+ trashButton . classList . add ( 'trash-btn' ) ;
138
+ todoDiv . appendChild ( trashButton ) ;
139
+
140
+ // Append to List
141
+ todoList . appendChild ( todoDiv ) ;
142
+ } ) ;
143
+ }
144
+
145
+ function removeLocalTodos ( todo ) {
146
+ let todos ;
147
+ if ( localStorage . getItem ( 'todos' ) === null ) {
148
+ todos = [ ] ;
149
+ }
150
+ else {
151
+ todos = JSON . parse ( localStorage . getItem ( 'todos' ) ) ;
152
+ }
153
+ const todoIndex = todo . children [ 0 ] . innerText ;
154
+ todos . splice ( todos . indexOf ( todoIndex ) , 1 ) ;
155
+ localStorage . setItem ( 'todos' , JSON . stringify ( todos ) ) ;
156
+ }
0 commit comments