-
Notifications
You must be signed in to change notification settings - Fork 0
/
scriptTodo.js
234 lines (211 loc) · 6.29 KB
/
scriptTodo.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
// let's Go..
// function listadd() {
// window.location.href = "http://127.0.0.1:5500/IndexTodo.html";
// }
// Add of todo...
const plus = document.querySelector(".plus");
const myTodoInput = document.querySelector(".mytodoInput");
const addTodo = document.querySelector(".addtodo");
function listAdding(todo) {
// console.log("here");
const li = document.createElement("li");
li.setAttribute("uniqueId", todo.id);
li.innerHTML = todo.name;
document.querySelector("ul").appendChild(li);
// creating cross..
const span = document.createElement("span");
span.classList.add("crossMark");
span.innerHTML = `<i class='bx bxs-message-square-x bx-tada bx-rotate-90' style='color:#fa1111'></i>`;
li.appendChild(span);
span.addEventListener("click", deleteToDo);
span.style.marginLeft = "20px";
span.style.cursor = "pointer";
span.style.fontSize = "30px";
// creating tick..
const div = document.createElement("div");
div.classList.add("tick");
div.innerHTML = `<i class='bx bxs-checkbox-checked bx-tada' style='color:#2bcf09'></i>`;
li.appendChild(div);
div.style.marginLeft = "20px";
div.style.cursor = "pointer";
div.style.fontSize = "40px";
div.addEventListener("click", updateState);
//creating editSVg..
const section = document.createElement("section");
section.classList.add("editSvg");
section.innerHTML = `<i class='bx bxs-edit-alt tickmarkzz' style='color:#cdb807' onclick='openModal()' ></i>`;
li.appendChild(section);
section.style.marginLeft = "20px";
section.style.cursor = "pointer";
section.style.fontSize = "40px";
}
// Edit state
async function updateState(event) {
const targetLi = event.target.closest("li");
const targetId = targetLi.getAttribute("uniqueId");
const url = `http://localhost:3000/lists/` + targetId;
const backendElement = await fetch(url);
const backendLi = await backendElement.json();
const updatedTodo = {
name: backendLi.name,
completed: !backendLi.completed,
id: backendLi.id,
};
const response = await fetch(url, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(updatedTodo),
});
getData();
}
// delete functionality..
async function deleteToDo(event) {
const id = event.target.closest("li").getAttribute("uniqueid");
const url = `http://localhost:3000/lists/` + id;
const del = await fetch(url, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
});
getData();
}
// get and display Data
async function getData() {
document.querySelector("ul").innerHTML = "";
const gettingData = await fetch(`http://localhost:3000/lists`);
const todoData = await gettingData.json();
// console.log(todoData);
todoData.forEach((each) => listAdding(each));
}
getData();
//Add todo Input to backend
async function postData() {
const url = `http://localhost:3000/lists`;
// console.log(myTodoInput.value);
data = { name: myTodoInput.value, completed: false };
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
myTodoInput.value = "";
getData();
}
let editTodoId;
// edit..
const overlay = document.querySelector(".overlay");
const modalEdit = document.querySelector(".editModal");
const openModal = function (event) {
editTodoId = this.event.target.closest("li").getAttribute("uniqueId");
modalEdit.classList.remove("hidden");
document.querySelector(".wholeBody").classList.add("hidden");
overlay.classList.remove("hidden");
// document.querySelector(".wholeBody").style.filter = "blur(20px)";
};
overlay.addEventListener("click", () => {
modalEdit.classList.add("hidden");
overlay.classList.add("hidden");
document.querySelector(".wholeBody").classList.remove("hidden");
// document.querySelector(".wholeBody").style.filter = "blur(20px)";
});
// fetch data
fetch`http://localhost:3000/lists/`
.then(function (db) {
return db.json();
})
.then(function (obj) {
console.log(obj);
})
.catch(function (error) {
console.log("Error");
});
// Completed button..
let taskFinished = false;
async function completeFun() {
document.querySelector("ul").innerHTML = "";
const gettingData = await fetch(
`http://localhost:3000/lists?completed=${taskFinished}`
);
console.log(gettingData);
const todoData = await gettingData.json();
console.log(todoData);
todoData.forEach((each) => listAdding(each));
}
// completed list
const completed = document.querySelector(".completed");
completed.addEventListener("click", () => {
taskFinished = true;
completeFun();
});
//incompleted list
const incomplete = document.querySelector(".incompleted");
incomplete.addEventListener("click", () => {
console.log("incomplete");
taskFinished = false;
completeFun();
});
// all ToDo..
const allTodo = document.querySelector(".alltodo");
allTodo.addEventListener("click", () => {
getData();
});
// for updating card in db
const url = `http://localhost:3000/lists/`;
async function EditFun(url, data) {
try {
let res = await fetch(url, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
} catch (error) {
console.log(error);
alert(error);
}
}
// Edit Todo..
const editTodoText = document.querySelector("#username");
async function editTodo() {
console.log(editTodoId);
const url = `http://localhost:3000/lists/` + editTodoId;
console.log("hello");
const backendElement = await fetch(url);
const backendLi = await backendElement.json();
const updatedTodo = {
name: editTodoText.value,
completed: backendLi.completed,
id: backendLi.id,
};
console.log(updatedTodo);
const response = await fetch(url, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(updatedTodo),
});
getData();
}
// Duplicate ToDo..
plus.addEventListener("click", async function () {
if (myTodoInput.value !== "") {
const listTodo = await fetch(`http://localhost:3000/lists`);
const jsonlist = await listTodo.json();
const errorDuplicate = jsonlist.find(
(each) => each.name === myTodoInput.value
);
if (errorDuplicate === undefined) {
postData();
// getData();
} else {
alert("User already exist ❌");
}
}
});