/
todo.js
236 lines (203 loc) · 7.11 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
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
"use strict";
//cors key: 5d9093301ce70f6379855131
//link https://todolist2019-e565.restdb.io/rest/addtaskform
const addForm = document.querySelector("form#addForm");
const addTaskBtn = document.querySelector(".addTaskBtn");
const editForm = document.querySelector("form#editForm");
const modal = document.querySelector(".modal");
document.addEventListener("DOMContentLoaded", function() {
get();
});
addForm.addEventListener("submit", e => {
e.preventDefault();
addTaskBtn.textContent = "Waiting..."
post();
});
editForm.addEventListener("submit", e => {
e.preventDefault();
put();
document.querySelector(".modal").classList.add("hide");
})
modal.querySelector(".closeModal").addEventListener("click", () => {
modal.classList.add("hide");
document.querySelector(".taskOgName").textContent = "";
editForm.reset();
})
modal.querySelector(".cancelEditing").addEventListener("click", () => {
modal.classList.add("hide");
document.querySelector(".taskOgName").textContent = "";
editForm.reset();
})
function get() {
fetch("https://todolist2019-e565.restdb.io/rest/addtaskform", {
method: "get",
headers: {
"Content-Type": "application/json; charset=utf-8",
"x-apikey": "5d9093301ce70f6379855131",
"cache-control": "no-cache"
}
})
.then(e => e.json())
.then(tasks => {
tasks.forEach(addTask);
});
}
function post() {
const data = {
task: addForm.elements.task.value,
when: addForm.elements.when.value,
notes: addForm.elements.notes.value,
done: addForm.elements.done.value,
};
const postData = JSON.stringify(data);
fetch("https://todolist2019-e565.restdb.io/rest/addtaskform", {
method: "post",
headers: {
"Content-Type": "application/json; charset=utf-8",
"x-apikey": "5d9093301ce70f6379855131",
"cache-control": "no-cache"
},
body: postData
})
.then(res => res.json())
.then(data => {
addTask(data);
showToast();
addForm.reset();
addTaskBtn.textContent = "Successfully added";
addTaskBtn.style.backgroundColor = "green";
setTimeout( function() {
addTaskBtn.textContent = "Add task";
addTaskBtn.style.backgroundColor = "#FA983A";
}, 3000);
});
}
function deleteTaskAnimation(id) {
const deletedTask = document.querySelector(`article[data-task-id="${id}"]`)
deletedTask.classList.add("deleteTask");
deletedTask.addEventListener("animationend", () => {
deleteTask(id)
});
}
function deleteTask(id) {
fetch("https://todolist2019-e565.restdb.io/rest/addtaskform/" + id, {
method: "delete",
headers: {
"Content-Type": "application/json; charset=utf-8",
"x-apikey": "5d9093301ce70f6379855131",
"cache-control": "no-cache"
}
})
.then(res => res.json())
.then(data => {
document.querySelector(`article[data-task-id="${id}"]`).remove();
});
}
function put() {
let data = {
task: editForm.elements.task.value,
when: editForm.elements.when.value,
notes: editForm.elements.notes.value
};
let postData = JSON.stringify(data);
const taskId = editForm.elements.id.value;
fetch("https://todolist2019-e565.restdb.io/rest/addtaskform/" + taskId,
{
method: "put",
headers: {
"Content-Type": "application/json; charset=utf-8",
"x-apikey": "5d9093301ce70f6379855131",
"cache-control": "no-cache"
},
body: postData
}
)
.then(d => d.json())
.then( updatedTask => {
const parentElement = document.querySelector(`article[data-task-id="${updatedTask._id}"]`);
parentElement.querySelector("h2").textContent = updatedTask.task;
parentElement.querySelector(".taskDate").textContent = updatedTask.when;
parentElement.querySelector(".taskNotes").textContent = updatedTask.notes;
});
}
function editTask(id){
//fetchAndPopulate in Jonas' example
document.querySelector(".modal").classList.remove("hide");
fetch(`https://todolist2019-e565.restdb.io/rest/addtaskform/${id}`, {
method: "get",
headers: {
"Content-Type": "application/json; charset=utf-8",
"x-apikey": "5d9093301ce70f6379855131",
"cache-control": "no-cache"
}
})
.then(e => e.json())
.then(tasks => {
document.querySelector(".taskOgName").textContent = tasks.task;
editForm.elements.task.value=tasks.task;
editForm.elements.when.value=tasks.when;
editForm.elements.notes.value=tasks.notes;
editForm.elements.id.value=tasks._id;
});
}
function addTask(task) {
const template = document.querySelector("template").content;
const clone = template.cloneNode(true);
clone.querySelector("article").dataset.taskId = task._id;
clone.querySelector("h2").textContent = task.task;
clone.querySelector(".taskDate").textContent = task.when;
clone.querySelector(".taskNotes").textContent = task.notes;
clone.querySelector(`input[type="number"]`).value = task.done;
clone.querySelector(".deleteBtn").addEventListener("click", () => {
deleteTaskAnimation(task._id);
});
clone.querySelector(".editBtn").addEventListener("click", e => {
editTask(task._id);
});
clone.querySelector("#toggleStatusBtn").addEventListener("click", () => {
changeStatus(task._id);
});
if (task.done === 1) {
clone.querySelector(`article[data-task-id="${task._id}"]`).classList.add("done");
clone.querySelector("#toggleStatusBtn").classList.remove("doneBtn");
clone.querySelector("#toggleStatusBtn").classList.add("notDoneBtn");
}
document.querySelector("main").prepend(clone);
}
function showToast() {
const toast = document.querySelector(".toast")
toast.classList.add("taskAdded");
setTimeout(function(){
toast.classList.remove("taskAdded");
}, 4000);
}
function changeStatus(id) {
const parentElement = document.querySelector(`article[data-task-id="${id}"]`);
//toggle visuals first to avoid lag
document.querySelector(`article[data-task-id="${id}"]`).classList.toggle("done");
parentElement.querySelector("#toggleStatusBtn").classList.toggle("doneBtn");
parentElement.querySelector("#toggleStatusBtn").classList.toggle("notDoneBtn");
//actually update the db
if (parentElement.querySelector(`input[type="number"`).value === "1") {
parentElement.querySelector(`input[type="number"`).value = "2";
} else {
parentElement.querySelector(`input[type="number"`).value = "1";
};
let data = {
done: parentElement.querySelector(`input[type="number"`).value
};
let postData = JSON.stringify(data);
console.log("https://todolist2019-e565.restdb.io/rest/addtaskform/" + id);
fetch("https://todolist2019-e565.restdb.io/rest/addtaskform/" + id,
{
method: "put",
headers: {
"Content-Type": "application/json; charset=utf-8",
"x-apikey": "5d9093301ce70f6379855131",
"cache-control": "no-cache"
},
body: postData
}
)
.then(d => d.json());
}