-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
165 lines (125 loc) · 4.11 KB
/
script.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
const EMPTY_STRING = '';
const TODOS_KEY = 'todos'
updateTodoListElement();
function getTodoListFromStorage(){
const todosJson = localStorage.getItem(TODOS_KEY);
if(!todosJson){
console.log('todos not found in local storage');
return createTodosOnStorage();
}
const todos = JSON.parse(todosJson);
return todos;
}
function saveTodos(todos){
const todosJson = JSON.stringify(todos);
localStorage.setItem(TODOS_KEY, todosJson);
}
function createTodosOnStorage(){
const todos = [];
const todosJson = JSON.stringify(todos);
localStorage.setItem(TODOS_KEY, todos);
return todos;
}
function handleKeyDown(event) {
if (event.key === 'Enter') {
addTodo();
}
}
function addTodo() {
//get todo input element
const todoInputElement = document.getElementById('todo-name-input');
if (!todoInputElement) {
console.log('Element has id named todo-input not found');
}
//get todo input value
const todoInput = todoInputElement.value;
//check input
if (!todoInput) {
console.log('Invalid input');
clearTodoInput(todoInputElement);
return;
}
//get todo date input element
const todoDateInputElement = document.getElementById('todo-date-input');
if (!todoDateInputElement) {
console.log('Element has id named todo-date-input not found');
}
//get todo input value
const todoDateInput = todoDateInputElement.value;
//check input
if (!todoDateInput) {
console.log('Invalid input');
clearTodoInput(todoInputElement);
return;
}
const todos = getTodoListFromStorage();
//add array
todos.push({
todoInput,
todoDateInput
});
saveTodos(todos);
updateTodoListElement();
console.log(todos);
//Don't forget to clear input
clearTodoInput(todoInputElement);
}
function clearTodoInput(todoInputElement) {
todoInputElement.value = EMPTY_STRING;
}
function clearTodoListElement(){
//get todo list element
const todoListElement = document.getElementById('todo-list');
if (!todoListElement) {
console.log('Element has id named todo-list not found');
return;
}
//clear it
todoListElement.innerHTML = EMPTY_STRING;
clearTodoArray();
}
function updateTodoListElement(){
//get todo list element
const todoListElement = document.getElementById('todo-list');
if (!todoListElement) {
console.log('Element has id named todo-list not found');
return;
}
//append todos in todo list
//FIRST RESET TODO list
todoListElement.innerHTML = EMPTY_STRING;
const todos = getTodoListFromStorage();
for (let i = todos.length - 1; i >= 0; --i) {
//Create todo name element
const todoItemNameElement = document.createElement('div');
todoItemNameElement.classList.add('todo-list-item');
todoItemNameElement.innerText = `${todos[i].todoInput}`;
//Create todo date element
const todoItemDateElement = document.createElement('div');
todoItemDateElement.innerText = `${todos[i].todoDateInput}`;
//Create delete button
const todoItemDeleteButtonElement = document.createElement('button');
//Set button click attribute
todoItemDeleteButtonElement.addEventListener('click', () => deleteTodoListItem(i));
//Set button content
todoItemDeleteButtonElement.innerText = 'Delete';
//Set button class
todoItemDeleteButtonElement.classList.add('btn-delete-todo');
todoListElement.appendChild(todoItemNameElement);
todoListElement.appendChild(todoItemDateElement);
todoListElement.appendChild(todoItemDeleteButtonElement);
}
console.log(todos);
}
function clearTodoArray(){
const todos = getTodoListFromStorage();
todos.length = 0;
saveTodos(todos);
}
function deleteTodoListItem(itemId){
const todos = getTodoListFromStorage();
console.log(`deleted item id: ${itemId}, name: ${todos[itemId].todoInput}`);
todos.splice(itemId, 1);
saveTodos(todos);
updateTodoListElement();
}