Populate all dropdowns in a single AJAX request

main
sudoer777 2022-03-07 21:31:04 -07:00
parent 1e78e37913
commit b2f66b3e34
3 changed files with 44 additions and 41 deletions

View File

@ -51,10 +51,16 @@ export async function populateSeasons(seasonDropdown, selectedSeasonID = undefin
if(selectedSeasonIndex) seasonDropdown.selectedIndex = selectedSeasonIndex; if(selectedSeasonIndex) seasonDropdown.selectedIndex = selectedSeasonIndex;
} }
export async function populateGenders(genderDropdown, selectedSportID, selectedGender = undefined) { export async function populateGenders(genderDropdown, selectedSportID, selectedGender = undefined, data = undefined) {
genderDropdown.innerHTML = ""; genderDropdown.innerHTML = "";
const gendersList = await Data.getGenders(selectedSportID); let gendersList;
if(data) {
gendersList = data.genders;
selectedSportID = data.latestGame.sportID;
} else {
gendersList = await Data.getGenders(selectedSportID);
}
if(selectedSportID) { if(selectedSportID) {
let currentIndex = 0; let currentIndex = 0;
@ -65,7 +71,7 @@ export async function populateGenders(genderDropdown, selectedSportID, selectedG
option.value = gender.name; option.value = gender.name;
genderDropdown.appendChild(option); genderDropdown.appendChild(option);
if(gender.name == selectedGender) selectedGenderIndex = currentIndex; if(gender.name == selectedGender || (data && gender.name == data.latestGame.gender.name)) selectedGenderIndex = currentIndex;
currentIndex++; currentIndex++;
}); });
@ -73,11 +79,21 @@ export async function populateGenders(genderDropdown, selectedSportID, selectedG
} }
} }
export async function populateDivisions (divisionDropdown, selectedSportID, selectedGender, selectedDivisionID = undefined) { export async function populateDivisions (divisionDropdown, selectedSportID, selectedGender, selectedDivisionID = undefined, data = undefined) {
divisionDropdown.innerHTML = ""; divisionDropdown.innerHTML = "";
if(data) {
selectedSportID = data.latestGame.sportID;
selectedGender = data.latestGame.gender;
}
if(selectedSportID && selectedGender) { if(selectedSportID && selectedGender) {
const divisionsList = await Data.getDivisions(selectedSportID, selectedGender); let divisionsList;
if(data) {
divisionsList = data.divisions;
} else {
divisionsList = await Data.getDivisions(selectedSportID, selectedGender);
}
let currentIndex = 0; let currentIndex = 0;
let selectedDivisionIndex; let selectedDivisionIndex;
@ -87,7 +103,7 @@ export async function populateDivisions (divisionDropdown, selectedSportID, sele
option.value = division.id; option.value = division.id;
divisionDropdown.appendChild(option); divisionDropdown.appendChild(option);
if(division.id == selectedDivisionID) selectedDivisionIndex = currentIndex; if(division.id == selectedDivisionID || (data && division.id == data.latestGame.divisionID)) selectedDivisionIndex = currentIndex;
currentIndex++; currentIndex++;
}); });
@ -95,11 +111,20 @@ export async function populateDivisions (divisionDropdown, selectedSportID, sele
} }
} }
export async function populateTeams(teamDropdown, selectedSportID, selectedTeamID = undefined) { export async function populateTeams(teamDropdown, selectedSportID, selectedTeamID = undefined, data = undefined) {
teamDropdown.innerHTML = ""; teamDropdown.innerHTML = "";
if(data) {
selectedSportID = data.latestGame.sportID;
}
if(selectedSportID) { if(selectedSportID) {
const teamsList = await Data.getTeams(selectedSportID); let teamsList;
if(data) {
teamsList = data.teams;
} else {
teamsList = await Data.getTeams(selectedSportID);
}
let currentIndex = 0; let currentIndex = 0;
let selectedTeamIndex; let selectedTeamIndex;
@ -109,7 +134,7 @@ export async function populateTeams(teamDropdown, selectedSportID, selectedTeamI
option.value = team.id; option.value = team.id;
teamDropdown.appendChild(option); teamDropdown.appendChild(option);
if(team.id == selectedTeamID) selectedTeamIndex = currentIndex; if(team.id == selectedTeamID || (data && team.id == data.latestGame.team1ID)) selectedTeamIndex = currentIndex;
currentIndex++; currentIndex++;
}); });

View File

@ -21,34 +21,10 @@ async function initializeForm() {
await Form.populateSeasons(seasonDropdown, null, data); await Form.populateSeasons(seasonDropdown, null, data);
await Form.populateSports(sportDropdown, null, data); await Form.populateSports(sportDropdown, null, data);
await Form.populateGenders(genderDropdown, null, data); await Form.populateGenders(genderDropdown, null, null, data);
await Form.populateDivisions(divisionDropdown, null, null, null, data); await Form.populateDivisions(divisionDropdown, null, null, null, data);
await Form.populateTeams(teamDropdown, null, null, data); await Form.populateTeams(teamDropdown, null, null, data);
/*let latestGame;
try {
latestGame = await Data.getLatestGame();
} catch {
latestGame = null;
}
if(latestGame) {
Form.populateSeasons(seasonDropdown, latestGame.seasonID);
const division = await Data.getDivision(latestGame.divisionID);
await Form.populateSports(sportDropdown, division.sportID);
await Form.populateGenders(genderDropdown, sportDropdown.value, division.gender.name);
await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value, latestGame.divisionID);
await Form.populateTeams(teamDropdown, sportDropdown.value, latestGame.team1ID);
} else {
Form.populateSeasons(seasonDropdown);
await Form.populateSports(sportDropdown);
await Form.populateGenders(genderDropdown, sportDropdown.value);
await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value);
await Form.populateTeams(teamDropdown, sportDropdown.value);
}*/
seasonDropdown.onchange = loadTable; seasonDropdown.onchange = loadTable;
sportDropdown.onchange = async () => { sportDropdown.onchange = async () => {

View File

@ -21,6 +21,10 @@ router.get('/index/dropdown', async function(req, res, next) {
try { try {
latestGame = await games.getLatest(); latestGame = await games.getLatest();
let division = await divisions.getFromID(latestGame.divisionID);
latestGame.sportID = division.sportID;
latestGame.gender = division.gender;
} catch { } catch {
latestGame = null; latestGame = null;
} }
@ -28,13 +32,11 @@ router.get('/index/dropdown', async function(req, res, next) {
try { try {
if(latestGame) { if(latestGame) {
let division = await divisions.getFromID(latestGame.divisionID);
seasonsData = await seasons.retrieveAll(); seasonsData = await seasons.retrieveAll();
sportsData = await sports.retrieveAll(); sportsData = await sports.retrieveAll();
gendersData = await genders.retrieveBySport(division.sportID); gendersData = await genders.retrieveBySport(latestGame.sportID);
divisionsData = await divisions.retrieve(division.sportID); divisionsData = await divisions.retrieve(latestGame.sportID);
teamsData = await teams.retrieve(division.sportID); teamsData = await teams.retrieve(latestGame.sportID);
} else { } else {
seasonsData = await seasons.retrieveAll(); seasonsData = await seasons.retrieveAll();
sportsData = await sports.retrieveAll(); sportsData = await sports.retrieveAll();
@ -47,7 +49,7 @@ router.get('/index/dropdown', async function(req, res, next) {
seasons : seasonsData, seasons : seasonsData,
sports : sportsData, sports : sportsData,
genders : gendersData, genders : gendersData,
divisons : divisionsData, divisions : divisionsData,
teams: teamsData, teams: teamsData,
latestGame latestGame
}); });