forked from hnasr/javascript_playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
62 lines (54 loc) · 2.08 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>MY TODO APP</h1>
<ol id = 'olTodo'>
</ol>
<button id = 'btnCreate'>Add ToDO!</button>
<script>
const btnCreate = document.getElementById("btnCreate")
btnCreate.addEventListener("click", async e=> {
const jsonRequest = {}
jsonRequest.todo = prompt("Enter your todo item!")
let result = await fetch("http://localhost:8080/todos", {method: "POST",
headers: {"content-type": "application/json"}, body: JSON.stringify(jsonRequest) })
result = await result.json();
readTodos()
if (!result.success) alert("FAILED! ")
})
readTodos();
async function readTodos() {
try{
const olTodo = document.getElementById("olTodo")
while(olTodo.firstChild) olTodo.removeChild(olTodo.firstChild)
const result = await fetch("http://localhost:8080/todos", {method:"GET"})
const todos = await result.json();
todos.forEach(t=>{
const li = document.createElement("li")
li.textContent = t.text;
li.id = t.id;
li.addEventListener("click", async e => {
const jsonRequest = {}
jsonRequest.id = e.target.id;
let result = await fetch("http://localhost:8080/todos", {method: "DELETE",
headers: {"content-type": "application/json"}, body: JSON.stringify(jsonRequest) })
result = await result.json();
readTodos();
if (!result.success) alert("FAILED! ")
})
olTodo.appendChild(li)
})
}
catch (e) {
console.log("Error reading the todos.")
}
}
</script>
</body>
</html>