-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
137 lines (123 loc) · 4.45 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
const URL = 'http://localhost:3000/contacs';
// fetch(URL)
// .then(res => res.json())
// .then(data => console.log(data))
// .catch(err => console.log(err));
window.onload = function () {
let tbody = document.querySelector('#tbody');
fetch(URL)
.then(res => res.json())
.then(data => {
data.forEach(contact => {
//console.log(contact.name);
createelement(contact, tbody);
});
})
.catch(error => {
console.log(error);
})
let saveContact = document.getElementById('saveContact');
saveContact.addEventListener('click', () => {
createContact();
})
}
function createContact() {
let nameFiled = document.getElementById('nameFiled');
let emailFiled = document.getElementById('emailFiled');
let phoneFiled = document.getElementById('phoneFiled');
// console.log(nameFiled.value);
// console.log(emailFiled.value);
// console.log(phoneFiled.value);
let contact = {
name: nameFiled.value,
email: emailFiled.value,
phone: phoneFiled.value
}
// console.log(contact);
fetch(URL, {
method: 'POST',
body: JSON.stringify(contact),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(res => res.json())
.then(data => {
let tbody = document.querySelector('#tbody');
//console.log(data);
createelement(data, tbody);
nameFiled.value = '';
emailFiled.value = '';
phoneFiled.value = '';
})
.catch(err => console.log(err))
}
function createelement(contact, parentelement) {
const tr = document.createElement('tr');
const tdName = document.createElement('td');
tdName.innerHTML = contact.name;
tr.appendChild(tdName);
const tdEmail = document.createElement('td');
tdEmail.innerHTML = contact.email ? contact.email : 'N/A';
tr.appendChild(tdEmail);
const tdPhone = document.createElement('td');
tdPhone.innerHTML = contact.phone ? contact.phone : 'N/A';
tr.appendChild(tdPhone);
const tdActions = document.createElement('td');
const tdEditButton = document.createElement('button');
tdEditButton.className = 'btn btn-warning mx-1';
tdEditButton.innerHTML = 'Edit';
tdEditButton.addEventListener('click', () => {
let mainModal = $('#contactEditModal')
mainModal.modal('toggle')
let editName = document.getElementById('edit-name');
let editEmail = document.getElementById('edit-email');
let editPhone = document.getElementById('edit-phone');
editName.value = contact.name;
editEmail.value = contact.email ? contact.email : '';
editPhone.value = contact.phone ? contact.phone : '';
let updatebtn = document.getElementById('updateContact');
//console.log(updateData);
updatebtn.addEventListener('click', () => {
let updateData = {
name: editName.value,
email: editEmail.value,
phone: editPhone.value
}
fetch(`${URL}/${contact.id}`, {
method: 'PUT',
body: JSON.stringify(updateData),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(res => res.json())
.then(data => {
tdName.innerHTML = data.name;
// console.log(data.name);
tdEmail.innerHTML = data.email;
tdPhone.innerHTML = data.phone;
mainModal.modal('hide');