diff --git a/public/scripts/form.js b/public/scripts/form.js index 631b404..96dadc6 100644 --- a/public/scripts/form.js +++ b/public/scripts/form.js @@ -20,11 +20,17 @@ export async function populateSports(sportDropdown, selectedSportID = undefined) if(selectedSportIndex) sportDropdown.selectedIndex = selectedSportIndex; } -export async function populateSeasons(seasonDropdown, selectedSeasonID = undefined) { +export async function populateSeasons(seasonDropdown, selectedSeasonID = undefined, data = undefined) { seasonDropdown.innerHTML = ""; - - const seasonsList = await Data.getSeasons(); + let seasonsList; + + if(data) { + seasonsList = data.seasons; + } else { + seasonsList = await Data.getSeasons(); + } + console.log(data); let currentIndex = 0; let selectedSeasonIndex; seasonsList.forEach(season => { @@ -33,7 +39,7 @@ export async function populateSeasons(seasonDropdown, selectedSeasonID = undefin option.value = season.id; seasonDropdown.appendChild(option); - if(season.id == selectedSeasonID) selectedSeasonIndex = currentIndex; + if(season.id == selectedSeasonID || (data && season.id == data.latestGame.seasonID)) selectedSeasonIndex = currentIndex; currentIndex++; }); diff --git a/public/scripts/index.js b/public/scripts/index.js index 1b70ca3..a30d681 100644 --- a/public/scripts/index.js +++ b/public/scripts/index.js @@ -17,7 +17,15 @@ const manageButton = document.getElementById('manage-button'); async function initializeForm() { - let latestGame; + const data = await (await fetch(`/fetch/index/dropdown`)).json(); + + await Form.populateSeasons(seasonDropdown, null, data); + await Form.populateSports(sportDropdown, null, data); + await Form.populateGenders(genderDropdown, null, data); + await Form.populateDivisions(divisionDropdown, null, null, null, data); + await Form.populateTeams(teamDropdown, null, null, data); + + /*let latestGame; try { latestGame = await Data.getLatestGame(); @@ -39,7 +47,7 @@ async function initializeForm() { await Form.populateGenders(genderDropdown, sportDropdown.value); await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value); await Form.populateTeams(teamDropdown, sportDropdown.value); - } + }*/ seasonDropdown.onchange = loadTable; diff --git a/routes/fetch.js b/routes/fetch.js new file mode 100644 index 0000000..756c6df --- /dev/null +++ b/routes/fetch.js @@ -0,0 +1,60 @@ +var express = require('express'); +var router = express.Router(); + +var sports = require('../database/scores/sports'); +var seasons = require('../database/scores/seasons'); +var genders = require('../database/scores/genders'); +var divisions = require('../database/scores/divisions'); +var teams = require('../database/scores/teams'); +var games = require('../database/scores/games'); +var accounts = require('../database/accounts/accounts'); + +var checkLoginStatus = require('./checkLoginStatus'); + +router.get('/index/dropdown', async function(req, res, next) { + let latestGame; + let seasonsData; + let sportsData; + let gendersData; + let divisionsData; + let teamsData; + + try { + latestGame = await games.getLatest(); + } catch { + latestGame = null; + } + + + try { + if(latestGame) { + let division = await divisions.getFromID(latestGame.divisionID); + + seasonsData = await seasons.retrieveAll(); + sportsData = await sports.retrieveAll(); + gendersData = await genders.retrieveBySport(division.sportID); + divisionsData = await divisions.retrieve(division.sportID); + teamsData = await teams.retrieve(division.sportID); + } else { + seasonsData = await seasons.retrieveAll(); + sportsData = await sports.retrieveAll(); + gendersData = await genders.retrieveBySport(sportsData[0].id); + divisionsData = await divisions.retrieve(sportsData[0].id); + teamsData = await teams.retrieve(sportsData[0].id); + } + + res.json({ + seasons : seasonsData, + sports : sportsData, + genders : gendersData, + divisons : divisionsData, + teams: teamsData, + latestGame + }); + } catch(err) { + console.error("ERROR: " + err.message); + res.status(500).send("An error has occurred"); + } +}); + +module.exports = router; \ No newline at end of file