-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
298 lines (239 loc) · 7.82 KB
/
Copy pathscript.js
File metadata and controls
298 lines (239 loc) · 7.82 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
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
const URL = `https://dummyjson.com/users`;
// ELEMENTS
const modal = document.getElementById("modal");
const searchInp = document.getElementById("searchInput");
const srchInpCrss = document.getElementById("srchInpCrss");
const perPage = document.getElementById("perPage");
const gotoInp = document.getElementById("gotoInp");
const gotoBtn = document.getElementById("gotoBtn");
const prvBtn = document.getElementById("prvBtn");
const nxtBtn = document.getElementById("nxtBtn");
const pageBtns = document.getElementById("pageBtns");
const table = document.querySelector("table");
const showingPage = document.getElementById("showingPage");
// VARIABLES
let isLoading = false;
let perPageVal = +perPage.value;
let skipped = 0;
let gotoPageVal = 1;
let total = 208;
let noofPages = Math.ceil(total / perPageVal);
let users = [];
let currPage = 1;
// FUNCTIONS
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const getUsers = async (limit = perPageVal, skip = skipped, search = "") => {
try {
const base = search ? `${URL}/search?q=${search}&` : `${URL}?`;
const res = await fetch(
`${base}limit=${limit}&skip=${skip}&select=firstName,lastName,email,gender`
);
if (!res.ok) throw new Error("res not okay");
const data = await res.json();
total = data.total;
users = data.users;
users.forEach((u) => {
u.status = u.gender.toLowerCase() === "male" ? "Active" : "Inactive";
});
noofPages = Math.ceil(total / perPageVal);
} catch (err) {
console.log(err.message);
}
await makeTable();
pageNums();
};
(load = async () => {
await getUsers();
updatePrvBtn(currPage > 1);
updateNxtBtn(currPage < noofPages);
})();
const closeModal = () => {
modal.style.display = "none";
};
const pageNums = () => {
pageBtns.innerHTML = "";
let start = Math.max(1, Math.min(currPage - 4, noofPages - 8));
let end = Math.min(noofPages, start + 8);
for (let i = start; i <= end; i++) {
const pageBtn = document.createElement("button");
pageBtn.innerText = i;
pageBtn.classList.add("goto");
pageBtn.value = i;
if (+i === currPage) pageBtn.classList.add("activePage");
pageBtn.addEventListener("click", () => {
currPage = +pageBtn.value;
skipped = (currPage - 1) * perPageVal;
gotoPage(currPage);
});
pageBtns.appendChild(pageBtn);
}
showingPage.innerText = `Page ${currPage} of ${Math.ceil(
total / perPageVal
)}`;
};
const switchLoading = () => {
isLoading = !isLoading;
};
const clearSearchInp = () => {
searchInp.value = "";
srchInpCrss.style.display = "none";
currPage = 1;
skipped = 0;
getUsers();
};
const showSearchInpValue = async () => {
const val = searchInp.value.toLowerCase();
srchInpCrss.style.display = val ? "inline-block" : "none";
currPage = 1;
skipped = 0;
await getUsers(perPageVal, skipped, val);
updatePrvBtn(currPage > 1);
updateNxtBtn(noofPages > 1);
};
const updateShowPerPage = () => {
perPageVal = +perPage.value;
currPage = 1;
skipped = 0;
gotoPage(currPage);
};
const updateGotoPageInp = () => {
gotoPage(+gotoInp.value);
gotoInp.value = "";
};
const updateStatus = async (user, tdStatus, statusBtn) => {
await delay(500);
const statusSpan = tdStatus.querySelector(".status");
const pulse = statusSpan.querySelector(".pulse");
const statusText = statusSpan.querySelector(".status-text");
if (user.status === "Active") {
user.status = "Inactive";
pulse.classList.remove("active");
pulse.classList.add("inactive");
} else {
user.status = "Active";
pulse.classList.remove("inactive");
pulse.classList.add("active");
}
// update status text
statusText.innerText = user.status;
// update button text
statusBtn.innerText = user.status === "Active" ? "Deactivate" : "Activate";
// update button color
statusBtn.style.backgroundColor = user.status === "Active" ? "red" : "green";
};
const deleteUser = async (user, tr) => {
try {
const res = await fetch(`https://dummyjson.com/users/${user.id}`, {
method: `DELETE`,
});
const data = await res.json();
if (data.isDeleted) {
tr.remove();
users = users.filter((u) => u.id !== user.id);
pageNums();
} else {
alert(`Unable to delete ${user.firstName}`);
}
} catch (err) {
console.log(err.message);
}
};
const updatePrvBtn = (prev) => {
prvBtn.disabled = !prev;
};
const updateNxtBtn = (next) => {
nxtBtn.disabled = !next;
};
const gotoPage = async (page) => {
if (page < 1 || page > noofPages) return;
isLoading = true;
currPage = page;
skipped = perPageVal * (currPage - 1);
await getUsers(perPageVal, skipped, searchInp.value.toLowerCase());
isLoading = false;
updatePrvBtn(currPage > 1);
updateNxtBtn(currPage < noofPages);
};
const goPrevPage = async () => {
if (currPage === 1 || isLoading) return;
await gotoPage(currPage - 1);
};
const goNextPage = async () => {
if (currPage === noofPages || isLoading) return;
await gotoPage(currPage + 1);
};
const makeTable = async (list = users) => {
table.innerHTML = "";
if (!list || list.length === 0) {
const trMsg = document.createElement("tr");
const tdMsg = document.createElement("td");
tdMsg.innerText = "No users to show";
tdMsg.colSpan = 5;
tdMsg.style.textAlign = "center";
trMsg.append(tdMsg);
table.appendChild(trMsg);
updatePrvBtn(false);
updateNxtBtn(false);
pageNums();
return;
}
const trFragment = document.createDocumentFragment();
const headTr = document.createElement("tr");
headTr.innerHTML = `<th>Id</th>
<th>Name</th>
<th>Mail</th>
<th>Status</th>
<th>Action</th>`;
trFragment.appendChild(headTr);
for (let user of list) {
const newTr = document.createElement("tr");
const tdId = document.createElement("td");
const tdFName = document.createElement("td");
const tdMail = document.createElement("td");
const tdStatus = document.createElement("td");
const statusSpan = document.createElement("span");
const statusPulse = document.createElement("span");
const tdAction = document.createElement("td");
const statusBtn = document.createElement("button");
const dltBtn = document.createElement("button");
tdId.innerText = user.id;
tdFName.innerText = user.firstName + " " + user.lastName;
tdMail.innerText = user.email;
statusSpan.classList.add("status");
statusPulse.classList.add("pulse");
statusPulse.classList.add(user.status === "Active" ? "active" : "inactive");
const statusText = document.createElement("span");
statusText.classList.add("status-text");
statusText.innerText = user.status;
statusSpan.append(statusPulse, statusText);
tdStatus.append(statusSpan);
dltBtn.classList.add("dltBtn");
dltBtn.innerText = "Delete";
statusBtn.classList.add("statusBtn");
statusBtn.classList.add(
user.status === "Active" ? "deactivate" : "activate"
);
statusBtn.innerText = `${
user.status === "Active" ? "Deactivate" : "Activate"
}`;
statusBtn.style.backgroundColor =
statusBtn.innerText === "Activate" ? "green" : "red";
tdAction.append(dltBtn, statusBtn);
newTr.append(tdId, tdFName, tdMail, tdStatus, tdAction);
statusBtn.addEventListener("click", () =>
updateStatus(user, tdStatus, statusBtn)
);
dltBtn.addEventListener("click", () => deleteUser(user, newTr));
trFragment.appendChild(newTr);
}
table.appendChild(trFragment);
};
// FUNCTION VALUES
const debouncedSearchInp = debounce(showSearchInpValue, 500);
// EVENT LISTENERS
searchInp.addEventListener("input", debouncedSearchInp);
srchInpCrss.addEventListener("click", clearSearchInp);
perPage.addEventListener("change", updateShowPerPage);
gotoBtn.addEventListener("click", updateGotoPageInp);
prvBtn.addEventListener("click", goPrevPage);
nxtBtn.addEventListener("click", goNextPage);