-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
187 lines (162 loc) · 5.26 KB
/
script.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
const addBtn = document.getElementById('submit-btn');
const cancelBtn = document.getElementById('cancel-btn');
const resetBtn = document.getElementById('reset-btn');
const recordContainer = document.querySelector('.record-container');
const deleteBtn = document.getElementById('delete-btn');
/************************************************ */
const name = document.getElementById('name');
const address = document.getElementById('address');
const number = document.getElementById('contact-num');
let ContactArray = [];
let id = 0;
// Object constructor for Contact
function Contact(id, name, address, number){
this.id = id;
this.name = name;
this.address = address;
this.number = number;
}
// Display available record
document.addEventListener('DOMContentLoaded', function(){
if(localStorage.getItem('contacts') == null){
ContactArray = [];
} else {
ContactArray = JSON.parse(localStorage.getItem('contacts'));
lastID(ContactArray);
}
displayRecord();
});
// Display Function
function displayRecord(){
ContactArray.forEach(function(singleContact){
addToList(singleContact);
});
}
// Finding the last id
function lastID(ContactArray){
if(ContactArray.length > 0){
id = ContactArray[ContactArray.length - 1].id;
} else {
id = 0;
}
}
// Adding contact record
addBtn.addEventListener('click', function(){
if(checkInputFields([name, address, number])){
setMessage("success", "Record added successfully!");
id++;
const contact = new Contact(id, name.value, address.value, number.value);
ContactArray.push(contact);
// Storing contact record in local storage
localStorage.setItem('contacts', JSON.stringify(ContactArray));
clearInputFields();
// Adding to list
addToList(contact);
} else {
setMessage("error", "Empty input fields or invalid input!");
}
});
// Adding to List (on the DOM)
function addToList(item){
const newRecordDiv = document.createElement('div');
newRecordDiv.classList.add('record-item');
newRecordDiv.innerHTML = `
<div class = "record-el">
<span id = "labelling">Contact ID: </span>
<span id = "contact-id-content">${item.id}</span>
</div>
<div class = "record-el">
<span id = "labelling">Name: </span>
<span id = "name-content">${item.name}</span>
</div>
<div class = "record-el">
<span id = "labelling">Address: </span>
<span id = "address-content">${item.address}</span>
</div>
<div class = "record-el">
<span id = "labelling">Contact Number: </span>
<span id = "contact-num-content"><a href="tel:${item.number}">${item.number}</a></span>
</div>
<button type = "button" id = "delete-btn">
<span>
<i class = "fas fa-trash"></i>
</span> Delete
</button>
`;
recordContainer.appendChild(newRecordDiv);
}
// Deletion of record
recordContainer.addEventListener('click', function(event){
//console.log(event.target);
if(event.target.id === 'delete-btn'){
// removing from DOM
let recordItem = event.target.parentElement;
recordContainer.removeChild(recordItem);
let tempContactList = ContactArray.filter(function(record){
return (record.id !== parseInt(recordItem.firstElementChild.lastElementChild.textContent));
});
ContactArray = tempContactList;
//removing from localstorage by overwriting
localStorage.setItem('contacts', JSON.stringify(ContactArray));
}
});
// resetting everything (id will get set to 0)
resetBtn.addEventListener('click', function(){
ContactArray = [];
localStorage.setItem('contacts', JSON.stringify(ContactArray));
location.reload();
})
// Displaying status/alerts
function setMessage(status, message){
let messageBox = document.querySelector('.message');
if(status == "error"){
messageBox.innerHTML = `${message}`;
messageBox.classList.add('error');
removeMessage(status, messageBox);
}
if(status == "success"){
messageBox.innerHTML = `${message}`;
messageBox.classList.add('success');
removeMessage(status, messageBox);
}
}
// Clearing all input fields
cancelBtn.addEventListener('click', function(){
clearInputFields();
});
function clearInputFields(){
name.value = "";
address.value = "";
number.value = "";
}
// Removing status/alerts
function removeMessage(status, messageBox){
setTimeout(function(){
messageBox.classList.remove(`${status}`);
}, 2000);
}
// Input field validation
function checkInputFields(inputArr){
for(let i = 0; i < inputArr.length; i++){
if(inputArr[i].value === ""){
return false;
}
}
if(!phoneNumCheck(inputArr[2].value)){
return false;
}
return true;
}
// Phone number validation function
function phoneNumCheck(inputtxt){
let phoneNo = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
if(inputtxt.match(phoneNo)){
return true;
} else {
return false;
}
}
// Works for the following formats:
// XXX-XXX-XXXX
// XXX.XXX.XXXX
// XXX XXX XXXX