From 2bec2b4be5fa5935a110888cbee4695a83f6e730 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Fri, 26 Nov 2021 21:28:20 -0700 Subject: [PATCH 1/6] Add functions to get latest game --- database/scores/games.js | 13 ++++++++++++- public/scripts/data.js | 8 ++++++++ routes/data.js | 6 +++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/database/scores/games.js b/database/scores/games.js index 8792b2e..e01352c 100644 --- a/database/scores/games.js +++ b/database/scores/games.js @@ -108,6 +108,16 @@ async function getFromID(gameID) { return new Game(row[0], row[3].toISOString().slice(0,10), row[4], row[5], row[6], row[7], row[1], row[2], row[8]); } +async function getLatest(userID = undefined) { + if(userID) { + const games = await retrieveByUser(userID); + return games[0]; + } else { + const games = await retrieve(); + return games[0]; + } +} + @@ -117,4 +127,5 @@ exports.remove = remove; exports.retrieve = retrieve; exports.retrieveByUser = retrieveByUser; exports.edit = edit; -exports.getFromID = getFromID; \ No newline at end of file +exports.getFromID = getFromID; +exports.getLatest = getLatest; \ No newline at end of file diff --git a/public/scripts/data.js b/public/scripts/data.js index e8c5898..28ec018 100644 --- a/public/scripts/data.js +++ b/public/scripts/data.js @@ -78,6 +78,14 @@ export async function getGame(gameID) { return game; } +export async function getLatestGame(userID = undefined) { + let URL = `/data/game?`; + if(userID) URL += `user=${userID}`; + const response = await fetch(URL); + const game = await response.json(); + return game; +} + export async function getAccounts() { const response = await fetch(`/data/accounts`); const accounts = await response.json(); diff --git a/routes/data.js b/routes/data.js index dfe6b8d..ae4fac3 100644 --- a/routes/data.js +++ b/routes/data.js @@ -122,7 +122,11 @@ router.get('/games', async function(req, res, next) { router.get('/game', async function(req, res, next) { try { const gameID = req.query.game; - const data = await games.getFromID(gameID); + const userID = req.query.user; + + let data; + if(gameID) data = await games.getFromID(gameID); + else data = await games.getLatest(userID); res.json(data); } catch(err) { console.error("ERROR: " + err.message); 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 2/6] 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 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 3/6] 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); From 0787a124046504dcfe83f99e070c73a196967f5c Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Fri, 26 Nov 2021 22:00:43 -0700 Subject: [PATCH 4/6] Rename management page --- routes/manage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/manage.js b/routes/manage.js index 8432ff8..113f31a 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -14,7 +14,7 @@ var checkLoginStatus = require('./checkLoginStatus'); router.get('/' ,checkLoginStatus.user, function(req, res, next) { - if(req.user[2]) res.render('manage', { title: 'Score Management', userLoggedIn: !!req.user }); + if(req.user[2]) res.render('manage', { title: 'Management Panel', userLoggedIn: !!req.user }); else res.render('manage/manage-nonadmin', { title: "My Games", userLoggedIn: !!req.user }); }); From a309a8216ceeaecf6054bae16da23fb028eb959f Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Fri, 26 Nov 2021 22:04:51 -0700 Subject: [PATCH 5/6] Prevent text wrapping for date in index page --- public/scripts/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/public/scripts/index.js b/public/scripts/index.js index 180f8b3..ef28b6f 100644 --- a/public/scripts/index.js +++ b/public/scripts/index.js @@ -92,6 +92,7 @@ async function loadTable() { const dateCell = document.createElement('td'); dateCell.textContent = game.date; + dateCell.style['white-space'] = 'nowrap'; row.appendChild(dateCell); gamesTable.appendChild(row); From 0fc1e03a3689e54bf4b193959dee23d4ea557ef4 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Fri, 26 Nov 2021 22:09:33 -0700 Subject: [PATCH 6/6] Update program version --- package-lock.json | 104 ++++++++++++++++++++++------------------------ package.json | 2 +- 2 files changed, 51 insertions(+), 55 deletions(-) diff --git a/package-lock.json b/package-lock.json index ff72bac..946bdbd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "score-tracker", - "version": "1.0.0-pre", + "version": "1.0.1-pre", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "score-tracker", - "version": "1.0.0-pre", + "version": "1.0.1-pre", "dependencies": { "async": "^3.2.2", "bcrypt": "^5.0.1", @@ -629,6 +629,25 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/glob": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", + "integrity": "sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==", + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + }, + "engines": { + "node": "*" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, "node_modules/has": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", @@ -927,6 +946,17 @@ "node": ">= 8" } }, + "node_modules/mkdirp": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", + "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==", + "bin": { + "mkdirp": "bin/cmd.js" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/morgan": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz", @@ -1431,25 +1461,6 @@ "url": "https://github.com/sponsors/isaacs" } }, - "node_modules/rimraf/node_modules/glob": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", - "integrity": "sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==", - "dependencies": { - "fs.realpath": "^1.0.0", - "inflight": "^1.0.4", - "inherits": "2", - "minimatch": "^3.0.4", - "once": "^1.3.0", - "path-is-absolute": "^1.0.0" - }, - "engines": { - "node": "*" - }, - "funding": { - "url": "https://github.com/sponsors/isaacs" - } - }, "node_modules/safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", @@ -1609,17 +1620,6 @@ "node": ">= 10" } }, - "node_modules/tar/node_modules/mkdirp": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", - "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==", - "bin": { - "mkdirp": "bin/cmd.js" - }, - "engines": { - "node": ">=10" - } - }, "node_modules/to-fast-properties": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-2.0.0.tgz", @@ -2218,6 +2218,19 @@ "has-symbols": "^1.0.1" } }, + "glob": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", + "integrity": "sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==", + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, "has": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", @@ -2437,6 +2450,11 @@ "yallist": "^4.0.0" } }, + "mkdirp": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", + "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==" + }, "morgan": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz", @@ -2832,21 +2850,6 @@ "integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==", "requires": { "glob": "^7.1.3" - }, - "dependencies": { - "glob": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", - "integrity": "sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==", - "requires": { - "fs.realpath": "^1.0.0", - "inflight": "^1.0.4", - "inherits": "2", - "minimatch": "^3.0.4", - "once": "^1.3.0", - "path-is-absolute": "^1.0.0" - } - } } }, "safe-buffer": { @@ -2970,13 +2973,6 @@ "minizlib": "^2.1.1", "mkdirp": "^1.0.3", "yallist": "^4.0.0" - }, - "dependencies": { - "mkdirp": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", - "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==" - } } }, "to-fast-properties": { diff --git a/package.json b/package.json index 91a43c2..08dee1d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "score-tracker", - "version": "1.0.0-pre", + "version": "1.0.1-pre", "private": true, "scripts": { "start": "node ./bin/www"