Add data validity check for adding game

main
sudoer777 2021-11-26 13:40:16 -07:00
parent cceda089c0
commit b1637d97bc
1 changed files with 52 additions and 32 deletions

View File

@ -28,33 +28,25 @@ async function initializeForm() {
Form.addHiddenValue('game', gameID, submissionForm); Form.addHiddenValue('game', gameID, submissionForm);
Form.populateSeasons(seasonDropdown, game.seasonID); Form.populateSeasons(seasonDropdown, game.seasonID);
Data.getDivision(game.divisionID) const data = await Data.getDivision(game.divisionID)
.then(data => { await Form.populateSports(sportDropdown, data.sportID)
Form.populateSports(sportDropdown, data.sportID) await Form.populateGenders(genderDropdown, sportDropdown.value, data.gender.name)
.then(() => { await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value, game.divisionID);
Form.populateGenders(genderDropdown, sportDropdown.value, data.gender.name) await Form.populateTeams(team1Dropdown, sportDropdown.value, game.team1ID);
.then(() => { await Form.populateTeams(team2Dropdown, sportDropdown.value, game.team2ID);
Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value, game.divisionID);
});
Form.populateTeams(team1Dropdown, sportDropdown.value, game.team1ID);
Form.populateTeams(team2Dropdown, sportDropdown.value, game.team2ID);
});
});
dateInput.value = game.date; dateInput.value = game.date;
team1ScoreTextbox.value = game.team1Score; team1ScoreTextbox.value = game.team1Score;
team2ScoreTextbox.value = game.team2Score; team2ScoreTextbox.value = game.team2Score;
} }
else { else {
Form.populateSeasons(seasonDropdown); await Form.populateSeasons(seasonDropdown);
Form.populateSports(sportDropdown) await Form.populateSports(sportDropdown)
.then(() => { await Form.populateGenders(genderDropdown, sportDropdown.value)
Form.populateGenders(genderDropdown, sportDropdown.value) await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value);
.then(() => { await Form.populateTeams(team1Dropdown, sportDropdown.value);
Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value); await Form.populateTeams(team2Dropdown, sportDropdown.value);
});
Form.populateTeams(team1Dropdown, sportDropdown.value);
Form.populateTeams(team2Dropdown, sportDropdown.value);
});
dateInput.value = (new Date()).toISOString().slice(0,10); dateInput.value = (new Date()).toISOString().slice(0,10);
} }
seasonDropdown.disabled = false; seasonDropdown.disabled = false;
@ -66,21 +58,49 @@ async function initializeForm() {
team2Dropdown.disabled = false; team2Dropdown.disabled = false;
team1ScoreTextbox.disabled = false; team1ScoreTextbox.disabled = false;
team2ScoreTextbox.disabled = false; team2ScoreTextbox.disabled = false;
submitButton.disabled = false;
sportDropdown.onchange = () => { sportDropdown.onchange = async () => {
Form.populateGenders(genderDropdown, sportDropdown.value) await Form.populateGenders(genderDropdown, sportDropdown.value)
.then(() => { await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value);
Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value); await Form.populateTeams(team1Dropdown, sportDropdown.value);
}); await Form.populateTeams(team2Dropdown, sportDropdown.value);
Form.populateTeams(team1Dropdown, sportDropdown.value); checkDataValidity();
Form.populateTeams(team2Dropdown, sportDropdown.value);
}; };
genderDropdown.onchange = () => { genderDropdown.onchange = async () => {
Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value); await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value);
checkDataValidity();
}; };
dateInput.addEventListener('keyup', checkDataValidity);
team1Dropdown.onchange = checkDataValidity;
team1ScoreTextbox.addEventListener('keyup', checkDataValidity);
team2Dropdown.onchange = checkDataValidity;
team2ScoreTextbox.addEventListener('keyup', checkDataValidity);
checkDataValidity();
} }
initializeForm(); initializeForm();
async function checkDataValidity() {
let dataIsValid = true;
const seasonHasContent = seasonDropdown.options.length;
const sportHasContent = sportDropdown.options.length;
const genderHasContent = genderDropdown.options.length;
const divisionHasContent = divisionDropdown.options.length;
const team1HasContent = team1Dropdown.options.length;
const team2HasContent = team2Dropdown.options.length;
if(!seasonHasContent || !sportHasContent || !genderHasContent || !divisionHasContent || !team1HasContent || !team2HasContent) dataIsValid = false;
if(team1Dropdown.selectedIndex == team2Dropdown.selectedIndex) dataIsValid = false;
if(team1ScoreTextbox.value == "" || team2ScoreTextbox.value == "") dataIsValid = false;
if(dateInput.value == "") dataIsValid = false;
submitButton.disabled = !dataIsValid;
}
Form.addRemoveFunction(deleteButton, submissionForm, "game"); Form.addRemoveFunction(deleteButton, submissionForm, "game");