refactor: address review feedback — extract date helper, rename listener
This commit is contained in:
@@ -40,15 +40,14 @@ function initChartsIn(rootEl) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupTooltipsAfterSwap() {
|
function setupAfterTableSwap() {
|
||||||
document.body.addEventListener('htmx:afterSwap', function(event) {
|
document.body.addEventListener('htmx:afterSwap', function(event) {
|
||||||
const target = event.detail.target;
|
const target = event.detail.target;
|
||||||
if (target.id === 'ratings-table') {
|
if (target.id === 'ratings-table') {
|
||||||
initRatingsTooltips();
|
initRatingsTooltips();
|
||||||
initChartsIn(target); // initial table render — chart any pre-loaded .player-chart
|
initChartsIn(target);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// refreshRatingHistory still re-fetches into #history-content-<id>
|
|
||||||
if (target.id && target.id.startsWith('history-content-')) {
|
if (target.id && target.id.startsWith('history-content-')) {
|
||||||
initChartsIn(target);
|
initChartsIn(target);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ const { getPlayerFromDB, savePlayerToDB, getRatingHistoryFromDB, saveRatingHisto
|
|||||||
const { fetchPlayerDataHTTP, parsePlayerData, fetchRatingHistory, parseRatingHistory } = require('../scrapers/player-http');
|
const { fetchPlayerDataHTTP, parsePlayerData, fetchRatingHistory, parseRatingHistory } = require('../scrapers/player-http');
|
||||||
const { getOfficialRatingHistory, getOptimizedPlayerRounds } = require('../scrapers/player-puppeteer');
|
const { getOfficialRatingHistory, getOptimizedPlayerRounds } = require('../scrapers/player-puppeteer');
|
||||||
const { launchBrowser } = require('../scrapers/browser');
|
const { launchBrowser } = require('../scrapers/browser');
|
||||||
const { getPlayerDataFromDB, scrapePDGARating, getAllRatingsFromDB, refreshAllPlayersInDB, getPredictedRatingFromDB } = require('../services/player-service');
|
const { getPlayerDataFromDB, scrapePDGARating, getAllRatingsFromDB, refreshAllPlayersInDB, getPredictedRatingFromDB, formatDisplayDate } = require('../services/player-service');
|
||||||
const { getTopbarLocals } = require('../services/topbar-service');
|
const { getTopbarLocals } = require('../services/topbar-service');
|
||||||
const { calculatePredictedRating } = require('../services/rating-calculator');
|
const { calculatePredictedRating } = require('../services/rating-calculator');
|
||||||
const logger = require('../logger');
|
const logger = require('../logger');
|
||||||
@@ -63,7 +63,7 @@ router.get('/partials/player-history/:pdgaNumber', async (req, res) => {
|
|||||||
const formattedHistory = (history || []).map(row => ({
|
const formattedHistory = (history || []).map(row => ({
|
||||||
date: row.date,
|
date: row.date,
|
||||||
rating: row.rating,
|
rating: row.rating,
|
||||||
displayDate: new Date(row.date).toLocaleDateString('en-US', { day: '2-digit', month: 'short', year: 'numeric' })
|
displayDate: formatDisplayDate(row.date)
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const player = await getPlayerDataFromDB(pdgaNumber);
|
const player = await getPlayerDataFromDB(pdgaNumber);
|
||||||
@@ -119,11 +119,7 @@ router.get('/api/rating-history/:pdgaNumber', async (req, res) => {
|
|||||||
const formattedHistory = cachedHistory.map(row => ({
|
const formattedHistory = cachedHistory.map(row => ({
|
||||||
date: row.date,
|
date: row.date,
|
||||||
rating: row.rating,
|
rating: row.rating,
|
||||||
displayDate: new Date(row.date).toLocaleDateString('en-US', {
|
displayDate: formatDisplayDate(row.date)
|
||||||
day: '2-digit',
|
|
||||||
month: 'short',
|
|
||||||
year: 'numeric'
|
|
||||||
})
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
|
|||||||
@@ -4,6 +4,12 @@ const { fetchPlayerDataHTTP, parsePlayerData } = require('../scrapers/player-htt
|
|||||||
const { calculatePredictedRating } = require('./rating-calculator');
|
const { calculatePredictedRating } = require('./rating-calculator');
|
||||||
const logger = require('../logger');
|
const logger = require('../logger');
|
||||||
|
|
||||||
|
function formatDisplayDate(dateStr) {
|
||||||
|
return new Date(dateStr).toLocaleDateString('en-US', {
|
||||||
|
day: '2-digit', month: 'short', year: 'numeric'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Derives previous-month rating and the delta to it. Prefers PDGA's reported
|
// Derives previous-month rating and the delta to it. Prefers PDGA's reported
|
||||||
// rating_change (canonical), falls back to our own monthly snapshots when
|
// rating_change (canonical), falls back to our own monthly snapshots when
|
||||||
// rating_change is missing — common for players whose latest scrape failed.
|
// rating_change is missing — common for players whose latest scrape failed.
|
||||||
@@ -194,9 +200,7 @@ async function getAllRatingsFromDB(progressCallback = null) {
|
|||||||
playerData.ratingHistory = rawHistory.map(row => ({
|
playerData.ratingHistory = rawHistory.map(row => ({
|
||||||
date: row.date,
|
date: row.date,
|
||||||
rating: row.rating,
|
rating: row.rating,
|
||||||
displayDate: new Date(row.date).toLocaleDateString('en-US', {
|
displayDate: formatDisplayDate(row.date)
|
||||||
day: '2-digit', month: 'short', year: 'numeric'
|
|
||||||
})
|
|
||||||
}));
|
}));
|
||||||
// Re-derive now that history is attached — bulk path skipped includeMonthlyHistory
|
// Re-derive now that history is attached — bulk path skipped includeMonthlyHistory
|
||||||
const derived = deriveMonthlyDeltas(playerData.rating, player.rating_change, playerData.monthlyHistory);
|
const derived = deriveMonthlyDeltas(playerData.rating, player.rating_change, playerData.monthlyHistory);
|
||||||
@@ -354,5 +358,6 @@ module.exports = {
|
|||||||
getPredictedRatingFromDB,
|
getPredictedRatingFromDB,
|
||||||
getAllRatingsFromDB,
|
getAllRatingsFromDB,
|
||||||
refreshAllPlayersInDB,
|
refreshAllPlayersInDB,
|
||||||
computeKpis
|
computeKpis,
|
||||||
|
formatDisplayDate
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -109,7 +109,7 @@
|
|||||||
activePage: 'players',
|
activePage: 'players',
|
||||||
cssFiles: ['players.css'],
|
cssFiles: ['players.css'],
|
||||||
jsFiles: ['tooltips.js', 'chart.js', 'players.js'],
|
jsFiles: ['tooltips.js', 'chart.js', 'players.js'],
|
||||||
initScript: 'setupTooltipsAfterSwap();',
|
initScript: 'setupAfterTableSwap();',
|
||||||
body: body,
|
body: body,
|
||||||
modals: modals
|
modals: modals
|
||||||
}) %>
|
}) %>
|
||||||
|
|||||||
Reference in New Issue
Block a user