-
Notifications
You must be signed in to change notification settings - Fork 598
/
10_crud_ajax.html
165 lines (148 loc) · 5.02 KB
/
10_crud_ajax.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
<!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 AJAX</title>
</head>
<body>
<h1>CRUD API REST AJAX</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: 115. APIs REST: CRUD con AJAX (1/2) - #jonmircha ********** */
/* ********** Curso JavaScript: 116. APIs REST: CRUD con AJAX (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 ajax = (options) => {
let { url, method, success, error, data } = options;
const xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", e => {
if (xhr.readyState !== 4) return;
if (xhr.status >= 200 && xhr.status < 300) {
let json = JSON.parse(xhr.responseText);
success(json);
} else {
let message = xhr.statusText || "Ocurrió un error";
error(`Error ${xhr.status}: ${message}`);
}
});
xhr.open(method || "GET", url);
xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
xhr.send(JSON.stringify(data));
}
const getAll = () => {
ajax({
url: "http://localhost:5555/santos",
success: (res) => {
console.log(res);
res.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);
},
error: (err) => {
console.log(err);
$table.insertAdjacentHTML("afterend", `<p><b>${err}</b></p>`);
}
})
}
d.addEventListener("DOMContentLoaded", getAll);
d.addEventListener("submit", e => {
if (e.target === $form) {
e.preventDefault();
if (!e.target.id.value) {
//Create - POST
ajax({
url: "http://localhost:5555/santos",
method: "POST",
success: (res) => location.reload(),
error: (err) => $form.insertAdjacentHTML("afterend", `<p><b>${err}</b></p>`),
data: {
nombre: e.target.nombre.value,
constelacion: e.target.constelacion.value
}
});
} else {
//Update - PUT
ajax({
url: `http://localhost:5555/santos/${e.target.id.value}`,
method: "PUT",
success: (res) => location.reload(),
error: (err) => $form.insertAdjacentHTML("afterend", `<p><b>${err}</b></p>`),
data: {
nombre: e.target.nombre.value,
constelacion: e.target.constelacion.value
}
});
}
}
});
d.addEventListener("click", 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
ajax({
url: `http://localhost:5555/santos/${e.target.dataset.id}`,
method: "DELETE",
success: (res) => location.reload(),
error: (err) => alert(err)
});
}
}
})
</script>
</body>
</html>