-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.js
More file actions
38 lines (31 loc) · 1.29 KB
/
main.js
File metadata and controls
38 lines (31 loc) · 1.29 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
const names = document.querySelectorAll("[data-name]");
const value = document.querySelectorAll("[data-revenue]");
const namePodium = document.querySelectorAll("[data-name-podium]");
const valuePodium = document.querySelectorAll("[data-revenue-podium]");
function getFormattedRevenue(revenue) {
const formattedRevenue = ((Math.ceil(revenue / 100) * 100 / 100) / 1000000).toFixed(1);
return `R$ ${formattedRevenue}${revenue < 1000000 ? " K" : "M"}`;
}
const fetchData = () => {
fetch('https://cors-everywhere.onrender.com/https://api.kiwify.com.br/v1/open/competition-ranking')
.then(response => response.json())
.then(data => {
data = JSON.parse(data);
const iterations = data.length > names.length ? names.length : data.length;
for (let i = 0; i < iterations; i++) {
names[i].innerHTML = data[i].competition_username;
value[i].innerHTML = getFormattedRevenue(data[i].revenue);
if (i < namePodium.length && i < valuePodium.length) {
namePodium[i].innerHTML = data[i].competition_username;
valuePodium[i].innerHTML = getFormattedRevenue(data[i].revenue);
}
}
})
.catch(error => console.error(error));
}
window.addEventListener("load", () => {
fetchData();
setInterval(function () {
fetchData();
}, 60 * 1000);
})