Improve loading for manage games page

main
sudoer777 2022-03-09 10:52:02 -07:00
parent 05473e07ad
commit 55a6fc7d7b
2 changed files with 39 additions and 15 deletions

View File

@ -209,7 +209,7 @@ CATEGORIES.push(new Category(
CATEGORIES.push(new Category(
"games",
async function getGames() {
return await Data.getGames();
return await (await fetch('/fetch/manage/games')).json();
},
async function listGameHeaders() {
const headerRow = document.createElement('tr');
@ -242,12 +242,12 @@ CATEGORIES.push(new Category(
function listGame(game, row) {
const teamsCell = document.createElement('td');
const team1NameSpan = document.createElement('span');
Data.getTeam(game.team1ID)
.then(data => team1NameSpan.textContent = data.name);
let team1Name = game.team1.name;
team1NameSpan.textContent = team1Name;
teamsCell.appendChild(team1NameSpan);
const team2NameSpan = document.createElement('span');
Data.getTeam(game.team2ID)
.then(data => team2NameSpan.textContent = data.name);
let team2Name = game.team2.name;
team2NameSpan.textContent = team2Name;
teamsCell.appendChild(team2NameSpan);
row.appendChild(teamsCell);
@ -271,13 +271,11 @@ CATEGORIES.push(new Category(
const divisionGenderSpan = document.createElement('span');
divisionSpan.appendChild(divisionNameSpan);
divisionSpan.appendChild(divisionGenderSpan);
Data.getDivision(game.divisionID)
.then(data => {
Data.getSportName(data.sportID)
.then(data => sportSpan.textContent = data);
divisionNameSpan.textContent = data.name;
divisionGenderSpan.textContent = getGenderLetter(data.gender.name);
});
let divisionName = game.division.name;
let sportName = game.sport.name;
divisionNameSpan.textContent = divisionName;
sportSpan.textContent = sportName;
divisionGenderSpan.textContent = getGenderLetter(game.division.gender.name);
sportCell.appendChild(sportSpan);
sportCell.appendChild(divisionSpan);
row.appendChild(sportCell);
@ -293,11 +291,10 @@ CATEGORIES.push(new Category(
const submitterCell = document.createElement('td');
if(game.submitterID) {
Data.getAccount(game.submitterID)
.then(data => submitterCell.textContent = data.name);
let submitterName = game.submitter.name;
submitterCell.textContent = submitterName;
} else {
submitterCell.textContent = game.submitterName;
console.log(game.submitterName);
}
row.appendChild(submitterCell);
},

View File

@ -146,4 +146,31 @@ router.get('/manage/teams', async function (req, res, next) {
res.json(data);
});
router.get('/manage/games', checkLoginStatus.user, async function (req, res, next) {
try{
const userIsAdmin = req.user[2];
const loggedInAccountID = req.user[0];
if(!userIsAdmin) {
res.status(403).send("ACCESS DENIED");
} else {
const data = await games.retrieve();
for(const game of data) {
game.team1 = await teams.getFromID(game.team1ID);
game.team2 = await teams.getFromID(game.team2ID);
game.division = await divisions.getFromID(game.divisionID);
game.sport = await sports.getFromID(game.division.sportID);
game.submitter = game.submitterName || (await accounts.getFromID(game.submitterID));
}
res.json(data);
}
} catch(err) {
console.error("ERROR: " + err.message);
res.status(500).send("An error has occurred");
}
});
module.exports = router;