From 9b7ec5f3d679fa0e13f028900dc4c1ba8e88e0e8 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 16:48:03 -0700 Subject: [PATCH] Add function to list divisions in submit form --- public/scripts/data.js | 6 ++++++ public/scripts/submit.js | 26 +++++++++++++++++++++++++- routes/data.js | 8 ++++++++ views/submit.pug | 3 --- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/public/scripts/data.js b/public/scripts/data.js index cccd7cf..27ba099 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -14,4 +14,10 @@ export async function getGenders(sportID) { const response = await fetch(`/data/genders?sport=${+sportID}`); const gendersList = await response.json(); return gendersList; +} + +export async function getDivisions(sportID, gender) { + const response = await fetch(`/data/divisions?sport=${+sportID}&gender=${gender}`); + const divisionsList = await response.json(); + return divisionsList; } \ No newline at end of file diff --git a/public/scripts/submit.js b/public/scripts/submit.js index e51fda6..85fc410 100644 --- a/public/scripts/submit.js +++ b/public/scripts/submit.js @@ -4,6 +4,7 @@ import * as Data from "./data.js"; const sportDropdown = document.getElementById('sport-dropdown'); const seasonDropdown = document.getElementById('year-dropdown'); const genderDropdown = document.getElementById('gender-dropdown'); +const divisionDropdown = document.getElementById('division-dropdown'); @@ -18,6 +19,8 @@ async function listSports() { option.value = sport.id; sportDropdown.appendChild(option); }); + + listSeasons(); } listSports(); @@ -45,10 +48,31 @@ async function listGenders() { option.value = gender.name; genderDropdown.appendChild(option); }); + + listDivisions(); +} + +async function listDivisions() { + divisionDropdown.innerHTML = ""; + + const selectedSportID = sportDropdown.value; + const selectedGender = genderDropdown.value; + + if(!selectedGender) return; + + 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); + }) } -sportDropdown.onchange = listGenders; \ No newline at end of file +sportDropdown.onchange = listGenders; +genderDropdown.onchange = listDivisions; \ No newline at end of file diff --git a/routes/data.js b/routes/data.js index 8b4ea87..68a7e6d 100644 --- a/routes/data.js +++ b/routes/data.js @@ -3,6 +3,7 @@ var router = express.Router(); var sports = require('../database/scores/sports'); var seasons = require('../database/scores/seasons'); var genders = require('../database/scores/genders'); +var divisions = require('../database/scores/divisions'); router.get('/sports', function(req, res, next) { @@ -20,4 +21,11 @@ router.get('/genders', function(req, res, next) { .then(data => res.json(data)); }) +router.get('/divisions', function(req, res, next) { + const gender = req.body.gender == 'female' ? genders.FEMALE : genders.MALE; + + divisions.retrieveBySportAndGender(req.query.sport, gender) + .then(data => res.json(data)); +}) + module.exports = router; \ No newline at end of file diff --git a/views/submit.pug b/views/submit.pug index 93fdec1..97d7b37 100644 --- a/views/submit.pug +++ b/views/submit.pug @@ -16,9 +16,6 @@ block content select#sport-dropdown(name="sport" class="main-dropdown") select#gender-dropdown(name="gender") select#division-dropdown(name="division") - option(value="Varsity") Varsity - option(value="JV-A") JV-A - option(value="JV-B") JV-B span(class='form-section') label Date of match span(class='form-section-input')