-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (71 loc) · 2.19 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
import {
eliminarPosteo,
guardarPost,
onGetPosteos,
getPost,
actualizarPosteo
} from './firebase.js';
const contenedorPosteos = document.getElementById('contenedor-posteos')
const idForm = document.getElementById('id-form');
let posteosEditados = false;
let id = '';
window.addEventListener('DOMContentLoaded', async () => {
onGetPosteos((querySnapshot) => {
let html = '';
querySnapshot.forEach(doc => {
const posteos = doc.data();
html += `
<div class="divPosteos">
<div class="card sinBg">
<img src="..." class="card-img-top" alt="">
<div class="card-body">
<h5 class="card-title"><h3>${posteos.titulo}</h3></h5>
<h6>${posteos.descripcion}</h6>
</div>
<div class="card-footer d-flex justify-content-around">
<button class='btnEditar buttonEditar' data-id="${doc.id}"><i class="fas fa-edit"></i></button>
<button class='btnEliminar buttonEliminar' data-id="${doc.id}"><i class="fas fa-trash"></i></button>
</div>
</div>
</div>`
});
contenedorPosteos.innerHTML = html;
const btnEliminar = contenedorPosteos.querySelectorAll('.btnEliminar');
btnEliminar.forEach(btn => {
btn.addEventListener('click', ({
target: {
dataset
}
}) => {
eliminarPosteo(dataset.id)
})
})
const btnEditar = contenedorPosteos.querySelectorAll('.btnEditar');
btnEditar.forEach((btn) => {
btn.addEventListener('click', async (e) => {
const doc = await getPost(e.target.dataset.id)
const paraEditar = doc.data();
idForm['id-titulo'].value = paraEditar.titulo;
idForm['id-descripcion'].value = paraEditar.descripcion;
posteosEditados = true;
id = doc.id;
idForm['btn-guardar'].innerText = 'Actualizar';
})
})
});
});
idForm.addEventListener('submit', (e) => {
e.preventDefault();
const titulo = idForm['id-titulo']
const descripcion = idForm['id-descripcion']
if (!posteosEditados) {
guardarPost(titulo.value, descripcion.value);
} else {
actualizarPosteo(id, {
titulo: titulo.value,
descripcion: descripcion.value
});
posteosEditados = false;
}
idForm.reset();
});