Get seasons from a single request

main
sudoer777 2022-03-07 12:21:17 -07:00
parent 0402209290
commit cc66467197
3 changed files with 80 additions and 6 deletions

View File

@ -20,11 +20,17 @@ export async function populateSports(sportDropdown, selectedSportID = undefined)
if(selectedSportIndex) sportDropdown.selectedIndex = selectedSportIndex; if(selectedSportIndex) sportDropdown.selectedIndex = selectedSportIndex;
} }
export async function populateSeasons(seasonDropdown, selectedSeasonID = undefined) { export async function populateSeasons(seasonDropdown, selectedSeasonID = undefined, data = undefined) {
seasonDropdown.innerHTML = ""; 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 currentIndex = 0;
let selectedSeasonIndex; let selectedSeasonIndex;
seasonsList.forEach(season => { seasonsList.forEach(season => {
@ -33,7 +39,7 @@ export async function populateSeasons(seasonDropdown, selectedSeasonID = undefin
option.value = season.id; option.value = season.id;
seasonDropdown.appendChild(option); seasonDropdown.appendChild(option);
if(season.id == selectedSeasonID) selectedSeasonIndex = currentIndex; if(season.id == selectedSeasonID || (data && season.id == data.latestGame.seasonID)) selectedSeasonIndex = currentIndex;
currentIndex++; currentIndex++;
}); });

View File

@ -17,7 +17,15 @@ const manageButton = document.getElementById('manage-button');
async function initializeForm() { 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 { try {
latestGame = await Data.getLatestGame(); latestGame = await Data.getLatestGame();
@ -39,7 +47,7 @@ async function initializeForm() {
await Form.populateGenders(genderDropdown, sportDropdown.value); await Form.populateGenders(genderDropdown, sportDropdown.value);
await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value); await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value);
await Form.populateTeams(teamDropdown, sportDropdown.value); await Form.populateTeams(teamDropdown, sportDropdown.value);
} }*/
seasonDropdown.onchange = loadTable; seasonDropdown.onchange = loadTable;

60
routes/fetch.js 100644
View File

@ -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;