// archives.js - Handles the archived leaderboards functionality document.addEventListener('DOMContentLoaded', () => { // DOM Elements const currentViewBtn = document.getElementById('current-view-btn'); const archivesViewBtn = document.getElementById('archives-view-btn'); const currentView = document.getElementById('current-view'); const archivesView = document.getElementById('archives-view'); const weekSelect = document.getElementById('week-select'); const startDateInput = document.getElementById('start-date'); const endDateInput = document.getElementById('end-date'); const searchArchivesBtn = document.getElementById('search-archives-btn'); const archiveResults = document.getElementById('archive-results'); // View toggle functionality currentViewBtn.addEventListener('click', () => { currentViewBtn.classList.add('active'); archivesViewBtn.classList.remove('active'); currentView.classList.remove('hidden'); archivesView.classList.add('hidden'); }); archivesViewBtn.addEventListener('click', () => { archivesViewBtn.classList.add('active'); currentViewBtn.classList.remove('active'); archivesView.classList.remove('hidden'); currentView.classList.add('hidden'); // Load archived weeks when switching to archives view loadArchivedWeeks(); }); // Load archived weeks for the dropdown async function loadArchivedWeeks() { try { const response = await fetch('/api/archives/weeks'); if (!response.ok) throw new Error('Failed to fetch archived weeks'); const weeks = await response.json(); // Clear previous options except the default one weekSelect.innerHTML = ''; if (weeks.length === 0) { const option = document.createElement('option'); option.disabled = true; option.textContent = 'No archives available'; weekSelect.appendChild(option); } else { weeks.forEach(weekId => { const option = document.createElement('option'); option.value = weekId; option.textContent = weekId; weekSelect.appendChild(option); }); } } catch (error) { console.error('Error loading archived weeks:', error); archiveResults.innerHTML = `
Error loading archived weeks: ${error.message}
`; } } // Load archived data for a specific week async function loadArchivedWeek(weekId) { try { const response = await fetch(`/api/archives/week/${weekId}`); if (!response.ok) throw new Error('Failed to fetch archived data'); const archive = await response.json(); displayArchivedData(archive); } catch (error) { console.error('Error loading archived week:', error); archiveResults.innerHTML = `Error loading archived data: ${error.message}
`; } } // Load archived data for a date range async function loadArchivedRange(startDate, endDate) { try { const response = await fetch(`/api/archives/range?startDate=${startDate}&endDate=${endDate}`); if (!response.ok) throw new Error('Failed to fetch archived data'); const archives = await response.json(); if (archives.length === 0) { archiveResults.innerHTML = 'No archives found for the specified date range
'; return; } // Display the first archive in the range displayArchivedData(archives[0], archives.length > 1 ? archives.length : null); } catch (error) { console.error('Error loading archived range:', error); archiveResults.innerHTML = `Error loading archived data: ${error.message}
`; } } // Display archived data function displayArchivedData(archive, totalArchives = null) { // Create archive info section let html = `Week ID: ${archive.weekId}
Period: ${archive.startDate} to ${archive.endDate}
Archived: ${new Date(archive.archivedAt).toLocaleString()}
${totalArchives ? `Note: Showing 1 of ${totalArchives} archives in the selected range
` : ''}Select a week or date range to view archived leaderboards
'; } }); searchArchivesBtn.addEventListener('click', () => { const startDate = startDateInput.value; const endDate = endDateInput.value; if (!startDate || !endDate) { alert('Please select both start and end dates'); return; } if (new Date(startDate) > new Date(endDate)) { alert('Start date must be before end date'); return; } loadArchivedRange(startDate, endDate); }); });