-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
161 lines (137 loc) · 5.1 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contacts</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1>API Contacts</h1>
<form id="form">
<div class="form-row">
<div class="col">
<input type="text" id="name" class="form-control" placeholder="Name"></input>
</div>
<div class="col">
<input type="text" id="email" class="form-control" placeholder="Email"></input>
</div>
<div class="col">
<input type="number" id="phone" class="form-control" placeholder="Phone"></input>
</div>
</div>
</form>
<button class="btn btn-success" id="btnAdd">
Add Contact
</button>
<button class="btn btn-primary" id="btnActualize" onclick="loaded()">
Actualize contact list
</button>
<table id="table" class ="table">
<thead>
<tr>
<th>ID</th>
<th>name</th>
<th>email</th>
<th>Phone</th>
</tr>
</thead>
<tbody id="tbData">
</tbody>
</table>
<script>
let response = null;
const buttonAdd = document.getElementById("btnAdd");
buttonAdd.addEventListener("click", addContact, false);
async function loaded(){
let info = await fetch("http://127.0.0.1:8000/contacts")
.then(response => response.json())
.then((data) => {
localStorage.setItem("Contacts", JSON.stringify(data));
window.location.reload()
}).catch (
function errorx(){
return false;
}
)
return info;
}
async function createTable() {
if (localStorage.getItem("Contacts")){
let contacts = JSON.parse(localStorage.getItem("Contacts"));
let tablx = document.getElementById("tbData");
for (let x in contacts){
cont = contacts[x];
tr = document.createElement("tr");
for (let i in cont){
td = document.createElement("td");
td.innerHTML = cont[i];
tr.appendChild(td);
};
td = document.createElement("td");
btnDelete = document.createElement("button");
btnDelete.setAttribute("class", "btnDelete btn btn-danger");
btnDelete.setAttribute("id", cont.id)
btnDelete.innerHTML = "Delete";
btnDelete.setAttribute("onclick", "deleteRow(this)");
td.appendChild(btnDelete);
tr.appendChild(td);
tablx.appendChild(tr);
};
} else {
alert("..!! No data in localStorage")
}
}
async function addContact() {
cont = {}
cont.name = document.getElementById("name").value;
cont.email = document.getElementById("email").value;
cont.phone = document.getElementById("phone").value;
url = "http://127.0.0.1:8000/contacts/"
await sendData(url, cont)
loaded()
createTable()
sendEmail(response);
}
function deleteRow(btnDelete){
row = btnDelete.parentNode.parentNode;
row.parentNode.removeChild(row);
deleteContact(btnDelete.id)
loaded()
}
async function deleteContact(id){
await fetch(`http://127.0.0.1:8000/contacts/?id=${id}`, {
method: "DELETE",
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
}
async function sendData(url, data){
await fetch(url, {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then((res) => res.json())
.then((res) => {
response = res;
});
}
async function sendEmail(dataEmail) {
fetch("http://127.0.0.1:8000/sendEmail", {
method: "POST",
body: JSON.stringify(dataEmail),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
}
window.addEventListener("load", createTable, false);
</script>
<script async src="https://cdn.jsdelivr.net/npm/es-module-shims@1/dist/es-module-shims.min.js" crossorigin="anonymous"></script>
</body>
</html>