-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
190 lines (163 loc) · 5.19 KB
/
script.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
let users = [];
let currentUsers = [];
let resultUsers;
const numberOfUsers = 100;
const cardsList = document.querySelector(".users__list");
const form = document.querySelector(".form");
const header = document.querySelector(".header");
const resetButton = document.querySelector(".form__button");
const burgerButton = document.querySelector(".burger");
const sidebar = document.querySelector(".sidebar");
const paginationList = document.querySelector(".pagination-list");
burgerButton.addEventListener("click", () => {
burgerButton.classList.toggle("active");
sidebar.classList.toggle("sidebar--visible");
document.body.classList.toggle("hidden");
});
const statusMessage = {
loading: "img/spinner.svg",
failure: "Technical problems, try again later",
};
const loadingAnimation = document.createElement("img");
function showLoadingStatus() {
loadingAnimation.src = statusMessage.loading;
loadingAnimation.classList.add("status-block");
cardsList.insertAdjacentElement("afterend", loadingAnimation);
}
function loadUsersData() {
showLoadingStatus();
fetch(
`https://randomuser.me/api/?results=${numberOfUsers}&inc=picture,name,dob,gender,location`
)
.then((response) => {
if (response.ok) return response.json();
})
.then((responseJson) => {
users = responseJson.results;
createCards(returnSelectedPage(users));
createPaginationList(users);
})
.catch(() => {
showErrorStautus();
})
.finally(() => {
loadingAnimation.remove();
});
}
function showErrorStautus() {
const errorMessage = document.createElement("div");
errorMessage.classList.add("error-block");
errorMessage.innerHTML = `${statusMessage.failure}`;
cardsList.append(errorMessage);
}
function createCards(cards) {
cardsList.innerHTML = "";
cardsList.innerHTML = cards
.map(
({ picture, name, dob, gender, location }) =>
`<li class="user__item">
<h4 class="user__gender user__gender--${gender}">${gender}</h4>
<img class="user__img" src="${picture.large}" alt="User photo" />
<p class="user__name">${name.first} ${name.last}</p>
<p class="user__age">Age: <span class="user__age-span">${dob.age}</span></p>
<p class="user__location">${location.country}</p>
</li>`
)
.join("");
}
let ifGender = false;
const compareByName = (firstUser, secondUser) =>
firstUser.name.first.toLowerCase() <= secondUser.name.first.toLowerCase()
? -1
: 1;
const compareByAge = (firstUser, secondUser) =>
firstUser.dob.age - secondUser.dob.age;
function filterByName(names, target) {
return names.filter((user) => {
const fullName = `${user.name.first} ${user.name.last}`.toLowerCase();
return fullName.includes(target.toLowerCase());
});
}
function filterUsers() {
resultUsers = [...users];
if (form.search.value !== "") {
resultUsers = filterByName(resultUsers, form.search.value);
}
switch (form.sort.value) {
case "age-up":
resultUsers.sort((a, b) => compareByAge(b, a));
break;
case "age-down":
resultUsers.sort((a, b) => compareByAge(a, b));
break;
case "name-up":
resultUsers.sort((a, b) => compareByName(b, a));
break;
case "name-down":
resultUsers.sort((a, b) => compareByName(a, b));
break;
}
if (form.gender.value === "male" || form.gender.value === "female") {
resultUsers = resultUsers.filter(
(user) => user.gender === form.gender.value
);
ifGender = true;
} else {
resultUsers;
}
createPaginationList(resultUsers);
resultUsers = returnSelectedPage(resultUsers);
}
function resetFilters() {
resultUsers = [...users];
createPaginationList(resultUsers);
resultUsers = returnSelectedPage(resultUsers);
}
const userPerPageNum = 20;
let selectedPage = 1;
let pageNum;
function countPages(friendsCopy) {
if (Number.isInteger(friendsCopy.length / userPerPageNum)) {
pageNum = friendsCopy.length / userPerPageNum;
} else {
pageNum = Math.round(friendsCopy.length / userPerPageNum);
}
}
function createPaginationList(friendsCopy) {
countPages(friendsCopy);
paginationList.innerHTML = "";
for (let i = 1; i <= pageNum; i++) {
const paginationListElem = document.createElement("LI");
paginationListElem.classList.add("pagination-number");
paginationListElem.dataset.pageNum = i;
paginationListElem.innerHTML = i;
if (i == selectedPage) {
paginationListElem.classList.add("selected-page");
}
paginationList.appendChild(paginationListElem);
}
}
function returnSelectedPage(friendsCopy) {
const sliceStart = userPerPageNum * (selectedPage - 1);
const sliceEnd = userPerPageNum + sliceStart;
const slicedUsersArr = friendsCopy.slice(sliceStart, sliceEnd);
if (ifGender) {
selectedPage = 1;
}
createCards(slicedUsersArr);
return slicedUsersArr;
}
document.addEventListener("DOMContentLoaded", function () {
loadUsersData();
form.addEventListener("input", (e) => {
filterUsers();
createCards(resultUsers);
});
resetButton.addEventListener("click", resetFilters);
});
paginationList.addEventListener("click", ({ target }) => {
resultUsers = [...users];
selectedPage = target.dataset.pageNum;
console.log(target);
filterUsers();
});