-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
140 lines (133 loc) · 4 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
const formNode = document.getElementById("task-form");
const inputNode = document.getElementById("task-input");
const showNode = document.getElementById("show-item");
//when item is submitted
formNode.addEventListener("submit", (e) => {
e.preventDefault();
const todoText = inputNode.value;
if (todoText.trim() == "") {
alert("Please enter a TODO");
return;
}
//key/value pairs, keys will be the names of the form fields
const formData = new FormData(formNode);
inputNode.value = "";
//send a POST request
fetch("/todo", {
method: "POST",
body: formData,
})
.then(function (response) {
if (response.status === 200) {
//the received object
return response.json();
} else {
alert("something weird happened");
}
})
.then(function (todo) {
showTodoInUI(todo);
});
// }
});
//make request to get data when page refreshes
//get request is made without specifying the method
fetch("/todo-data")
.then(function (response) {
if (response.status === 200) {
//the received object
return response.json();
} else {
alert("something weird happened");
}
})
.then(function (todos) {
//hrr todo ke liye call hoga function
todos.forEach(function (todo) {
showTodoInUI(todo);
});
});
//create the DOM structure and add desired event listeners
function showTodoInUI(todo) {
const todoDiv = document.createElement("div");
todoDiv.setAttribute("id", todo.id);
todoDiv.classList.add("items");
const todoTextNode = document.createElement("span");
const todoImgNode = document.createElement("img");
const checkbox = document.createElement("input");
const deleteButton = document.createElement("button");
deleteButton.classList.add("btn");
checkbox.type = "checkbox";
if (todo.isComplete) {
todoTextNode.classList.add("complete");
checkbox.checked = true;
}
todoTextNode.innerText = todo.todoText;
todoImgNode.setAttribute("src", todo.pic.filename);
todoImgNode.setAttribute("height", "50px");
todoImgNode.setAttribute("width", "50px");
deleteButton.innerText = "x";
// when click on checkbox
checkbox.addEventListener("click", function (e) {
//PUT request bhjenge
const checkId = e.target.parentElement.id;
const isComplete = false;
if (checkbox.checked) {
//if after clicking the checkbox is CHECKED means we want to mark it as COMPLETED
updateById(checkId, !isComplete); // pass id and true
} else {
//if after clicking the checkbox is NOT CHECKED means we want to mark it as NOT COMPLETED
updateById(checkId, isComplete); // pass id and false
}
});
// when click on delete button
deleteButton.addEventListener("click", function (e) {
const deleteId = e.target.parentElement.id;
//DELETE request bhjenge
deleteById(deleteId);
});
todoDiv.appendChild(todoTextNode);
todoDiv.appendChild(todoImgNode);
todoDiv.appendChild(checkbox);
todoDiv.appendChild(deleteButton);
showNode.appendChild(todoDiv);
}
function updateById(checkId, isComplete) {
fetch("/update", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id: checkId, isComplete }),
})
.then(function (response) {
if (response.status !== 200) {
throw new Error("failed to modify status");
}
})
.then(function () {
const item = document.getElementById(checkId);
const itemText = item.querySelector("span");
itemText.classList.toggle("complete");
})
.catch(function (err) {
alert(err);
});
}
function deleteById(deleteId) {
fetch("/delete", {
method: "DELETE",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id: deleteId }),
})
.then(function (response) {
if (response.status !== 200) {
throw new Error("failed to delete");
}
})
.then(function () {
const item = document.getElementById(deleteId);
item.remove();
})
.catch(function (err) {
alert(err);
});
}