-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.js
204 lines (168 loc) · 5.89 KB
/
todo.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
// Model
// If localstorage has a todos array, then use it
// Otherwise use the default array.
let todos;
// Retrieve localStorage
const savedTodos = JSON.parse(localStorage.getItem('todos'));
// Check if it is an array
if (Array.isArray(savedTodos)) {
todos = savedTodos;
} else {
todos = [{
title: 'Get groceries',
dueDate: '2022-10-04',
id: 'id1'
}, {
title: 'Wash car',
dueDate: '2022-02-03',
id: 'id2'
}, {
title: 'Make dinner',
dueDate: '2022-03-04',
id: 'id3'
}];
}
// Creates a todo
const createTodo = (title, dueDate) => {
const id = '' + new Date().getTime();
todos.push({
title: title,
dueDate: dueDate,
id: id
});
saveTodo();
}
// Deletes a todo
const removeTodo = idToDelete => {
todos = todos.filter(todo => {
// If the id of this todo matches idToDelete, return false
// For everything else, return true
if (todo.id === idToDelete) {
return false;
} else {
return true;
}
});
saveTodo();
}
function setEditing(todoId) {
todos.forEach(function (todo) {
if (todo.id === todoId) {
todo.isEditing = true;
}
});
saveTodo();
}
function updateTodo(todoId, newTitle, newDate) {
todos.forEach(function (todo) {
if (todo.id === todoId) {
todo.title = newTitle;
todo.dueDate = newDate;
todo.isEditing = false;
}
});
saveTodo();
}
// Saves a todo
const saveTodo = () => {
localStorage.setItem('todos', JSON.stringify(todos));
}
const toggleTodo = (todoId, checked) => {
todos.forEach(todo => {
if (todo.id === todoId) {
todo.isDone = checked;
}
});
}
// Controller
const addTodo = () => {
const textbox = document.getElementById('todo-title');
const title = textbox.value;
const datePicker = document.getElementById('date-picker');
const dueDate = datePicker.value;
createTodo(title, dueDate);
render();
}
const deleteTodo = event => {
const deleteButton = event.target;
const idToDelete = deleteButton.id;
removeTodo(idToDelete);
render();
}
const checkTodo = event => {
const checkbox = event.target;
const todoId = checkbox.dataset.todoId;
const checked = checkbox.checked;
toggleTodo(todoId, checked);
render();
}
function onEdit(event) {
const editButton = event.target;
const todoId = editButton.dataset.todoId;
setEditing(todoId);
render();
}
function onUpdate(event) {
const updateButton = event.target;
const todoId = updateButton.dataset.todoId;
const textbox = document.getElementById('edit-title-' + todoId);
const newTitle = textbox.value;
const datePicker = document.getElementById('edit-date-' + todoId);
const newDate = datePicker.value;
updateTodo(todoId, newTitle, newDate);
render();
}
// View
const render = () => {
// reset our list
document.getElementById('todo-list').innerHTML = '';
todos.forEach(todo => {
const element = document.createElement('div');
element.innerText = todo.title + " | " + todo.dueDate;
// If this todo is being edited, render a textbox, date picker and a
// button for saving the edits.
if (todo.isEditing === true) {
const textbox = document.createElement('input');
textbox.type = 'text';
textbox.id = 'edit-title-' + todo.id;
element.appendChild(textbox);
const datePicker = document.createElement('input');
datePicker.type = 'date';
datePicker.id = 'edit-date-' + todo.id;
element.appendChild(datePicker);
const updateButton = document.createElement('button');
updateButton.innerText = 'Update';
updateButton.innerText.style = "font-weight: 600;";
updateButton.dataset.todoId = todo.id;
updateButton.onclick = onUpdate;
element.appendChild(updateButton);
// If this todo is not being edited, render what we had before
// and add an "Edit" button.
} else {
element.innerText = todo.title + " | " + todo.dueDate;
const editButton = document.createElement('button');
editButton.innerText = 'Edit';
editButton.onclick = onEdit;
editButton.dataset.todoId = todo.id;
element.appendChild(editButton);
}
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.onchange = checkTodo;
checkbox.dataset.todoId = todo.id;
if (todo.isDone === true) {
checkbox.checked = true;
} else {
checkbox.checked = false;
}
element.prepend(checkbox);
const deleteButton = document.createElement('button');
deleteButton.innerText = 'Delete';
deleteButton.onclick = deleteTodo;
deleteButton.id = todo.id;
element.appendChild(deleteButton);
const todoList = document.getElementById('todo-list');
todoList.appendChild(element);
});
}
render();