Repository navigation
Expand file tree
/
Copy pathcontacts.js
More file actions
143 lines (119 loc) · 4.07 KB
/
Copy pathcontacts.js
File metadata and controls
143 lines (119 loc) · 4.07 KB
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
let rick = [];
function getCharacters(done) {
let results = fetch('https://rickandmortyapi.com/api/character');
results
.then(response => response.json())
.then(data => {
done(data)
});
}
getCharacters(any => {
any.results.forEach(personaje => {
rick.push(personaje.image);
});
});
setTimeout(function () {
let contactos = allStorage();
mostrarContactos(contactos);
}, 100);
const btn = document.getElementById('on')
btn.onclick = () => {
document.location.reload(true);
const nombre = document.getElementById('nombre').value;
const apellido = document.getElementById('apellido').value;
const telefono = document.getElementById('telefono').value;
const correo = document.getElementById('email').value;
const randomColor = Math.floor(Math.random() * 16777215).toString(16);
let contacto = {
id: suma() + 1,
nombre: nombre,
apellido: apellido,
telefono: telefono,
correo: correo,
color: randomColor,
}
localStorage.setItem(contacto.id, JSON.stringify(contacto));
mostrarContactos();
cerrarContact();
openPopUp();
}
function cerrarContact() {
const modalContacts = document.getElementById('modalContacts');
modalContacts.style.display = "none";
resetForm();
}
function resetForm() {
document.getElementById("formulario").reset();
}
function allStorage() {
const contactos = [];
let keys = Object.keys(localStorage);
let i = keys.length;
while (i--) {
if (Number.parseInt(keys[i]))
contactos.push(JSON.parse(localStorage.getItem(keys[i])));
}
return contactos;
}
function getRandomImage() {
let cantidad = rick.length;
let nroRandom = Math.random() * cantidad;
nroRandom = Math.floor(nroRandom);
return rick[nroRandom];
}
function mostrarContactos(contactos) {
if (contactos.length > 0) {
contactos.forEach(contacto => {
const node = document.createElement("button");
node.setAttribute('class', 'my-template');
node.setAttribute('id', contacto.id++);
const li = document.createElement('li');
li.setAttribute('class', 'contact');
li.classList.add('listCards', 'contact');
li.innerHTML = `
<img src="${getRandomImage()}"> `
node.appendChild(li);
const textnode = document.createTextNode(contacto.nombre + ' ' + contacto.apellido + ' ' + contacto.telefono + ' ' + contacto.correo);
node.appendChild(textnode);
let color = (contacto.color);
node.style.backgroundColor = `#${color}`;
// lamada al contacto
document.getElementById("lista").appendChild(node);
let btnToast = document.querySelectorAll('.my-template');
for (var i = 0; i < btnToast.length; i++) {
btnToast[i].addEventListener('click', (boton) => {
Swal.fire({
title: 'Llamar',
showCancelButton: true,
confirmButtonText: 'Llamar',
}).then((result) => {
if (result.isConfirmed) {
localStorage.setItem("contactoSeleccionado", boton.target.id);
}
result.isConfirmed ? window.location.href = "../views/telefono.html" : Swal.fire('Gracias');
});
});
};
});
}
}
function openModal() {
const modalContent = document.getElementById('modalContacts');
modalContent.style.display = ('block');
}
function openPopUp() {
let popUp = document.getElementById('mensajePop');
popUp.classList.add('openPopUp');
}
function closePopup() {
let popUp = document.getElementById('mensajePop');
popUp.classList.remove('openPopUp');
}
function openModal2() {
const modalContent = document.getElementById('modalContent');
modalContent.style.display = ('block');
}
function suma() {
contactos = allStorage();
return contactos.length;
}