Automatically fill last submitted game details when submitting new game

main
sudoer777 2021-11-26 21:57:02 -07:00
parent 6243427976
commit 8ab01bb6a4
3 changed files with 24 additions and 10 deletions

View File

@ -78,9 +78,9 @@ export async function getGame(gameID) {
return game; return game;
} }
export async function getLatestGame(userID = undefined) { export async function getLatestGame(ofUser = false) {
let URL = `/data/game?`; let URL = `/data/game?`;
if(userID) URL += `user=${userID}`; if(ofUser) URL += `ofuser=1`;
const response = await fetch(URL); const response = await fetch(URL);
const game = await response.json(); const game = await response.json();
return game; return game;

View File

@ -40,12 +40,24 @@ async function initializeForm() {
team2ScoreTextbox.value = game.team2Score; team2ScoreTextbox.value = game.team2Score;
} }
else { else {
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.populateSeasons(seasonDropdown);
await Form.populateSports(sportDropdown) await Form.populateSports(sportDropdown)
await Form.populateGenders(genderDropdown, sportDropdown.value) await Form.populateGenders(genderDropdown, sportDropdown.value)
await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value); await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value);
await Form.populateTeams(team1Dropdown, sportDropdown.value); await Form.populateTeams(team1Dropdown, sportDropdown.value);
await Form.populateTeams(team2Dropdown, sportDropdown.value); await Form.populateTeams(team2Dropdown, sportDropdown.value);
}
dateInput.value = (new Date()).toISOString().slice(0,10); dateInput.value = (new Date()).toISOString().slice(0,10);
} }

View File

@ -122,11 +122,13 @@ router.get('/games', async function(req, res, next) {
router.get('/game', async function(req, res, next) { router.get('/game', async function(req, res, next) {
try { try {
const gameID = req.query.game; 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; let data;
if(gameID) data = await games.getFromID(gameID); 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); res.json(data);
} catch(err) { } catch(err) {
console.error("ERROR: " + err.message); console.error("ERROR: " + err.message);