From 62434279760395699118c1dbdabe840e62ad8ea3 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Fri, 26 Nov 2021 21:44:32 -0700 Subject: [PATCH] Index page updates to latest score --- public/scripts/form.js | 2 +- public/scripts/index.js | 130 ++++++++++++---------------------------- 2 files changed, 39 insertions(+), 93 deletions(-) diff --git a/public/scripts/form.js b/public/scripts/form.js index 5132ea9..631b404 100644 --- a/public/scripts/form.js +++ b/public/scripts/form.js @@ -84,7 +84,7 @@ export async function populateDivisions (divisionDropdown, selectedSportID, sele } } -export async function populateTeams(teamDropdown, selectedSportID, selectedTeamID) { +export async function populateTeams(teamDropdown, selectedSportID, selectedTeamID = undefined) { teamDropdown.innerHTML = ""; if(selectedSportID) { diff --git a/public/scripts/index.js b/public/scripts/index.js index 0beced6..180f8b3 100644 --- a/public/scripts/index.js +++ b/public/scripts/index.js @@ -1,4 +1,5 @@ import * as Data from "./data.js"; +import * as Form from "./form.js"; const sportDropdown = document.getElementById('sport-dropdown'); const seasonDropdown = document.getElementById('year-dropdown'); @@ -15,93 +16,48 @@ const manageButton = document.getElementById('manage-button'); -async function listSeasons() { - seasonDropdown.innerHTML = ""; - - const seasonsList = await Data.getSeasons(); +async function initializeForm() { + let latestGame; - seasonsList.forEach(season => { - const option = document.createElement('option'); - option.text = (season.year - 1) + "-" + season.year; - option.value = season.id; - seasonDropdown.appendChild(option); - }); -} -listSeasons(); - -async function listSports() { - sportDropdown.innerHTML = ""; - - const sportsList = await Data.getSports(); - - sportsList.forEach(sport => { - const option = document.createElement('option'); - option.text = sport.name; - option.value = sport.id; - sportDropdown.appendChild(option); - }); - - listGenders(); -} -listSports(); - -async function listGenders() { - genderDropdown.innerHTML = ""; - - const selectedSportID = sportDropdown.value; - const gendersList = await Data.getGenders(selectedSportID); - - if(selectedSportID) { - gendersList.forEach(gender => { - const option = document.createElement('option'); - option.text = (gender.name == "female") ? "Female" : (gender.name == "male") ? "Male" : ""; - option.value = gender.name; - genderDropdown.appendChild(option); - }); + try { + latestGame = await Data.getLatestGame(); + } catch { + latestGame = null; } - listDivisions(); -} + if(latestGame) { + Form.populateSeasons(seasonDropdown, latestGame.seasonID); -async function listDivisions() { - divisionDropdown.innerHTML = ""; - - const selectedSportID = sportDropdown.value; - const selectedGender = genderDropdown.value; - - if(selectedGender) { - const divisionsList = await Data.getDivisions(selectedSportID, selectedGender); - - divisionsList.forEach(division => { - const option = document.createElement('option'); - option.text = division.name; - option.value = division.id; - divisionDropdown.appendChild(option); - }); - } - listTeams(); -} - -async function listTeams() { - teamDropdown.innerHTML = ""; - - const selectedSportID = sportDropdown.value; - - if(selectedSportID) { - const teamsList = await Data.getTeams(selectedSportID); - - teamsList.forEach(team => { - const option = document.createElement('option'); - option.text = team.name; - option.value = team.id; - teamDropdown.appendChild(option); - }); + 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); } - listGames(); -} + sportDropdown.onchange = async () => { + await Form.populateGenders(genderDropdown, sportDropdown.value) + await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value); + await Form.populateTeams(teamDropdown, sportDropdown.value); + }; -async function listGames() { + genderDropdown.onchange = async () => { + await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value); + }; + + loadTable(); + + +} +initializeForm(); + +async function loadTable() { gamesTable.innerHTML = ""; gamesTableHeader.textContent = ""; noScoresMessage.textContent = ""; @@ -146,8 +102,8 @@ async function listGames() { } } } -async function setupGamesTableHeaders() { +async function setupGamesTableHeaders() { const row = document.createElement('tr'); const scoresHeader = document.createElement('th'); @@ -167,16 +123,6 @@ async function setupGamesTableHeaders() { - - -sportDropdown.onchange = (() => { - listGenders(); - listTeams(); -}); -genderDropdown.onchange = listDivisions; -teamDropdown.onchange = listGames; -seasonDropdown.onchange = listGames; - if(addScoreButton) { addScoreButton.addEventListener('click', () => { window.location.href = '/manage/game'; @@ -187,4 +133,4 @@ if(manageButton) { manageButton.addEventListener('click', () => { window.location.href = '/manage' }); -} +} \ No newline at end of file