-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
118 lines (101 loc) · 2.77 KB
/
main.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
let contacts = []
/**
* Called when submitting the new Contact Form
* This method will pull data from the form
* use the provided function to give the data an id
* then add that data to the contacts list.
* Then reset the form
* *** hints:
* *** push: resources/push.jpg
*/
function addContact(event) {
event.preventDefault()
let form = event.target
let contact= {
Id: generateId(),
name: form.name.value,
phoneNumber: form.phoneNumber.value,
emergencyContact: form.emergencyContact.checked
}
contacts.push(contact)
saveContacts()
form.reset()
}
/**
* Converts the contacts array to a JSON string then
* Saves the string to localstorage at the key contacts
*/
function saveContacts() {
window.localStorage.setItem("contacts", JSON.stringify(contacts))
drawContacts()
}
/**
* Attempts to retrieve the contacts string from localstorage
* then parses the JSON string into an array. Finally sets
* the contacts array to the retrieved array
*/
function loadContacts() {
let storedContacts = JSON.parse(window.localStorage.getItem("contacts"))
if (storedContacts) {
contacts = storedContacts
}
}
/**
* This function targets the contacts-list on the
* DOM and adds a new div element for each of the
* contacts in the contacts array
*/
function drawContacts() {
let contactListElement = document.getElementById("contact-list")
let contactsTemplate = ""
contacts.forEach(contact => {
contactsTemplate += `
<div class="contact-card card mt-1 mb-1 ${contact.emergencyContact ?
'emergency-contact' : ''}">
<h3 class="mt-1 mb-1">${contact.name}</h3>
<div class="d-flex space-between">
<p>
<i class="fa fa-fw fa-phone"></i>
<span>${contact.phoneNumber}</span>
</p>
<i class="action fa fa-trash text-danger" onclick="removeContact(${contact.id}
)"></i>
</div>
</div>
`
})
contactListElement.innerHTML = contactsTemplate
}
/**
* This function is called with a contact id
* and will use the id to find and remove the
* contact by their id from the list of contacts
* *** hints:
* *** findIndex: resources/findIndex.jpg
* *** splice: resources/splice.jpg
* @param {string} contactId
*/
function removeContact(contactId) {
let index = contacts.findIndex(contact => contact.id == contactId)
if (index == -1) {
throw new Error("invalid Contact Id")
}
contacts.splice(index, 1)
saveContacts()
}
/**
* Toggles the visibility of the AddContact Form
*/
function toggleAddContactForm() {
document.getElementById('new-contact-form').classList.toggle("hidden")
}
/**
* Used to generate a random string id for mocked
* database generated Id
* @returns {string}
*/
function generateId() {
return Math.floor(Math.random() * 10000000) + "-" + Math.floor(Math.random() * 10000000)
}
loadContacts()
drawContacts()