-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
176 lines (140 loc) · 4.12 KB
/
script.js
File metadata and controls
176 lines (140 loc) · 4.12 KB
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
const nedb = require('nedb');
const db = new nedb({ filename: 'contacts.db', autoload: true });
let addContact = {
name: '',
number: ''
};
function createContactElement(name, number, id) {
const contactList = document.querySelector('#contact-list');
const contactDiv = document.createElement('div');
const contactNumberAndRemove = document.createElement('div');
const contactName = document.createElement('span');
const contactNumber = document.createElement('span');
const contactRemove = document.createElement('span');
contactDiv.classList.add('contact');
contactName.classList.add('contact-name');
contactRemove.classList.add('contact-remove');
contactName.innerText = name;
contactNumber.innerText = number;
contactRemove.innerText = 'Remove';
const contactRemoveFunction = async () => {
try {
await removeContact(id);
setContacts();
} catch(err) {
console.log(err);
alert('Error on remove contact');
}
};
contactRemove.addEventListener('click', contactRemoveFunction);
contactNumberAndRemove.appendChild(contactNumber);
contactNumberAndRemove.appendChild(contactRemove);
contactDiv.appendChild(contactName);
contactDiv.appendChild(contactNumberAndRemove);
contactList.appendChild(contactDiv);
}
function getContacts() {
return new Promise((next, reject) => {
db.find({}).sort({ name: 1 }).exec((err, results) => {
if(err)
return reject(err);
next(results);
});
});
}
function searchContacts(name) {
return new Promise((next, reject) => {
db.find({ name: {$regex: new RegExp(name, 'i')} }).sort({ name: 1 }).exec((err, results) => {
if(err)
return reject(err);
next(results);
});
});
}
function insertContact(newContact) {
return new Promise((next, reject) => {
db.insert(newContact, err => {
if(err)
return reject(err);
next();
});
});
}
function removeContact(_id) {
return new Promise((next, reject) => {
db.remove({ _id }, {}, (err) => {
if(err)
return reject(err);
next();
});
});
}
function openAddContact() {
const addContactBackground = document.querySelector('#add-contact-background');
addContactBackground.style.display = 'flex';
}
function closeAddContact() {
const addContactBackground = document.querySelector('#add-contact-background');
addContactBackground.style.display = 'none';
}
function cleanAddContact() {
const submitButton = document.querySelector('#submit');
const nameInput = document.querySelector('#name');
const numberInput = document.querySelector('#number');
addContact.name = '';
addContact.number = '';
nameInput.value = '';
numberInput.value = '';
submitButton.style.display = 'none';
}
async function onSearchChange(event) {
const searchText = event.target.value;
try {
const contacts = await searchContacts(searchText);
const contactList = document.querySelector('#contact-list');
contactList.innerHTML = '';
contacts.map(contact => {
createContactElement(contact.name, contact.number, contact._id);
});
} catch(err) {
alert('Error on Search');
console.log(err);
}
}
function onChange(event, parameter) {
const submitButton = document.querySelector('#submit');
addContact[parameter] = event.target.value;
if(!addContact.name || !addContact.number)
return submitButton.style.display = 'none';
submitButton.style.display = 'block';
}
async function onSubmit(event) {
event.preventDefault();
if(!addContact.name || !addContact.number)
return;
try {
await insertContact(addContact);
cleanAddContact();
setContacts();
} catch(err) {
alert('Error on insert contact');
console.log(err);
}
}
async function setContacts() {
try {
const contacts = await getContacts();
const contactList = document.querySelector('#contact-list');
contactList.innerHTML = '';
contacts.map(contact => {
createContactElement(contact.name, contact.number, contact._id);
});
} catch(err) {
alert('Error on set contacts');
console.log(err);
}
}
RegExp.escape = (s) => {
return s.replace(/[-\/\\^$*+?.,()|[\]{}]/g, '\\$&');
};
setContacts();