-
Notifications
You must be signed in to change notification settings - Fork 596
/
11_crud_fetch.html
177 lines (157 loc) · 5.66 KB
/
11_crud_fetch.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CRUD API REST FETCH</title>
</head>
<body>
<h1>CRUD API REST FETCH</h1>
<section class="crud">
<article>
<h2 class="crud-title">Agregar Santo</h2>
<form class="crud-form">
<input type="text" name="nombre" placeholder="nombre" required>
<br>
<input type="text" name="constelacion" placeholder="constelación" required>
<br>
<input type="submit" value="Enviar">
<input type="hidden" name="id">
</form>
</article>
<article>
<h2>Ver Santos</h2>
<table class="crud-table">
<thead>
<tr>
<th>Nombre</th>
<th>Constelación</th>
<th>Acciones</th>
</tr>
</thead>
<tbody></tbody>
</table>
</article>
</section>
<template id="crud-template">
<tr>
<td class="name"></td>
<td class="constellation"></td>
<td>
<button class="edit">Editar</button>
<button class="delete">Eliminar</button>
</td>
</tr>
</template>
<script>
/* ********** Curso JavaScript: 117. APIs REST: CRUD con Fetch (1/2) - #jonmircha ********** */
/* ********** Curso JavaScript: 118. APIs REST: CRUD con Fetch (2/2) - #jonmircha ********** */
const d = document,
$table = d.querySelector(".crud-table"),
$form = d.querySelector(".crud-form"),
$title = d.querySelector(".crud-title"),
$template = d.getElementById("crud-template").content,
$fragment = d.createDocumentFragment();
const getAll = async () => {
try {
let res = await fetch("http://localhost:5555/santos"),
json = await res.json();
if (!res.ok) throw { status: res.status, statusText: res.statusText };
console.log(json);
json.forEach(el => {
$template.querySelector(".name").textContent = el.nombre;
$template.querySelector(".constellation").textContent = el.constelacion;
$template.querySelector(".edit").dataset.id = el.id;
$template.querySelector(".edit").dataset.name = el.nombre;
$template.querySelector(".edit").dataset.constellation = el.constelacion;
$template.querySelector(".delete").dataset.id = el.id;
let $clone = d.importNode($template, true);
$fragment.appendChild($clone);
});
$table.querySelector("tbody").appendChild($fragment);
} catch (err) {
let message = err.statusText || "Ocurrió un error";
$table.insertAdjacentHTML("afterend", `<p><b>Error ${err.status}: ${message}</b></p>`);
}
}
d.addEventListener("DOMContentLoaded", getAll);
d.addEventListener("submit", async e => {
if (e.target === $form) {
e.preventDefault();
if (!e.target.id.value) {
//Create - POST
try {
let options = {
method: "POST",
headers: {
"Content-type": "application/json; charset=utf-8"
},
body: JSON.stringify({
nombre: e.target.nombre.value,
constelacion: e.target.constelacion.value
})
},
res = await fetch("http://localhost:5555/santos", options),
json = await res.json();
if (!res.ok) throw { status: res.status, statusText: res.statusText };
location.reload();
} catch (err) {
let message = err.statusText || "Ocurrió un error";
$form.insertAdjacentHTML("afterend", `<p><b>Error ${err.status}: ${message}</b></p>`);
}
} else {
//Update - PUT
try {
let options = {
method: "PUT",
headers: {
"Content-type": "application/json; charset=utf-8"
},
body: JSON.stringify({
nombre: e.target.nombre.value,
constelacion: e.target.constelacion.value
})
},
res = await fetch(`http://localhost:5555/santos/${e.target.id.value}`, options),
json = await res.json();
if (!res.ok) throw { status: res.status, statusText: res.statusText };
location.reload();
} catch (err) {
let message = err.statusText || "Ocurrió un error";
$form.insertAdjacentHTML("afterend", `<p><b>Error ${err.status}: ${message}</b></p>`);
}
}
}
});
d.addEventListener("click", async e => {
if (e.target.matches(".edit")) {
$title.textContent = "Editar Santo";
$form.nombre.value = e.target.dataset.name;
$form.constelacion.value = e.target.dataset.constellation;
$form.id.value = e.target.dataset.id;
}
if (e.target.matches(".delete")) {
let isDelete = confirm(`¿Estás seguro de eliminar el id ${e.target.dataset.id}?`);
if (isDelete) {
//Delete - DELETE
try {
let options = {
method: "DELETE",
headers: {
"Content-type": "application/json; charset=utf-8"
}
},
res = await fetch(`http://localhost:5555/santos/${e.target.dataset.id}`, options),
json = await res.json();
if (!res.ok) throw { status: res.status, statusText: res.statusText };
location.reload();
} catch (err) {
let message = err.statusText || "Ocurrió un error";
alert(`Error ${err.status}: ${message}`);
}
}
}
})
</script>
</body>
</html>