-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
224 lines (205 loc) · 8.32 KB
/
index.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
const userUrl = `http://localhost:3000/users`
const userContainer = document.querySelector('#user-container')
const searchBar = document.getElementById('searchBar');
const userForm = document.querySelector('#user-form')
var allUsers = []
const loadUsers = async () => { // using const instead of function to create function because It makes the function immutable, so you don't have to worry about that function being changed by some other piece of code.
const res = await fetch(`${userUrl}`);
allUsers = await res.json();
displayUsers(allUsers);
};
//--------------------------------Search and Display------------------------------------------------------------------------------------------------------------------------------------------------------------------------
searchBar.addEventListener('keyup', (e) => {
const searchString = e.target.value.toLowerCase();
const filteredUsers = allUsers.filter((user) => {
return (
// user.is_active==false
user.name.toLowerCase().includes(searchString) ||
user.email.toLowerCase().includes(searchString) ||
user.phone.toLowerCase().includes(searchString)
);
})
displayUsers(filteredUsers)
})
// TODO: Push the selected label of filters to top to show which option is selected, Refer commented function below
// $(function(){
// $("#dropdown-menu-role li a").click(function(){
// $(".btn:first-child").text($(this).text());
// });
// });
function statusFilter(filter) {
if (filter == "any") {
displayUsers(allUsers)
}
if (filter == "active") {
const filteredUsers = allUsers.filter((user) => {
return (
user.is_active == true
);
})
displayUsers(filteredUsers)
}
if (filter == "inActive") {
const filteredUsers = allUsers.filter((user) => {
return (
user.is_active == false
);
})
displayUsers(filteredUsers)
}
if (filter == "admin") {
const filteredUsers = allUsers.filter((user) => {
return (
user.role == "ADMIN"
);
})
displayUsers(filteredUsers)
}
if (filter == "member") {
const filteredUsers = allUsers.filter((user) => {
return (
user.role == "MEMBER"
);
})
displayUsers(filteredUsers)
}
if (filter == "guest") {
const filteredUsers = allUsers.filter((user) => {
return (
user.role == "GUEST"
);
})
displayUsers(filteredUsers)
}
}
// [const | let | var] = function () {} (or () =>
// Is the creation of an anonymous function (function () {}) and the creation of a variable, and then the assignment of that anonymous function to that variable.
// So the usual rules around variable hoisting within a scope -- block-scoped variables (let and const) do not hoist as undefined to the top of their block scope.
const displayUsers = (users) => {
const htmlString = users
.map((user) => {
return `
<li class="user">
<div id=${user.id}>
<img class="avatar" src="${user.avatar}" alt="Avatar" >
<h5 id='user-name'>${user.name}</h5>
<h5>${user.email}</h5>
<h5 class="user-address">${user.address}</h5>
<h5>${user.phone}</h5>
<h5>${user.role}</h5>
<div id="modify-buttons">
<button data-id="${user.id}" id="edit-${user.id}" data-action="edit" class="btn btn-small btn-secondary active btn-block">Edit</button>
<button data-id="${user.id}" id="delete-${user.id}" data-action="delete" type="button" class="btn btn-small active btn-danger btn-block" >Delete</button>
</div>
</div>
<div id=edit-user-${user.id}>
</div>
</li>`
}
)
.join('');
userContainer.innerHTML = htmlString;
// ----------------------------------------------------Show user status in form of colors as green for active and default for inActive---------------------------------------------------------------------------------------
var div_list = document.querySelectorAll('.avatar') // querySelectorAll returns a Nodelist object which is similar to an array but NOT an array
var div_array = [...div_list];
div_array.forEach((div, index) => {
if (users[index].is_active === true) {
div.style.border = '10px solid rgb(108, 166, 111)';
}
else {
div.style.border = '10px solid #581d38';
}
});
}
loadUsers();
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Adding user, Create(C) operation...
document.getElementById("addUser").addEventListener("click", ()=>{
$('#newUserModal').modal('show')
});
// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Editing user/UpdateU) and Deleting User Delete(D) operation....
userContainer.addEventListener('click', (e) => {
if (e.target.dataset.action === 'edit') {
const userData = allUsers.find((user) => {
return user.id == e.target.dataset.id
})
var form = document.getElementById("editForms");
form["Id"].value = userData.id;
form["editName"].value = userData.name;
form["editEmail"].value = userData["email"];
form["editAddress"].value = userData["address"];
form["editPhone"].value = userData["phone"];
form["editRole"].value = userData["role"];
form["editStatus"].value = userData["is_active"]
$('#editModal').modal('show')
// end of this event listener for edit
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Deleting user, Delete() operation
} else if (e.target.dataset.action === 'delete') {
$('#deleteModal').modal('show')
$('#deleteModal .modal-footer button').on('click', function (event) {
var $button = $(event.target);
$(this).closest('.modal').one('hidden.bs.modal', function () {
if ($button[0].id == "confirm-delete") {
deleteUser(e.target.dataset.id)
}
else {
console.log('cancel button pressed')
}
});
});
}
}) // end of eventListener delete
//Crud completed
// Functions----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function deleteUser(targetDatasetId) {
document.querySelector(`#user-${targetDatasetId}`)
fetch(`${userUrl}/${targetDatasetId}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
}
}).then(response => response.json())
.then(window.location.reload());
}
function editUser(formData) {
let status= true
// console.log(typeof status)
if(formData["editStatus"].value==="false"){
status=false
}
fetch(`${userUrl}/${formData["Id"].value}`, {
method: 'PATCH',
body: JSON.stringify({
name: formData["editName"].value,
email: formData["editEmail"].value,
phone: formData["editPhone"].value,
address: formData["editAddress"].value,
is_active: status,
role: formData["editRole"].value,
}),
headers: {
'Content-Type': 'application/json'
}
}).then(response => response.json())
.then(window.location.reload()); // refresh the page after the edit is made.
}
function addNewUser(formData) {
fetch(`${userUrl}`, {
method: 'POST',
body: JSON.stringify({
name: formData["addName"].value,
email: formData["addEmail"].value,
phone: formData["addPhone"].value,
address: formData["addAddress"].value,
is_active: formData["addStatus"].value,
role: formData["addRole"].value
}),
headers: {
'Content-Type': 'application/json'
}
}).then(response => response.json())
.then(window.location.reload()); // refresh the page after the add is made.
}
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------