-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
73 lines (60 loc) · 1.72 KB
/
utils.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
const keysMemo = []
const createContact = (store, contact) => store.setItem(contact.id, JSON.stringify(contact))
const loadContacts = (store, node) =>{
const contactKeys = Object.keys(store)
const sortContactKeys = sortContacts(contactKeys)
sortContactKeys.map(key => {
const data = (JSON.parse(store.getItem(key)))
if (!keysMemo.includes(data.id)){
keysMemo.push(data.id)
// Datos del contacto
const contact = [
data.name,
data.number,
data.address,
'delete_forever'
]
const divContact = document.createElement('div')
divContact.classList.add('contacts__contact')
// Elementos
const contactElements = [
nameContact = document.createElement('p'),
numberContact = document.createElement('p'),
addressContact = document.createElement('p'),
iconDelete = document.createElement('span')
]
contactElements[3].classList.add('material-icons')
contactElements[3].onclick = () =>{
removeContact(store, data.id)
}
// Se cargan los datos de cada contacto en su respectivo elemento
// Se carga cada elemento en el div de los contactos
contactElements.map(element =>{
const index = contactElements.indexOf(element);
element.innerHTML = contact[index]
divContact.appendChild(element)
})
// Se agrega al DOM el div de los contactos
node.appendChild(divContact)
}
})
}
const removeContact = (store, id) => {
store.removeItem(id)
window.location.href = '/'
}
const sortContacts = (array) =>{
let swapped = true
do {
swapped = false
for (let i = 0; i < array.length; i++) {
if(array[i] > array[i + 1]){
let temp = array[i]
array[i] = array[i + 1]
array[i + 1] = temp
swapped = true
}
}
} while (swapped);
return array
}