-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
333 lines (294 loc) · 11.5 KB
/
app.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
const strRegex = /^[a-zA-Z\s]*$/; // containing only letters
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
const phoneRegex = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im;
/* supports following number formats - (123) 456-7890, (123)456-7890, 123-456-7890, 123.456.7890, 1234567890, +31636363634, 075-63546725 */
const digitRegex = /^\d+$/;
// -------------------------------------------------- //
const countryList = document.getElementById('country-list');
const fullscreenDiv = document.getElementById('fullscreen-div');
const modal = document.getElementById('modal');
const addBtn = document.getElementById('add-btn');
const closeBtn = document.getElementById('close-btn');
const modalBtns = document.getElementById('modal-btns');
const form = document.getElementById('modal');
const addrBookList = document.querySelector('#addr-book-list tbody');
// -------------------------------------------------- //
let addrName = firstName = lastName = email = phone = streetAddr = postCode = city = country = labels = "";
// Address class
class Address{
constructor(id, addrName, firstName, lastName, email, phone, streetAddr, postCode, city, country, labels){
this.id = id;
this.addrName = addrName;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.phone = phone;
this.streetAddr = streetAddr;
this.postCode = postCode;
this.city = city;
this.country = country;
this.labels = labels;
}
static getAddresses(){
// from local storage
let addresses;
if(localStorage.getItem('addresses') == null){
addresses = [];
} else {
addresses = JSON.parse(localStorage.getItem('addresses'));
}
return addresses;
}
static addAddress(address){
const addresses = Address.getAddresses();
addresses.push(address);
localStorage.setItem('addresses', JSON.stringify(addresses));
}
static deleteAddress(id){
const addresses = Address.getAddresses();
addresses.forEach((address, index) => {
if(address.id == id){
addresses.splice(index, 1);
}
});
localStorage.setItem('addresses', JSON.stringify(addresses));
form.reset();
UI.closeModal();
addrBookList.innerHTML = "";
UI.showAddressList();
}
static updateAddress(item){
const addresses = Address.getAddresses();
addresses.forEach(address => {
if(address.id == item.id){
address.addrName = item.addrName;
address.firstName = item.firstName;
address.lastName = item.lastName;
address.email = item.email;
address.phone = item.phone;
address.streetAddr = item.streetAddr;
address.postCode = item.postCode;
address.city = item.city;
address.country = item.country;
address.labels = item.labels;
}
});
localStorage.setItem('addresses', JSON.stringify(addresses));
addrBookList.innerHTML = "";
UI.showAddressList();
}
}
// UI class
class UI{
static showAddressList(){
const addresses = Address.getAddresses();
addresses.forEach(address => UI.addToAddressList(address));
}
static addToAddressList(address){
const tableRow = document.createElement('tr');
tableRow.setAttribute('data-id', address.id);
tableRow.innerHTML = `
<td>${address.id}</td>
<td>
<span class = "addressing-name">${address.addrName}</span><br><span class = "address">${address.streetAddr} ${address.postCode} ${address.city} ${address.country}</span>
</td>
<td><span>${address.labels}</span></td>
<td>${address.firstName + " " + address.lastName}</td>
<td>${address.phone}</td>
`;
addrBookList.appendChild(tableRow);
}
static showModalData(id){
const addresses = Address.getAddresses();
addresses.forEach(address => {
if(address.id == id){
form.addr_ing_name.value = address.addrName;
form.first_name.value = address.firstName;
form.last_name.value = address.lastName;
form.email.value = address.email;
form.phone.value = address.phone;
form.street_addr.value = address.streetAddr;
form.postal_code.value = address.postCode;
form.city.value = address.city;
form.country.value = address.country;
form.labels.value = address.labels;
document.getElementById('modal-title').innerHTML = "Change Address Details";
document.getElementById('modal-btns').innerHTML = `
<button type = "submit" id = "update-btn" data-id = "${id}">Update </button>
<button type = "button" id = "delete-btn" data-id = "${id}">Delete </button>
`;
}
});
}
static showModal(){
modal.style.display = "block";
fullscreenDiv.style.display = "block";
}
static closeModal(){
modal.style.display = "none";
fullscreenDiv.style.display = "none";
}
}
// DOM Content Loaded
window.addEventListener('DOMContentLoaded', () => {
loadJSON(); // loading country list from json file
eventListeners();
UI.showAddressList();
});
// event listeners
function eventListeners(){
// show add item modal
addBtn.addEventListener('click', () => {
form.reset();
document.getElementById('modal-title').innerHTML = "Add Address";
UI.showModal();
document.getElementById('modal-btns').innerHTML = `
<button type = "submit" id = "save-btn"> Save </button>
`;
});
// close add item modal
closeBtn.addEventListener('click', UI.closeModal);
// add an address item
modalBtns.addEventListener('click', (event) => {
event.preventDefault();
if(event.target.id == "save-btn"){
let isFormValid = getFormData();
if(!isFormValid){
form.querySelectorAll('input').forEach(input => {
setTimeout(() => {
input.classList.remove('errorMsg');
}, 1500);
});
} else {
let allItem = Address.getAddresses();
let lastItemId = (allItem.length > 0) ? allItem[allItem.length - 1].id : 0;
lastItemId++;
const addressItem = new Address(lastItemId, addrName, firstName, lastName, email, phone, streetAddr, postCode, city, country, labels);
Address.addAddress(addressItem);
UI.closeModal();
UI.addToAddressList(addressItem);
form.reset();
}
}
});
// table row items
addrBookList.addEventListener('click', (event) => {
UI.showModal();
let trElement;
if(event.target.parentElement.tagName == "TD"){
trElement = event.target.parentElement.parentElement;
}
if(event.target.parentElement.tagName == "TR"){
trElement = event.target.parentElement;
}
let viewID = trElement.dataset.id;
UI.showModalData(viewID);
});
// delete an address item
modalBtns.addEventListener('click', (event) => {
if(event.target.id == 'delete-btn'){
Address.deleteAddress(event.target.dataset.id);
}
});
// update an address item
modalBtns.addEventListener('click', (event) => {
event.preventDefault();
if(event.target.id == "update-btn"){
let id = event.target.dataset.id;
let isFormValid = getFormData();
if(!isFormValid){
form.querySelectorAll('input').forEach(input => {
setTimeout(() => {
input.classList.remove('errorMsg');
}, 1500);
});
} else {
const addressItem = new Address(id, addrName, firstName, lastName, email, phone, streetAddr, postCode, city, country, labels);
Address.updateAddress(addressItem);
UI.closeModal();
form.reset();
}
}
});
}
// load countries list
function loadJSON(){
fetch('countries.json')
.then(response => response.json())
.then(data => {
let html = "";
data.forEach(country => {
html += `
<option> ${country.country} </option>
`;
});
countryList.innerHTML = html;
})
}
// get form data
function getFormData(){
let inputValidStatus = [];
// console.log(form.addr_ing_name.value, form.first_name.value, form.last_name.value, form.email.value, form.phone.value, form.street_addr.value, form.postal_code.value, form.city.value, form.country.value, form.labels.value);
if(!strRegex.test(form.addr_ing_name.value) || form.addr_ing_name.value.trim().length == 0){
addErrMsg(form.addr_ing_name);
inputValidStatus[0] = false;
} else {
addrName = form.addr_ing_name.value;
inputValidStatus[0] = true;
}
if(!strRegex.test(form.first_name.value) || form.first_name.value.trim().length == 0){
addErrMsg(form.first_name);
inputValidStatus[1] = false;
} else {
firstName = form.first_name.value;
inputValidStatus[1] = true;
}
if(!strRegex.test(form.last_name.value) || form.last_name.value.trim().length == 0){
addErrMsg(form.last_name);
inputValidStatus[2] = false;
} else {
lastName = form.last_name.value;
inputValidStatus[2] = true;
}
if(!emailRegex.test(form.email.value)){
addErrMsg(form.email);
inputValidStatus[3] = false;
} else {
email = form.email.value;
inputValidStatus[3] = true;
}
if(!phoneRegex.test(form.phone.value)){
addErrMsg(form.phone);
inputValidStatus[4] = false;
} else {
phone = form.phone.value;
inputValidStatus[4] = true;
}
if(!(form.street_addr.value.trim().length > 0)){
addErrMsg(form.street_addr);
inputValidStatus[5] = false;
} else {
streetAddr = form.street_addr.value;
inputValidStatus[5] = true;
}
if(!digitRegex.test(form.postal_code.value)){
addErrMsg(form.postal_code);
inputValidStatus[6] = false;
} else {
postCode = form.postal_code.value;
inputValidStatus[6] = true;
}
if(!strRegex.test(form.city.value) || form.city.value.trim().length == 0){
addErrMsg(form.city);
inputValidStatus[7] = false;
} else {
city = form.city.value;
inputValidStatus[7] = true;
}
country = form.country.value;
labels = form.labels.value;
return inputValidStatus.includes(false) ? false : true;
}
function addErrMsg(inputBox){
inputBox.classList.add('errorMsg');
}