-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
146 lines (130 loc) · 4.54 KB
/
script.js
File metadata and controls
146 lines (130 loc) · 4.54 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
import {
fetchUserData,
getLanguagesNames,
sortUsersDataByScore,
} from "./logic.js";
let usersData = [];
let nonValidUsers = [];
let languagesNamesFromAll = [];
let selectedLanguage = "overall";
const errorDiv = document.getElementById("error");
//------------- Input User Name -----------------------
const usernameInput = document.getElementById("usernameInput");
const submitButton = document.getElementById("submitButton");
let userNamesArray = [];
submitButton.addEventListener("click", () => {
let userInputData = usernameInput.value;
errorDiv.innerHTML = "";
if (userInputData) {
userNamesArray = userInputData.split(",");
usersData = [];
handleFetchUsersData(userNamesArray);
} else {
showError("no username entered!", "");
}
});
//-------------- Fetch User Data ----------------------
async function handleFetchUsersData(userNamesArray) {
const promises = userNamesArray.map((user) => {
return fetchUserData(user);
});
const results = await Promise.all(promises);
nonValidUsers = [];
for (let i = 0; i < results.length; i++) {
if (results[i].response) {
if (results[i].response.status === 200) {
usersData.push(results[i].data);
} else if (results[i].response.status === 404) {
nonValidUsers.push(results[i].username);
showError(results[i].errorMessege, results[i].username);
console.log(showError(results[i].errorMessege, results[i].username));
}
} else {
const errorMessege =
"Network error. Please check your internet connection.";
showError(errorMessege);
}
}
if (nonValidUsers.length > 0) {
let messege = "these usernames were not found: ";
let names = nonValidUsers.join(", ");
showError(messege, names);
}
languagesNamesFromAll = getLanguagesNames(usersData);
makeLangsSelect(usersData);
renderRanks(usersData, "overall");
}
//----------- Select languages ------------
const select = document.getElementById("languageSelect");
function makeLangsSelect() {
select.innerHTML = "";
const overallOption = document.createElement("option");
overallOption.value = "overall";
overallOption.textContent = "overall";
select.appendChild(overallOption);
languagesNamesFromAll.forEach((lang) => {
const option = document.createElement("option");
option.value = lang;
option.textContent = lang;
select.appendChild(option);
});
}
select.addEventListener("change", () => {
selectedLanguage = select.value;
renderRanks();
});
//----------- Render Ranks ------------
function renderRanks() {
const tableBody = document.querySelector("tbody");
tableBody.innerHTML = "";
if (selectedLanguage === "overall") {
//sort then loop and render
usersData = sortUsersDataByScore(usersData, selectedLanguage);
for (let user = 0; user < usersData.length; user++) {
const row = document.createElement("tr");
tableBody.appendChild(row);
const usernameCell = document.createElement("td");
const clanCell = document.createElement("td");
const scoreCell = document.createElement("td");
usernameCell.textContent = usersData[user].username;
clanCell.textContent = usersData[user].clan;
scoreCell.textContent = usersData[user].ranks.overall.score;
row.appendChild(usernameCell);
row.appendChild(clanCell);
row.appendChild(scoreCell);
if (usersData.length > 1 && user === 0) {
row.className = "top-user";
}
}
} else {
const filteredDataByLang = usersData.filter(
(data) => data.ranks.languages[selectedLanguage],
);
const newsorted = sortUsersDataByScore(
filteredDataByLang,
selectedLanguage,
);
for (let user = 0; user < newsorted.length; user++) {
const row = document.createElement("tr");
tableBody.appendChild(row);
const usernameCell = document.createElement("td");
const clanCell = document.createElement("td");
const scoreCell = document.createElement("td");
usernameCell.textContent = newsorted[user].username;
clanCell.textContent = newsorted[user].clan;
scoreCell.textContent =
newsorted[user].ranks.languages[selectedLanguage].score;
row.appendChild(usernameCell);
row.appendChild(clanCell);
row.appendChild(scoreCell);
if (newsorted.length > 1 && user === 0) {
row.className = "top-user";
}
}
}
}
//----------- error messege function ------------
export function showError(messege, nonValid) {
let usernames = nonValid ? nonValid : "";
return (errorDiv.textContent = `${messege} ${usernames}`);
}