This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathpagination.js
143 lines (132 loc) · 3.68 KB
/
pagination.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
// PAGNATION
const BASE_URL = 'https://opencodeiiita.herokuapp.com/get-all-data/?page=1';
const prev = document.getElementById('prev');
const next = document.getElementById('next');
const current = document.getElementById('current');
var pointSet = new Set();
var currentPage = 1;
var nextPage = 2;
var prevPage = 0;
var totalPages = 100;
function getData(url, currPage) {
loadingOn();
currentPage = currPage;
prevPage = currentPage - 1;
if (prevPage == 0) document.getElementById('prev').classList.add('disabled');
else document.getElementById('prev').classList.remove('disabled');
fetch(url)
.then(res => res.json())
.then(pageData => {
currentPage = pageData.page_number;
return (participantsData = pageData.data);
})
.then(data => {
data.forEach(elem => {
pointSet.add(elem.totalPoints);
});
return data;
})
.then(data => {
addToTable(data);
});
document.getElementById('current').innerHTML = `${currentPage}`;
nextPage = currentPage + 1;
totalPages = 250;
}
function addToTable(arr) {
let markup = ``;
let rank = 0;
for (var i = 0; i < arr.length; i++) {
let user = arr[i].username;
let points = arr[i].totalPoints;
let fullName = arr[i].name;
rank = [...pointSet].indexOf(points) + 1;
if(rank==1){
markup +=
'<tr style="background-color:#ff5500; color:white;"><td>' +
'<i class="fas fa-medal" style="color:#FFD700;"></i> '+
rank +
'</td><td> ' +
' ' +
user +
'</td><td> ' +
' ' +
points +
'</td><td> ' +
' ' +
fullName +
'</td></tr>';
}
else if(rank==2){
markup +=
'<tr style="background-color:#ff6f27; color:white;"><td>' +
'<i class="fas fa-medal" style="color:#C0C0C0;"></i> '+
rank +
'</td><td> ' +
' ' +
user +
'</td><td> ' +
' ' +
points +
'</td><td> ' +
' ' +
fullName +
'</td></tr>';
}
else if(rank==3){
markup +=
'<tr style="background-color: #ff884c; color:white;"><td>' +
'<i class="fas fa-medal" style="color:#8C7853;"></i> '+
rank +
'</td><td> ' +
' ' +
user +
'</td><td> ' +
' ' +
points +
'</td><td> ' +
' ' +
fullName +
'</td></tr>';
}
else{
markup +=
'<tr><td>' +
rank +
'</td><td> ' +
' ' +
user +
'</td><td> ' +
' ' +
points +
'</td><td> ' +
' ' +
fullName +
'</td></tr>';
}
document.getElementById('participantsList').innerHTML = markup;
}
loadingOff();
}
getData(BASE_URL, currentPage);
next.addEventListener('click', () => {
if (nextPage < totalPages) pageCall(nextPage);
});
prev.addEventListener('click', () => {
console.log(prevPage);
if (prevPage > 0) pageCall(prevPage);
});
function pageCall(page_number) {
url = `https://opencodeiiita.herokuapp.com/get-all-data/?page=${page_number}`;
getData(url, page_number);
}
function loadingOn() {
document.querySelector('#participantsList').style.opacity = 0;
document.querySelector('.participants-table-spinner-container').classList.add('d-block')
document.querySelector('.participants-table-spinner-container').classList.remove('d-none')
}
function loadingOff() {
document.querySelector('#participantsList').style.opacity = 1;
document.querySelector('.participants-table-spinner-container').classList.add('d-none')
document.querySelector('.participants-table-spinner-container').classList.remove('d-block')
}