1
1
const formAdd = document . querySelector ( "#add-form" ) ;
2
2
const formEdit = document . querySelector ( "#edit-form" ) ;
3
- const tasksList = document . querySelector ( ".tasksList-listTasks " ) ;
3
+ const tasksList = document . querySelector ( ".todoapp__tasks-list " ) ;
4
4
const textBoxAdd = document . querySelector ( "#todoapp-addTask" ) ;
5
5
const textBoxEdit = document . querySelector ( "#todoapp-editTask" ) ;
6
6
const btnAdd = document . querySelector ( "#button-Add" ) ;
@@ -9,14 +9,13 @@ let toDoStorage = [];
9
9
10
10
let oldInputValue ;
11
11
12
- // LOCAL STORAGE
13
-
14
- if ( localStorage . getItem ( "arrayTasks" ) == "" ) {
15
- localStorage . removeItem ( "arrayTasks" ) ;
12
+ if ( ! localStorage . getItem ( "arrayTasks" ) ) {
13
+ localStorage . setItem ( "arrayTasks" , JSON . stringify ( [ ] ) ) ;
16
14
}
17
15
18
16
document . addEventListener ( "DOMContentLoaded" , ( ) => {
19
- toDoStorage = JSON . parse ( localStorage . getItem ( "arrayTasks" ) ) ;
17
+ const storedTasks = localStorage . getItem ( "arrayTasks" ) ;
18
+ toDoStorage = storedTasks ? JSON . parse ( storedTasks ) : [ ] ;
20
19
21
20
toDoStorage . forEach ( ( element ) => {
22
21
saveToDo ( element ) ;
@@ -28,88 +27,77 @@ document.addEventListener("DOMContentLoaded", () => {
28
27
document . addEventListener ( "click" , ( e ) => {
29
28
const targetEl = e . target ;
30
29
const parentEl = targetEl . closest ( "li" ) ;
31
- const pEl = targetEl . closest ( "div" ) . querySelector ( "p" ) ;
30
+ const parentDiv = targetEl . closest ( "div" ) ;
31
+ const pEl = parentDiv ? parentDiv . querySelector ( "p" ) : null ;
32
32
let taskTitle ;
33
33
34
34
if ( parentEl && parentEl . querySelector ( "p" ) ) {
35
35
taskTitle = parentEl . querySelector ( "p" ) . innerHTML ;
36
36
}
37
37
38
- if ( targetEl . classList . contains ( "tasksList-taskCheck " ) ) {
38
+ if ( targetEl . classList . contains ( "todoapp__task-check " ) ) {
39
39
targetEl . querySelector ( "i" ) . classList . toggle ( "fa-check" ) ;
40
40
pEl . classList . toggle ( "done" ) ;
41
41
42
42
toDoStorage . forEach ( ( element ) => {
43
- if ( element . taskTitle == pEl . innerHTML ) {
43
+ if ( element . taskTitle === pEl . innerHTML ) {
44
44
element . isChecked = ! element . isChecked ;
45
45
}
46
46
} ) ;
47
47
localStorage . setItem ( "arrayTasks" , JSON . stringify ( toDoStorage ) ) ;
48
48
}
49
49
50
- if ( targetEl . classList . contains ( "tasksList-taskRemove " ) ) {
50
+ if ( targetEl . classList . contains ( "todoapp__button--removeTask " ) ) {
51
51
toDoStorage = toDoStorage . filter (
52
52
( element ) => element . taskTitle !== taskTitle
53
53
) ;
54
54
localStorage . setItem ( "arrayTasks" , JSON . stringify ( toDoStorage ) ) ;
55
-
56
55
parentEl . remove ( ) ;
57
56
}
58
57
59
- if ( targetEl . classList . contains ( "tasksList-taskEdit" ) ) {
58
+ if ( targetEl . classList . contains ( "todoapp__button--editTask" ) ) {
59
+ oldInputValue = taskTitle ; // Definindo o valor aqui
60
60
toggleForms ( ) ;
61
-
62
61
textBoxEdit . value = taskTitle ;
63
- oldInputValue = taskTitle ;
64
62
}
65
63
} ) ;
66
64
67
65
// CREATE TASK
68
66
69
67
const saveToDo = ( element ) => {
68
+ const li = document . createElement ( "li" ) ;
69
+ li . className = "todoapp__task" ;
70
+
71
+ const leftSide = document . createElement ( "div" ) ;
72
+ leftSide . className = "task-LeftSide" ;
73
+
74
+ const checkSpan = document . createElement ( "span" ) ;
75
+ checkSpan . className = "todoapp__task-check" ;
76
+ checkSpan . innerHTML = `<i class='fa-solid ${
77
+ element . isChecked ? "fa-check" : ""
78
+ } '></i>`;
79
+
80
+ const p = document . createElement ( "p" ) ;
81
+ p . className = "todoapp__task-name" ;
70
82
if ( element . isChecked ) {
71
- tasksList . innerHTML += `
72
- <li class="tasksList-task">
73
- <div class="task-LeftSide">
74
- <span class="tasksList-taskCheck">
75
- <i class='fa-solid fa-check'></i>
76
- </span>
77
- <p class="tasksList-taskName done">${ element . taskTitle } </p>
78
- </div>
79
- <div class="task-RightSide">
80
- <button
81
- type="button"
82
- class="todo-button fa-solid fa-pen-to-square tasksList-taskEdit"
83
- ></button>
84
- <button
85
- type="button"
86
- class="todo-button fa-solid fa-trash tasksList-taskRemove"
87
- ></button>
88
- </div>
89
- </li>
90
- ` ;
91
- } else {
92
- tasksList . innerHTML += `
93
- <li class="tasksList-task">
94
- <div class="task-LeftSide">
95
- <span class="tasksList-taskCheck">
96
- <i class='fa-solid'></i>
97
- </span>
98
- <p class="tasksList-taskName">${ element . taskTitle } </p>
99
- </div>
100
- <div class="task-RightSide">
101
- <button
102
- type="button"
103
- class="todo-button fa-solid fa-pen-to-square tasksList-taskEdit"
104
- ></button>
105
- <button
106
- type="button"
107
- class="todo-button fa-solid fa-trash tasksList-taskRemove"
108
- ></button>
109
- </div>
110
- </li>
111
- ` ;
83
+ p . classList . add ( "done" ) ;
112
84
}
85
+ p . innerText = element . taskTitle ;
86
+
87
+ leftSide . appendChild ( checkSpan ) ;
88
+ leftSide . appendChild ( p ) ;
89
+
90
+ const rightSide = document . createElement ( "div" ) ;
91
+ rightSide . className = "task-RightSide" ;
92
+ rightSide . innerHTML = `
93
+ <button type="button" class="todoapp__button fa-solid fa-pen-to-square todoapp__button--editTask"></button>
94
+ <button type="button" class="todoapp__button fa-solid fa-trash todoapp__button--removeTask"></button>
95
+ ` ;
96
+
97
+ li . appendChild ( leftSide ) ;
98
+ li . appendChild ( rightSide ) ;
99
+
100
+ tasksList . appendChild ( li ) ;
113
101
} ;
114
102
115
103
formAdd . addEventListener ( "submit" , ( e ) => {
@@ -141,7 +129,7 @@ const toggleForms = () => {
141
129
} ;
142
130
143
131
const updateToDo = ( text ) => {
144
- const allTasks = document . querySelectorAll ( ".tasksList-task " ) ;
132
+ const allTasks = document . querySelectorAll ( ".todoapp__task " ) ;
145
133
146
134
allTasks . forEach ( ( task ) => {
147
135
let taskText = task . querySelector ( "p" ) ;
@@ -150,9 +138,8 @@ const updateToDo = (text) => {
150
138
taskText . innerText = text ;
151
139
152
140
toDoStorage . forEach ( ( objTask , indexTask ) => {
153
- if ( toDoStorage [ indexTask ] . taskTitle == oldInputValue ) {
141
+ if ( toDoStorage [ indexTask ] . taskTitle === oldInputValue ) {
154
142
objTask . taskTitle = text ;
155
-
156
143
localStorage . setItem ( "arrayTasks" , JSON . stringify ( toDoStorage ) ) ;
157
144
}
158
145
} ) ;
@@ -174,6 +161,5 @@ formEdit.addEventListener("submit", (e) => {
174
161
175
162
btnCancelEdit . addEventListener ( "click" , ( e ) => {
176
163
e . preventDefault ( ) ;
177
-
178
164
toggleForms ( ) ;
179
165
} ) ;
0 commit comments