-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
419 lines (246 loc) · 10.1 KB
/
index.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* INICIALIZO variable de firebase */
const db = firebase.firestore();
/* INICIALIZO EL OBJETO "BLOG" */
const blog = {};
let id = '';
const tablaBlogs = document.getElementById('tabla');
/* CAPTURO EL FORMULARIO */
const blogForm = document.getElementById('blog-form');
const addBlog = document.getElementById('addBlog');
const modalVistaPrevia = document.getElementById('modalBlogbody');
const modalNuevo = document.getElementById('modalNuevoBlog');
let editStatus = false;
/* FUNCION PARA AGREGAR NUEVO BLOG */
const saveBlog = (blog) => {
/* agrego un nuevo blog (si no existe la colleccion "blogs" crea una nueva) */
db.collection('blogs').doc().set(blog);
}
/* FUNCION PARA OBTENER COLECCION DE BLOGS */
const getBlogs = () => {
const collection = db.collection('blogs');
return collection.get();
}
/* FUNCION PARA 1 BLOG */
const getBlog = (id) => {
const collection = db.collection('blogs');
return collection.doc(id).get();
}
/* FUNCION PARA OBTENER COLECCION DE BLOGS CADA VEZ QUE SE ACTUALICE */
const ongetBlogs = (callback) => {
const blogsCollection = db.collection('blogs');
blogsCollection.onSnapshot(callback);
}
// const deleteBlog = (id) => {
// db.collection('blogs').doc(id).delete();
// }
/* FUNCION PARA ELIMINAR BLOG */
const deleteBlog = (id) => {
const swalWithBootstrapButtons = Swal.mixin({
customClass: {
confirmButton: 'btn btn-success',
cancelButton: 'btn btn-danger'
},
buttonsStyling: false
})
swalWithBootstrapButtons.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, delete it!',
cancelButtonText: 'No, cancel!',
reverseButtons: true
}).then((result) => {
if (result.isConfirmed) {
db.collection('blogs').doc(id).delete();
swalWithBootstrapButtons.fire(
'Deleted!',
'Your file has been deleted.',
'success'
)
} else if (
/* Read more about handling dismissals below */
result.dismiss === Swal.DismissReason.cancel
) {
swalWithBootstrapButtons.fire(
'Cancelled',
'Your imaginary file is safe :)',
'error'
)
}
})
}
/* FUNCION PARA ACTUALIZAR BLOG */
const updateBlog = (id,updatedBlog) => {
db.collection('blogs').doc(id).update(updatedBlog);
}
/* CARGA AUTOMATICA DE TABLA DE BLOGS // EDIT // DELETE */
window.addEventListener('DOMContentLoaded', async (e) => {
ongetBlogs((querySnapshot) => {
tabla.innerHTML = `
<table class="table table-striped table-hover" id="tabla">
<caption>Lista de blogs subidos</caption>
<thead>
<tr>
<th class="text-center">ID</th>
<th class="text-center">Titulo</th>
<th class="text-center">Fecha</th>
<th class="text-center">Descripcion</th>
<th class="text-center">Editar</th>
<th class="text-center">Vista Previa</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
`
querySnapshot.forEach(doc => {
const blog = doc.data();
blog.id = doc.id;
tabla.innerHTML += `
<tr>
<td class="text-center">${blog.id}</td>
<td class="text-center">${blog.title}</td>
<td class="text-center">${blog.date}</td>
<td class="text-center">${blog.description}</td>
<td class="text-center">
<i class="bi bi-pencil-fill btn-edit" data-id="${blog.id}" style="font-size: 2rem; color: black;cursor: pointer;">
</i>
<i class="bi bi-x-square btn-delete" data-id="${blog.id}" style="font-size: 2rem; color: red; cursor: pointer;">
</i>
</td>
<td class="text-center">
<button type="button " class="btn btn-secondary" data-bs-toggle="modal" data-bs-target="#modalBlog">
Vista Previa
</button>
</td>
</tr>
`;
/* CAPTUIRO LOS BOTONES DELETE*/
const btnDelete = document.querySelectorAll('.btn-delete');
btnDelete.forEach((btn)=> {
btn.addEventListener('click', async (e) =>{
await deleteBlog(e.target.dataset.id);
})
})
const btnEdit = document.querySelectorAll('.btn-edit');
btnEdit.forEach((btn) =>{
btn.addEventListener('click', async (e) => {
/* OBTENGO EL DOCUMENTO DE FIREBASE CON X ID */
const doc = await getBlog(e.target.dataset.id);
/* MUESTRO EL MODAL, USANDO UN METODO DE BOOTSRAP */
const modalNuevo = new bootstrap.Modal(document.getElementById('modalNuevoBlog'))
modalNuevo.show();
blogForm['btn-task-form'].innerHTML = 'Actualizar';
editStatus = true;
id = doc.id;
/* RELLENO LOS VALORES DEL MODAL ABIERTO CON EL OBJETO A EDITAR */
blogForm['blog-title'].value = doc.data().title
blogForm['blog-description'].value = doc.data().description
})
})
});
})
});
/* BOTON VISTA PREVIA */
modalVistaPrevia.addEventListener( 'DOMContentLoaded', async (e) => {
const querySnapshot = await getBlogs();
console.log(querySnapshot.data())
querySnapshot.data().forEach();
modalVistaPrevia.innerHTML = `
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="modalBlogLabel">${querySnapshot.data().title}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<!-- CUERPO DEL MODAL -->
<div class="modal-body">
<h2>${querySnapshot.data().title}</h2>
<span class="text-secondary">${querySnapshot.data().date}</span>
<p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Quasi iure voluptate quis delectus
corrupti fugiat ad? Aliquam excepturi quidem odio quibusdam architecto ipsam animi dignissimos
repellat magni. Animi, asperiores dolor?</p>
<!-- CAROUSEL -->
<div id="carouselExampleIndicators" class="carousel slide" data-bs-ride="carousel">
<div class="carousel-indicators">
<button type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide-to="0"
class="active" aria-current="true" aria-label="Slide 1"></button>
<button type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide-to="1"
aria-label="Slide 2"></button>
<button type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide-to="2"
aria-label="Slide 3"></button>
</div>
<div class="carousel-inner">
<div class="carousel-item active">
<img src="./images/gallery/beautiful-shot-of-modern-house-kitchen-and-dining-room.jpg"
class="d-block w-50 m-auto" alt="...">
</div>
<div class="carousel-item">
<img src="./images/gallery/before-1.jpg" class="d-block w-50 m-auto" alt="...">
</div>
<div class="carousel-item">
<img src="./images/gallery/hairdresser-cut-hair-her-client-in-hair-salon.jpg"
class="d-block w-50 m-auto" alt="...">
</div>
</div>
<button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleIndicators"
data-bs-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="visually-hidden">Previous</span>
</button>
<button class="carousel-control-next" type="button" data-bs-target="#carouselExampleIndicators"
data-bs-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="visually-hidden">Next</span>
</button>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cerrar</button>
</div>
</div>
</div>
`;
});
/* BOTON GUARDAR */
blogForm.addEventListener('submit', async (e) => {
e.preventDefault();
/*CAPTURO LOS INPUT*/
const title_input = blogForm['blog-title'];
/* ASIGNO LOS VALORES DEL INPUT AL OBJETO */
blog.title = blogForm['blog-title'].value;
blog.description = blogForm['blog-description'].value;
blog.id = id;
if (!editStatus)
{
await saveBlog(blog);
blogForm.reset();
Swal.fire(
'Buen trabajo!',
'El blog ha sido creado',
'success'
)
}
else
{
await updateBlog(id,blog);
editStatus = false;
blogForm['btn-task-form'].innerHTML = 'Guardar';
/* CERRAR FORM*/
var myModal = new bootstrap.Modal(document.getElementById('modalNuevoBlog'))
blogForm.reset();
myModal.hide();
Swal.fire(
'Buen trabajo!',
'El blog ha sido modificado',
'success'
)
}
});
/* BOTON Añadir Blog */
addBlog.addEventListener('click', (e) =>{
editStatus = false;
blogForm['btn-task-form'].innerHTML = 'Guardar';
blogForm.reset();
})