-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
237 lines (185 loc) · 7.07 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
// Regex
const REGEX_NAME = /^[A-Z][a-z]*[ ][A-Z][a-z]*$/;
const REGEX_NUMBER = /^[0](212|412|414|424|416|426)[0-9]{7}$/;
// Selectors
const inputName = document.querySelector('#input-name'); // input nombre
const inputNumber = document.querySelector('#input-number'); // input telefono
const formBtn = document.querySelector('#form-btn');//boton
const form = document.querySelector('#form'); //formulario
const list = document.querySelector('#list'); // lista ul
// Validations
let nameValidation = false;
let numberValidation = false;
let nameEditValidation = true; //
let numberEditValidation = true;
// Data
let contacts = [];
// Functions
const validateInput = (input, validation) => {
const infoText = input.parentElement.children[2]; // nos mostrara p
console.log(infoText);
if (input.value === '') {
input.classList.remove('correct');
input.classList.remove('incorrect');
infoText.classList.remove('show-info');
} else if (validation) {
input.classList.add('correct');
input.classList.remove('incorrect');
infoText.classList.remove('show-info');
} else {
infoText.classList.add('show-info');
input.classList.add('incorrect');
input.classList.remove('correct');
}
if (nameValidation && numberValidation) {
formBtn.disabled = false;
formBtn.classList.add('btn')
} else {
formBtn.disabled = true;
}
};
// funcion para recorrer el ul por cada li ingresado
const renderContacts = () => {
list.innerHTML = '';
contacts.forEach(contact => {
const li = document.createElement('li');
li.id = contact.id;
li.innerHTML = `
<button class="delete-btn">
<svg class="delete-icon" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
</svg>
</button>
<p>${contact.name}</p>
<p>${contact.number}</p>
<button class="edit-btn">
<svg class="edit-icon" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L10.582 16.07a4.5 4.5 0 0 1-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 0 1 1.13-1.897l8.932-8.931Zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0 1 15.75 21H5.25A2.25 2.25 0 0 1 3 18.75V8.25A2.25 2.25 0 0 1 5.25 6H10" />
</svg>
</button>
`;
list.append(li);
});
}
const getContacts = () => {// funcion
const contactsDb = localStorage.getItem('contacts');
if (localStorage.getItem('contacts')) {
contacts = JSON.parse(contactsDb);
}
renderContacts();
}
getContacts(); // llamando a la funcion getContactos
// Events
inputName.addEventListener('input', e => {
nameValidation = REGEX_NAME.test(inputName.value);
validateInput(inputName, nameValidation)
});
inputNumber.addEventListener('input', e => {
numberValidation = REGEX_NUMBER.test(inputNumber.value);
validateInput(inputNumber, numberValidation)
});
form.addEventListener('submit', e => {
// Logica de negocio
e.preventDefault();
if (!nameValidation || !numberValidation) {
return;
};
// Creo el nuevo contacto
const newContact = {
id: crypto.randomUUID(), // crea un id aleatorio
name: inputName.value,
number: inputNumber.value,
}
// Agrego el nuevo contacto al array
contacts = contacts.concat(newContact);
// Guardo en el navegador
localStorage.setItem('contacts', JSON.stringify(contacts));
// Limpiar el formulario
nameValidation = false;
numberValidation = false;
// Logica de renderizado
inputName.value = '';
inputNumber.value = '';
validateInput(inputName, nameValidation);
validateInput(inputNumber, numberValidation);
renderContacts();
});
list.addEventListener('click', e => {
const deleteBtn = e.target.closest('.delete-btn');
const editBtn = e.target.closest('.edit-btn');
// Eliminar
if (deleteBtn) {
const id = deleteBtn.parentElement.id;
contacts = contacts.filter(contact => {
if (contact.id !== id) {
return contact;
}
});
localStorage.setItem('contacts', JSON.stringify(contacts));
renderContacts();
}
// Editar
if (editBtn) {
const li = editBtn.parentElement;
const nameEdit = li.children[1];
console.log(nameEdit);//
const numberEdit = li.children[2];
if (li.classList.contains('editando')) {
li.classList.remove('editando');
contacts = contacts.map(contact => {
if (contact.id === li.id) {
return {
...contact,
name: nameEdit.innerHTML,
number: numberEdit.innerHTML
}
} else {
return contact;
}
});
localStorage.setItem('contacts', JSON.stringify(contacts));
renderContacts();
editBtn.innerHTML = `
<svg class="edit-icon" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L10.582 16.07a4.5 4.5 0 0 1-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 0 1 1.13-1.897l8.932-8.931Zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0 1 15.75 21H5.25A2.25 2.25 0 0 1 3 18.75V8.25A2.25 2.25 0 0 1 5.25 6H10" />
</svg>
`
} else {
li.classList.add('editando');
nameEdit.setAttribute('contenteditable', 'true');
numberEdit.setAttribute('contenteditable', 'true');
editBtn.classList.add('editando')
editBtn.innerHTML = `
<svg class="edit-icon" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L6.832 19.82a4.5 4.5 0 0 1-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 0 1 1.13-1.897L16.863 4.487Zm0 0L19.5 7.125" />
</svg>
`;
nameEdit.classList.add('editando')
numberEdit.classList.add('editando')
// nameEdit.classList.add('info')
// numberEdit.classList.add('info')
nameEdit.addEventListener('input',e=>{
nameEditValidation = REGEX_NAME.test(nameEdit.innerHTML )
validateInput(nameEdit, nameEditValidation)
if (nameEditValidation && numberEditValidation) {
editBtn.disabled = false;
} else {
editBtn.disabled = true;
}
})
numberEdit.addEventListener('input',e=>{
numberEditValidation = REGEX_NUMBER.test(numberEdit.innerHTML)
validateInput(numberEdit, numberEditValidation)
const infoText = li.parentElement.children[3]
console.log(infoText)
console.log()
if (nameEditValidation && numberEditValidation) {
editBtn.disabled = false;
// infoText.classList.add('show-info');
} else {
editBtn.disabled = true;
}
})
}
}
});