From 36de9b37734c66275a91d7a567acde4f60bc4a59 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 7 Mar 2022 23:07:26 -0700 Subject: [PATCH] Speed up loading for home page --- public/scripts/index.js | 6 +++--- routes/fetch.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/public/scripts/index.js b/public/scripts/index.js index ae7bd01..3d77b89 100644 --- a/public/scripts/index.js +++ b/public/scripts/index.js @@ -60,7 +60,8 @@ async function loadTable() { if(selectedTeamID && selectedDivisionID) { gamesTableHeader.textContent = `Scores for ${teamDropdown.options[teamDropdown.selectedIndex].text}`; - const gamesList = await Data.getGames(selectedTeamID, selectedDivisionID, selectedSeasonID); + const requestURL = `/fetch/index/scores?team=${+selectedTeamID}&division=${+selectedDivisionID}&season=${+selectedSeasonID}`; + const gamesList = await (await fetch(requestURL)).json(); if(gamesList.length > 0) { await setupGamesTableHeaders(); @@ -77,8 +78,7 @@ async function loadTable() { row.appendChild(scoreCell); const opponentCell = document.createElement('td'); - Data.getTeam(game.team2ID) - .then(data => opponentCell.textContent = data.name); + opponentCell.textContent = game.opponent.name; row.appendChild(opponentCell); const dateCell = document.createElement('td'); diff --git a/routes/fetch.js b/routes/fetch.js index cbe203a..5abc17d 100644 --- a/routes/fetch.js +++ b/routes/fetch.js @@ -59,4 +59,20 @@ router.get('/index/dropdown', async function(req, res, next) { } }); +router.get('/index/scores', async function (req, res, next) { + try { + const seasonID = req.query.season; + const divisionID = req.query.division; + const teamID = req.query.team; + const data = await games.retrieve(teamID, divisionID, seasonID); + for (const game of data) { + game.opponent = await teams.getFromID(game.team2ID); + } + res.json(data); + } catch(err) { + console.error("ERROR: " + err.message); + res.status(500).send("An error has occurred"); + } +}); + module.exports = router; \ No newline at end of file