-
Notifications
You must be signed in to change notification settings - Fork 8
/
todo.html
140 lines (135 loc) · 2.96 KB
/
todo.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script>
window.onload = () => {
let tasks = document.getElementById("tasks");
let content = document.getElementById("content");
let button = document.getElementById("button");
function remove(taskId) {
fetch("http://localhost:8080/tasks/" + taskId, { method: 'DELETE' })
.then(response => {
//console.log("delete task");
refresh();
});
}
function refresh() {
fetch("http://localhost:8080/tasks", { method: 'GET' })
.then(response => response.json())
.then(json => {
//console.log("get tasks response ", json);
tasks.innerHTML = "";
json.forEach(task => {
var element = document.createElement("div");
var content = document.createElement("div");
content.append(task.content);
var trash = document.createElement("button");
trash.append("X");
element.append(trash);
element.append(content);
tasks.append(element);
trash.onclick = () => {
remove(task.id);
};
});
})
.catch(error => console.error('error:', error));
}
refresh();
let action = () => {
let text = content.value;
if (text == "") {
return;
}
let newTask = { content: text };
fetch("http://localhost:8080/tasks", {
method: 'POST',
body: JSON.stringify(newTask),
headers: {
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(json => {
//console.log("post tasks response ", json);
content.value = "";
refresh();
})
.catch(error => console.error('error:', error));
};
button.onclick = action;
content.onchange = action;
};
</script>
<style>
#container {
width: 70%;
margin: auto;
}
#content {
width: 65%;
height: 45px;
margin: 10px;
font-size: 16px;
color: black;
border: 2px solid lightgray;
border-radius: 5px;
}
#content:focus {
outline: none;
border-color: blue;
caret-color: blue;
}
#button {
float: right;
width: 20%;
height: 45px;
padding: 12px;
margin: 12px;
font-size: 16px;
background-color: blue;
color: white;
border: none;
outline: none;
border-radius: 5px;
}
#tasks {
width: 70%;
margin: auto;
}
#tasks div {
width: auto;
background-color: antiquewhite;
color: black;
font-size: 16px;
border-radius: 5px;
}
#tasks div div {
width: 85%;
padding: 12px;
margin: 14px;
}
#tasks button {
float: right;
width: 30px;
height: 30px;
margin: 7px;
font-size: 16px;
background-color: lightgray;
color: black;
border: none;
outline: none;
border-radius: 3px;
}
</style>
</head>
<body>
<div id="container">
<input id="content" type="text" placeholder="Type something here ...">
<button id="button">Add</button>
</div>
<div id="tasks">
</div>
</body>
</html>