From 30ddb13ec6815007a80c4db7de6a3b2b64108fa1 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Sun, 21 Nov 2021 15:30:54 -0700 Subject: [PATCH] Add function to list sports in submit form dropdown --- public/scripts/data.js | 5 +++++ public/scripts/submit.js | 20 ++++++++++++++++++++ routes/data.js | 7 ++++--- views/submit.pug | 3 +-- 4 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 public/scripts/data.js diff --git a/public/scripts/data.js b/public/scripts/data.js new file mode 100644 index 0000000..01b681b --- /dev/null +++ b/public/scripts/data.js @@ -0,0 +1,5 @@ +export async function getSports() { + const response = await fetch('/data/sports'); + const sportsList = await response.json(); + return sportsList; +} \ No newline at end of file diff --git a/public/scripts/submit.js b/public/scripts/submit.js index e69de29..dc3b2b7 100644 --- a/public/scripts/submit.js +++ b/public/scripts/submit.js @@ -0,0 +1,20 @@ +import * as Data from "./data.js"; + + +const sportDropdown = document.getElementById('sport-dropdown'); + + + + + +async function listSports() { + let sportsList = await Data.getSports(); + + sportsList.forEach(sport => { + const option = document.createElement('option'); + option.text = sport.name; + option.value = sport.id; + sportDropdown.appendChild(option); + }); +} +listSports(); \ No newline at end of file diff --git a/routes/data.js b/routes/data.js index 05f5291..072a63e 100644 --- a/routes/data.js +++ b/routes/data.js @@ -1,10 +1,11 @@ var express = require('express'); var router = express.Router(); -var database = require('../database/database'); +var sports = require('../database/scores/sports'); /* GET submit page. */ -router.get('/', function(req, res, next) { - res.json( { message : "test" }); +router.get('/sports', function(req, res, next) { + sports.retrieveAll() + .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 149fde7..25ebe71 100644 --- a/views/submit.pug +++ b/views/submit.pug @@ -15,7 +15,6 @@ block content label Sport span(class='form-section-input') select#sport-dropdown(name="sport" class="main-dropdown") - option(value="Basketball" selected) Basketball select#gender-dropdown(name="gender") option(value="Male" selected) Male option(value="Female") Female @@ -41,4 +40,4 @@ block content button(type="submit") Submit block scripts - script(src='/scripts/submit.js') \ No newline at end of file + script(src='/scripts/submit.js' type="module") \ No newline at end of file