-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
261 lines (212 loc) · 7.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import createTodo from "./createTodo.js";
import createTodoCompleted from "./createTodoCompleted.js";
import loadAllTodo from "./loadAllTodo.js";
const submitForm = document.getElementById("todo-form");
const todoList = document.getElementById("todoList");
const completedTasksCont = document.getElementById("complete");
// Create unique id
// https://stackoverflow.com/questions/3231459/how-can-i-create-unique-ids-with-javascript
function uid() {
return Date.now().toString(36) + Math.random().toString(36).substring(2);
}
function completedTasks() {
// Get todo list of completed items
const { children } = completedTasksCont;
const completeTask = children;
// array of todo items checked as complete
const compTasks = Array.from(completeTask);
const completedHeader = document.querySelector("#error");
completedHeader.classList.remove("hidden");
// Check if todo list contains any tasks
if (compTasks.length <= 0) {
// eslint-disable-next-line no-console
console.log("no items");
// Create new list element
// const completedHeader = document.querySelector("#error");
completedHeader.classList.add("hidden");
}
const result = compTasks.forEach((element) => {
const [completeLabel, completeButton] = [...element.children];
const [completeCheckBox] = [...completeLabel.children];
// eslint-disable-next-line no-console
console.log(completeLabel.lastChild);
// delete button to remove todo container
completeButton.addEventListener("click", () => {
// if id string includes completed.
if (completeCheckBox.id.includes("completed")) {
// remove todo from todo list container
element.remove();
//remove todo from local storage
localStorage.removeItem(
`${completeCheckBox.id}`,
`${completeLabel.lastChild}`
);
}
});
});
return result;
}
function moveItem() {
// Get all todos created.
const { children } = todoList;
const taskToMove = children;
// Return array of todos with checkbox checked as completed.
const taskToMoveList = Array.from(taskToMove).filter((element) =>
element.childNodes[0].firstChild.checked ? element : false
);
// Conditional to prevent uncaught reference error as completedListIt not created yet.
if (taskToMoveList.length > 0) {
const [data] = [...taskToMoveList];
const [completedCheck] = [...data.childNodes];
// get checkbox and todo text
const [checkBox, text] = [...completedCheck.childNodes];
// remove task if not marked as completed.
if (!checkBox.id.includes("completed")) {
localStorage.removeItem(`${checkBox.id}`, `${text.textContent}`);
}
//if not marked as completed - Add task to local storage marked as completed.
localStorage.setItem(`completed-${checkBox.id}`, `${text.textContent}`);
// create completed todo
const completedListItem = createTodoCompleted(
checkBox.id,
`${text.textContent}`
);
// append completed todo to completed todo container
completedTasksCont.append(completedListItem);
completedTasks();
}
// eslint-disable-next-line no-console
return console.error("completed item created");
}
function addItemToList(e) {
e.preventDefault();
// Get input value remove any whitespace
const [userInput] = [e.target[0]];
const itemToAdd = userInput.value.trim();
// grab error message and hide it
const errorMessage = document.querySelector(".error");
errorMessage.classList.add("hidden");
// display message only if input is empty string
if (itemToAdd === "") {
errorMessage.classList.remove("hidden");
return;
}
// create todo
const itemContainer = createTodo(`${uid()}`, itemToAdd);
// add to todo to todo container
todoList.appendChild(itemContainer);
// get todo label and delete button
const [todolabel, todoDelete] = [...itemContainer.children];
// get todo checkbox inside label
const [todoCheckBox] = [...todolabel.children];
todoCheckBox.addEventListener("change", (event) => {
// eslint-disable-next-line no-console
console.log(event);
// check if checkbox is checked
if (todoCheckBox.checked) {
// add css class
itemContainer.classList.add("completed");
// add todo to completed tasks to be added to complete section
// completedTasks();
} else if (!todoCheckBox.checked) {
// remove css class
itemContainer.classList.remove("completed");
}
});
// delete button to remove todo container
todoDelete.addEventListener("click", () => {
// if todo checkbox checked
if (todoCheckBox.checked) {
// move selected todo to completed section
moveItem();
// remove selected todo from todo list container
itemContainer.remove();
}
// if todo not checkbox checked
if (!todoCheckBox.checked) {
//remove todo from local storage
localStorage.removeItem(`${todoCheckBox.id}`);
// remove selected todo from todo list container
itemContainer.remove();
}
});
// reset input
document.getElementById("addItem").value = "";
}
function loadAllTasks() {
// check if local storage has data
if (window.localStorage) {
// get todos data from local storage
const allTasks = { ...window.localStorage };
// remove hidden class to show header
const compHeader = document.querySelector("#error");
compHeader.classList.remove("hidden");
// return array of todos not checked as completed
const NotCompletedTasks = Object.entries(allTasks).filter(
(item) => !item[0].includes("completed")
);
// iterate over array of todos not completed.
NotCompletedTasks.forEach((value) => {
// create todo
const itemContainer = loadAllTodo(`${value[0]}`, `${value[1]}`);
todoList.appendChild(itemContainer);
// get todo label and delete button
const [todolabel, todoDelete] = [...itemContainer.children];
// get todo checkbox inside label
const [todoCheckBox] = [...todolabel.children];
todoCheckBox.addEventListener("change", () => {
// check if checkbox is checked
if (todoCheckBox.checked) {
// add css class
itemContainer.classList.add("completed");
} else if (!todoCheckBox.checked) {
// remove css class
itemContainer.classList.remove("completed");
}
});
// remove item container
todoDelete.addEventListener("click", () => {
if (todoCheckBox.checked) {
moveItem();
itemContainer.remove();
}
if (!todoCheckBox.checked) {
localStorage.removeItem(`${value[0]}`, `${value[1]}`);
itemContainer.remove();
}
});
});
// return array of todos checked as completed
const CompletedTasks = Object.entries(allTasks).filter((item) =>
item[0].includes("completed")
);
// iterate over completed todos and get value array
CompletedTasks.forEach((value) => {
// get completed task container
const compTasksList = document.querySelector("#complete");
// create completed todo
const compItemCont = loadAllTodo(`${value[0]}`, `${value[1]}`);
compTasksList.appendChild(compItemCont);
// get todo label and delete button
const [todolabel, todoDelete] = [...compItemCont.children];
// get todo checkbox inside label
const [todoCheckBox] = [...todolabel.children];
if (!todoCheckBox.checked) {
todoCheckBox.checked = true;
}
todoDelete.addEventListener("click", () => {
// check if delete button id matches checkbox id.
if (todoDelete.id.includes("complete")) {
// remove selected todo from todo list container
compItemCont.remove();
//remove todo from local storage
localStorage.removeItem(`${value[0]}`, `${value[1]}`);
}
});
});
}
}
// load tasks on page reload
loadAllTasks();
// add task to todo list
submitForm.addEventListener("submit", addItemToList);