Populate all dropdowns in a single AJAX request
parent
1e78e37913
commit
b2f66b3e34
|
@ -51,11 +51,17 @@ export async function populateSeasons(seasonDropdown, selectedSeasonID = undefin
|
|||
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 = "";
|
||||
|
||||
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) {
|
||||
let currentIndex = 0;
|
||||
let selectedGenderIndex;
|
||||
|
@ -65,7 +71,7 @@ export async function populateGenders(genderDropdown, selectedSportID, selectedG
|
|||
option.value = gender.name;
|
||||
genderDropdown.appendChild(option);
|
||||
|
||||
if(gender.name == selectedGender) selectedGenderIndex = currentIndex;
|
||||
if(gender.name == selectedGender || (data && gender.name == data.latestGame.gender.name)) selectedGenderIndex = 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 = "";
|
||||
|
||||
if(data) {
|
||||
selectedSportID = data.latestGame.sportID;
|
||||
selectedGender = data.latestGame.gender;
|
||||
}
|
||||
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 selectedDivisionIndex;
|
||||
|
@ -87,7 +103,7 @@ export async function populateDivisions (divisionDropdown, selectedSportID, sele
|
|||
option.value = division.id;
|
||||
divisionDropdown.appendChild(option);
|
||||
|
||||
if(division.id == selectedDivisionID) selectedDivisionIndex = currentIndex;
|
||||
if(division.id == selectedDivisionID || (data && division.id == data.latestGame.divisionID)) selectedDivisionIndex = 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 = "";
|
||||
|
||||
if(data) {
|
||||
selectedSportID = data.latestGame.sportID;
|
||||
}
|
||||
|
||||
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 selectedTeamIndex;
|
||||
|
@ -109,7 +134,7 @@ export async function populateTeams(teamDropdown, selectedSportID, selectedTeamI
|
|||
option.value = team.id;
|
||||
teamDropdown.appendChild(option);
|
||||
|
||||
if(team.id == selectedTeamID) selectedTeamIndex = currentIndex;
|
||||
if(team.id == selectedTeamID || (data && team.id == data.latestGame.team1ID)) selectedTeamIndex = currentIndex;
|
||||
currentIndex++;
|
||||
});
|
||||
|
||||
|
|
|
@ -21,33 +21,9 @@ async function initializeForm() {
|
|||
|
||||
await Form.populateSeasons(seasonDropdown, 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.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;
|
||||
|
||||
|
|
|
@ -21,6 +21,10 @@ router.get('/index/dropdown', async function(req, res, next) {
|
|||
|
||||
try {
|
||||
latestGame = await games.getLatest();
|
||||
|
||||
let division = await divisions.getFromID(latestGame.divisionID);
|
||||
latestGame.sportID = division.sportID;
|
||||
latestGame.gender = division.gender;
|
||||
} catch {
|
||||
latestGame = null;
|
||||
}
|
||||
|
@ -28,13 +32,11 @@ router.get('/index/dropdown', async function(req, res, next) {
|
|||
|
||||
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);
|
||||
gendersData = await genders.retrieveBySport(latestGame.sportID);
|
||||
divisionsData = await divisions.retrieve(latestGame.sportID);
|
||||
teamsData = await teams.retrieve(latestGame.sportID);
|
||||
} else {
|
||||
seasonsData = await seasons.retrieveAll();
|
||||
sportsData = await sports.retrieveAll();
|
||||
|
@ -47,7 +49,7 @@ router.get('/index/dropdown', async function(req, res, next) {
|
|||
seasons : seasonsData,
|
||||
sports : sportsData,
|
||||
genders : gendersData,
|
||||
divisons : divisionsData,
|
||||
divisions : divisionsData,
|
||||
teams: teamsData,
|
||||
latestGame
|
||||
});
|
||||
|
|
Reference in New Issue