7e5fa6cbf1
- Add HTMX CDN to layout - Replace client-side table rendering (displayRatings, displayCourses) with server-rendered EJS partials via hx-get - Add server-side course search with debounced hx-trigger - Lazy-load player history and course layouts via htmx.ajax() - Render rating chart via htmx:afterSwap with data attributes - Add partial routes: ratings-table, course-table, player-history, course-layouts
98 lines
4.3 KiB
JavaScript
98 lines
4.3 KiB
JavaScript
function fetchRatingsWithProgress() {
|
|
const progressSection = document.getElementById('progress-section');
|
|
const progressBar = document.getElementById('progress-bar');
|
|
const progressText = document.getElementById('progress-text');
|
|
const tableDiv = document.getElementById('ratings-table');
|
|
|
|
progressSection.style.display = 'block';
|
|
tableDiv.innerHTML = '';
|
|
|
|
const eventSource = new EventSource('/api/ratings/progress');
|
|
|
|
eventSource.onmessage = function(event) {
|
|
const data = JSON.parse(event.data);
|
|
|
|
if (data.status === 'loading') {
|
|
const percentage = Math.round((data.current / data.total) * 100);
|
|
progressBar.style.width = `${percentage}%`;
|
|
progressBar.textContent = `${percentage}%`;
|
|
progressText.textContent = `Loading player ${data.current}/${data.total}: PDGA #${data.pdgaNumber}`;
|
|
} else if (data.status === 'completed') {
|
|
const percentage = Math.round((data.current / data.total) * 100);
|
|
progressBar.style.width = `${percentage}%`;
|
|
progressBar.textContent = `${percentage}%`;
|
|
progressText.textContent = `Loaded ${data.name} (${data.current}/${data.total})`;
|
|
} else if (data.status === 'error') {
|
|
const percentage = Math.round((data.current / data.total) * 100);
|
|
progressBar.style.width = `${percentage}%`;
|
|
progressBar.textContent = `${percentage}%`;
|
|
progressText.textContent = `Error loading PDGA #${data.pdgaNumber} (${data.current}/${data.total})`;
|
|
} else if (data.status === 'complete') {
|
|
progressSection.style.display = 'none';
|
|
htmx.ajax('GET', '/partials/ratings-table', '#ratings-table');
|
|
eventSource.close();
|
|
}
|
|
};
|
|
|
|
eventSource.onerror = function() {
|
|
progressSection.style.display = 'none';
|
|
tableDiv.textContent = 'Connection error. Please refresh the page.';
|
|
eventSource.close();
|
|
};
|
|
}
|
|
|
|
function loadAllPlayers() {
|
|
const button = document.getElementById('load-all-btn');
|
|
const originalText = button.textContent;
|
|
button.textContent = 'Loading...';
|
|
button.style.pointerEvents = 'none';
|
|
|
|
try {
|
|
const progressSection = document.getElementById('progress-section');
|
|
const progressBar = document.getElementById('progress-bar');
|
|
const progressText = document.getElementById('progress-text');
|
|
const tableDiv = document.getElementById('ratings-table');
|
|
|
|
progressSection.style.display = 'block';
|
|
tableDiv.textContent = '';
|
|
|
|
const eventSource = new EventSource('/api/load-all-players');
|
|
|
|
eventSource.onmessage = function(event) {
|
|
const data = JSON.parse(event.data);
|
|
|
|
if (data.status === 'loading' || data.status === 'completed' || data.status === 'error') {
|
|
const percentage = Math.round((data.current / data.total) * 100);
|
|
progressBar.style.width = `${percentage}%`;
|
|
progressBar.textContent = `${percentage}%`;
|
|
|
|
if (data.status === 'loading') {
|
|
progressText.textContent = `Loading player ${data.current}/${data.total}: PDGA #${data.pdgaNumber}`;
|
|
} else if (data.status === 'completed') {
|
|
progressText.textContent = `Loaded ${data.name} (${data.current}/${data.total})`;
|
|
} else if (data.status === 'error') {
|
|
progressText.textContent = `Error loading PDGA #${data.pdgaNumber} (${data.current}/${data.total})`;
|
|
}
|
|
} else if (data.status === 'complete') {
|
|
progressSection.style.display = 'none';
|
|
htmx.ajax('GET', '/partials/ratings-table', '#ratings-table');
|
|
eventSource.close();
|
|
button.textContent = originalText;
|
|
button.style.pointerEvents = 'auto';
|
|
}
|
|
};
|
|
|
|
eventSource.onerror = function() {
|
|
progressSection.style.display = 'none';
|
|
tableDiv.textContent = 'Connection error. Please refresh the page.';
|
|
eventSource.close();
|
|
button.textContent = originalText;
|
|
button.style.pointerEvents = 'auto';
|
|
};
|
|
} catch (error) {
|
|
console.error('Error loading all players:', error);
|
|
button.textContent = originalText;
|
|
button.style.pointerEvents = 'auto';
|
|
}
|
|
}
|