2024-03-22 23:50:10 +00:00
|
|
|
async function fetchLeaderboardData() {
|
2024-03-25 09:40:43 +00:00
|
|
|
const response = await fetch('/api/user/tests/');
|
2024-03-22 23:50:10 +00:00
|
|
|
if (!response.ok) {
|
|
|
|
console.error('Failed to fetch leaderboard data');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
const tableBody = document.getElementById('leaderboardTable').getElementsByTagName('tbody')[0];
|
|
|
|
tableBody.innerHTML = ''; // Clear existing rows
|
|
|
|
data.forEach(item => {
|
|
|
|
const row = tableBody.insertRow();
|
|
|
|
row.insertCell(0).innerText = item.username;
|
|
|
|
row.insertCell(1).innerText = item.wpm;
|
|
|
|
row.insertCell(2).innerText = item.accuracy;
|
|
|
|
row.insertCell(3).innerText = item.test_time;
|
|
|
|
row.insertCell(4).innerText = item.test_length;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure this function is called on page load and when the refresh button is clicked.
|
|
|
|
document.addEventListener('DOMContentLoaded', fetchLeaderboardData);
|
|
|
|
document.getElementById('refreshButton').addEventListener('click', fetchLeaderboardData);
|