From b1637d97bc036d94b3e46bdb7a4210fba273c158 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Fri, 26 Nov 2021 13:40:16 -0700 Subject: [PATCH] Add data validity check for adding game --- public/scripts/manage/game.js | 84 ++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/public/scripts/manage/game.js b/public/scripts/manage/game.js index 8338d4c..ce80b97 100644 --- a/public/scripts/manage/game.js +++ b/public/scripts/manage/game.js @@ -28,33 +28,25 @@ async function initializeForm() { Form.addHiddenValue('game', gameID, submissionForm); Form.populateSeasons(seasonDropdown, game.seasonID); - Data.getDivision(game.divisionID) - .then(data => { - Form.populateSports(sportDropdown, data.sportID) - .then(() => { - Form.populateGenders(genderDropdown, sportDropdown.value, data.gender.name) - .then(() => { - Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value, game.divisionID); - }); - Form.populateTeams(team1Dropdown, sportDropdown.value, game.team1ID); - Form.populateTeams(team2Dropdown, sportDropdown.value, game.team2ID); - }); - }); + const data = await Data.getDivision(game.divisionID) + await Form.populateSports(sportDropdown, data.sportID) + await Form.populateGenders(genderDropdown, sportDropdown.value, data.gender.name) + await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value, game.divisionID); + await Form.populateTeams(team1Dropdown, sportDropdown.value, game.team1ID); + await Form.populateTeams(team2Dropdown, sportDropdown.value, game.team2ID); + dateInput.value = game.date; team1ScoreTextbox.value = game.team1Score; team2ScoreTextbox.value = game.team2Score; } else { - Form.populateSeasons(seasonDropdown); - Form.populateSports(sportDropdown) - .then(() => { - Form.populateGenders(genderDropdown, sportDropdown.value) - .then(() => { - Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value); - }); - Form.populateTeams(team1Dropdown, sportDropdown.value); - Form.populateTeams(team2Dropdown, sportDropdown.value); - }); + await Form.populateSeasons(seasonDropdown); + await Form.populateSports(sportDropdown) + await Form.populateGenders(genderDropdown, sportDropdown.value) + await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value); + await Form.populateTeams(team1Dropdown, sportDropdown.value); + await Form.populateTeams(team2Dropdown, sportDropdown.value); + dateInput.value = (new Date()).toISOString().slice(0,10); } seasonDropdown.disabled = false; @@ -66,21 +58,49 @@ async function initializeForm() { team2Dropdown.disabled = false; team1ScoreTextbox.disabled = false; team2ScoreTextbox.disabled = false; - submitButton.disabled = false; - sportDropdown.onchange = () => { - Form.populateGenders(genderDropdown, sportDropdown.value) - .then(() => { - Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value); - }); - Form.populateTeams(team1Dropdown, sportDropdown.value); - Form.populateTeams(team2Dropdown, sportDropdown.value); + sportDropdown.onchange = async () => { + await Form.populateGenders(genderDropdown, sportDropdown.value) + await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value); + await Form.populateTeams(team1Dropdown, sportDropdown.value); + await Form.populateTeams(team2Dropdown, sportDropdown.value); + checkDataValidity(); }; - genderDropdown.onchange = () => { - Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value); + genderDropdown.onchange = async () => { + 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(); +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");