From 8ab01bb6a4967bfbb48b2d0e1f25706672b224dc Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Fri, 26 Nov 2021 21:57:02 -0700 Subject: [PATCH] Automatically fill last submitted game details when submitting new game --- public/scripts/data.js | 4 ++-- public/scripts/manage/game.js | 24 ++++++++++++++++++------ routes/data.js | 6 ++++-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/public/scripts/data.js b/public/scripts/data.js index 28ec018..9a1c12d 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -78,9 +78,9 @@ export async function getGame(gameID) { return game; } -export async function getLatestGame(userID = undefined) { +export async function getLatestGame(ofUser = false) { let URL = `/data/game?`; - if(userID) URL += `user=${userID}`; + if(ofUser) URL += `ofuser=1`; const response = await fetch(URL); const game = await response.json(); return game; diff --git a/public/scripts/manage/game.js b/public/scripts/manage/game.js index ce80b97..9b0e86e 100644 --- a/public/scripts/manage/game.js +++ b/public/scripts/manage/game.js @@ -40,12 +40,24 @@ async function initializeForm() { team2ScoreTextbox.value = game.team2Score; } else { - 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); + try { + const game = await Data.getLatestGame(true); + + Form.populateSeasons(seasonDropdown, game.seasonID); + 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); + } catch { + 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); } diff --git a/routes/data.js b/routes/data.js index ae4fac3..b6769d5 100644 --- a/routes/data.js +++ b/routes/data.js @@ -122,11 +122,13 @@ router.get('/games', async function(req, res, next) { router.get('/game', async function(req, res, next) { try { const gameID = req.query.game; - const userID = req.query.user; + const ofUser = req.query.ofuser; + const currentUserID = req.user ? req.user[0] : null; let data; if(gameID) data = await games.getFromID(gameID); - else data = await games.getLatest(userID); + else if(ofUser) data = await games.getLatest(currentUserID); + else data = await games.getLatest(); res.json(data); } catch(err) { console.error("ERROR: " + err.message);