-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
156 lines (136 loc) · 3.98 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
let todosArr = [];
let count = 0;
let filter = "all";
const newToDoInput = document.querySelector("#inputField");
readTodos(todosArr).then((jsonData) => {
todosArr = jsonData;
renderToDos();
});
function addToDo(event) {
event.preventDefault();
const newToDoText = newToDoInput.value;
if (checkDuplis()) {
const newToDo = {
description: newToDoText,
done: false,
};
newToDoInput.value = "";
createToDo(newToDo).then((newToDoFromApi) => {
todosArr.push(newToDoFromApi);
renderToDos();
});
}
}
function renderToDos() {
const toDoList = document.querySelector("#toDoList");
toDoList.innerHTML = "";
todosArr.forEach((ObjOfArr) => {
const ListElement = document.createElement("li");
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked = ObjOfArr.done;
checkbox.Obj = ObjOfArr;
if (ObjOfArr.done) {
ListElement.style = "text-decoration: line-through;";
}
checkbox.addEventListener("change", doneTask);
const description = document.createTextNode(ObjOfArr.description);
ListElement.appendChild(checkbox);
ListElement.appendChild(description);
toDoList.appendChild(ListElement);
console.log(filter);
if (filter === "all") {
ListElement.hidden = false;
} else if (filter === "open") {
ListElement.hidden = ObjOfArr.done;
} else if (filter === "done") {
ListElement.hidden = !ObjOfArr.done;
}
console.log(ListElement.hidden);
});
}
function doneTask(event) {
const changedTodo = event.target.Obj;
if (event.target.checked === true) {
changedTodo.done = true;
event.target.Obj.done = true;
event.target.parentElement.style = "text-decoration: line-through;";
} else {
event.target.parentElement.style = "text-decoration: none;";
changedTodo.done = false;
event.target.Obj.done = false;
}
updateToDo(event.target.Obj.id, changedTodo); //event.target.Obj.id = Argument welches den Parameter (api.js) wiedergibt.
}
function createID(text) {
return text.replaceAll(" ", "").toLowerCase() + Date.now();
}
const addBtn = document.querySelector("#addTodo");
addBtn.addEventListener("click", addToDo);
function checkDuplis() {
for (let duplis of todosArr) {
console.log(duplis.description.toLowerCase());
if (newToDoInput.value.toLowerCase() === duplis.description.toLowerCase()) {
alert("Wenn schon da, dann nix dazu!");
return false;
}
}
return true;
}
const radioBtn = document.querySelector("#radioBtn");
radioBtn.addEventListener("change", useFilter);
const open = document.querySelector("#openFilter");
const done = document.querySelector("#doneFilter");
const all = document.querySelector("#allFilter");
function useFilter(event) {
if (event.target === open) {
filter = "open";
console.log(filter);
} else if (event.target === done) {
filter = "done";
console.log(filter);
} else if (event.target === all) {
filter = "all";
console.log(filter);
}
renderToDos();
}
const removeBtn = document.querySelector("#removeBtn");
removeBtn.addEventListener("click", deleteTodo);
function deleteTodo() {
for (let deletedObj of todosArr) {
if (deletedObj.done === true) {
fetch("http://localhost:4730/todos/" + deletedObj.id, {
method: "DELETE",
})
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error("Network response not ok!");
}
})
.then(readTodos())
.catch((error) => {
alert(error.message);
});
}
}
renderToDos();
}
function removeToDoItem() {
let fetches = [];
for (const deletedToDo of state) {
if (deletedToDo.done) {
const todoID = deletedToDo.id;
fetches.push(removeFromAPI(todoID));
}
}
Promise.all(fetches).then((values) => {
if (values.indexOf(undefined) != -1) {
console.log(values);
state = openTodos;
renderToDos();
}
});
}