2021-11-22 02:07:44 +00:00
|
|
|
import * as Data from "./data.js";
|
2021-11-27 04:44:32 +00:00
|
|
|
import * as Form from "./form.js";
|
2021-11-22 02:07:44 +00:00
|
|
|
|
|
|
|
const sportDropdown = document.getElementById('sport-dropdown');
|
|
|
|
const seasonDropdown = document.getElementById('year-dropdown');
|
|
|
|
const genderDropdown = document.getElementById('gender-dropdown');
|
|
|
|
const divisionDropdown = document.getElementById('division-dropdown');
|
|
|
|
const teamDropdown = document.getElementById('team-dropdown');
|
2021-11-22 04:59:07 +00:00
|
|
|
const gamesTable = document.getElementById('games-table');
|
|
|
|
const gamesTableHeader = document.getElementById('games-table-header');
|
2021-11-22 05:07:25 +00:00
|
|
|
const noScoresMessage = document.getElementById('no-scores-message');
|
2021-11-23 21:48:30 +00:00
|
|
|
const addScoreButton = document.getElementById('add-score-button');
|
|
|
|
const manageButton = document.getElementById('manage-button');
|
2021-11-22 02:07:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-11-27 04:44:32 +00:00
|
|
|
async function initializeForm() {
|
|
|
|
let latestGame;
|
2021-11-22 02:07:44 +00:00
|
|
|
|
2021-11-27 04:44:32 +00:00
|
|
|
try {
|
|
|
|
latestGame = await Data.getLatestGame();
|
|
|
|
} catch {
|
|
|
|
latestGame = null;
|
2021-11-22 02:07:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 04:44:32 +00:00
|
|
|
if(latestGame) {
|
|
|
|
Form.populateSeasons(seasonDropdown, latestGame.seasonID);
|
|
|
|
|
|
|
|
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);
|
2021-11-22 02:07:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 04:44:32 +00:00
|
|
|
sportDropdown.onchange = async () => {
|
|
|
|
await Form.populateGenders(genderDropdown, sportDropdown.value)
|
|
|
|
await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value);
|
|
|
|
await Form.populateTeams(teamDropdown, sportDropdown.value);
|
|
|
|
};
|
2021-11-22 02:07:44 +00:00
|
|
|
|
2021-11-27 04:44:32 +00:00
|
|
|
genderDropdown.onchange = async () => {
|
|
|
|
await Form.populateDivisions(divisionDropdown, sportDropdown.value, genderDropdown.value);
|
|
|
|
};
|
2021-11-22 02:07:44 +00:00
|
|
|
|
2021-11-27 04:44:32 +00:00
|
|
|
loadTable();
|
2021-11-22 02:07:44 +00:00
|
|
|
|
2021-11-22 04:59:07 +00:00
|
|
|
|
|
|
|
}
|
2021-11-27 04:44:32 +00:00
|
|
|
initializeForm();
|
2021-11-22 04:59:07 +00:00
|
|
|
|
2021-11-27 04:44:32 +00:00
|
|
|
async function loadTable() {
|
2021-11-22 04:59:07 +00:00
|
|
|
gamesTable.innerHTML = "";
|
|
|
|
gamesTableHeader.textContent = "";
|
2021-11-22 05:07:25 +00:00
|
|
|
noScoresMessage.textContent = "";
|
2021-11-22 04:59:07 +00:00
|
|
|
|
|
|
|
const selectedTeamID = teamDropdown.value;
|
|
|
|
const selectedDivisionID = divisionDropdown.value;
|
|
|
|
const selectedSeasonID = seasonDropdown.value;
|
|
|
|
|
|
|
|
if(selectedTeamID && selectedDivisionID) {
|
2021-11-22 05:07:25 +00:00
|
|
|
gamesTableHeader.textContent = `Scores for ${teamDropdown.options[teamDropdown.selectedIndex].text}`;
|
2021-11-22 04:59:07 +00:00
|
|
|
|
|
|
|
const gamesList = await Data.getGames(selectedTeamID, selectedDivisionID, selectedSeasonID);
|
2021-11-22 05:07:25 +00:00
|
|
|
if(gamesList.length > 0) {
|
|
|
|
await setupGamesTableHeaders();
|
|
|
|
|
|
|
|
gamesList.forEach((game) => {
|
|
|
|
const row = document.createElement('tr');
|
|
|
|
|
|
|
|
const scoreCell = document.createElement('td');
|
|
|
|
const winLossLine = document.createElement('span');
|
|
|
|
winLossLine.textContent = (game.team1Score > game.team2Score) ? "Win" : (game.team1Score < game.team2Score) ? "Loss" : "Tie";
|
|
|
|
scoreCell.appendChild(winLossLine);
|
|
|
|
const scoreLine = document.createElement('span');
|
|
|
|
scoreLine.textContent = game.team1Score + "-" + game.team2Score;
|
|
|
|
scoreCell.appendChild(scoreLine);
|
|
|
|
row.appendChild(scoreCell);
|
|
|
|
|
|
|
|
const opponentCell = document.createElement('td');
|
2021-11-23 06:45:24 +00:00
|
|
|
Data.getTeam(game.team2ID)
|
|
|
|
.then(data => opponentCell.textContent = data.name);
|
2021-11-22 05:07:25 +00:00
|
|
|
row.appendChild(opponentCell);
|
|
|
|
|
|
|
|
const dateCell = document.createElement('td');
|
|
|
|
dateCell.textContent = game.date;
|
2021-11-27 05:04:51 +00:00
|
|
|
dateCell.style['white-space'] = 'nowrap';
|
2021-11-22 05:07:25 +00:00
|
|
|
row.appendChild(dateCell);
|
|
|
|
|
|
|
|
gamesTable.appendChild(row);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
noScoresMessage.textContent = "No scores available";
|
|
|
|
}
|
2021-11-22 04:59:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 04:44:32 +00:00
|
|
|
async function setupGamesTableHeaders() {
|
2021-11-22 04:59:07 +00:00
|
|
|
const row = document.createElement('tr');
|
|
|
|
|
|
|
|
const scoresHeader = document.createElement('th');
|
|
|
|
scoresHeader.textContent = "Score"
|
|
|
|
row.appendChild(scoresHeader);
|
|
|
|
|
|
|
|
const opponentHeader = document.createElement('th');
|
|
|
|
opponentHeader.textContent = "Opponent";
|
|
|
|
row.appendChild(opponentHeader);
|
|
|
|
|
|
|
|
const dateHeader = document.createElement('th');
|
|
|
|
dateHeader.textContent = "Date";
|
|
|
|
row.appendChild(dateHeader);
|
|
|
|
|
|
|
|
gamesTable.appendChild(row);
|
2021-11-22 02:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-11-26 19:53:54 +00:00
|
|
|
if(addScoreButton) {
|
|
|
|
addScoreButton.addEventListener('click', () => {
|
|
|
|
window.location.href = '/manage/game';
|
|
|
|
});
|
|
|
|
}
|
2021-11-23 21:48:30 +00:00
|
|
|
|
2021-11-26 19:53:54 +00:00
|
|
|
if(manageButton) {
|
|
|
|
manageButton.addEventListener('click', () => {
|
|
|
|
window.location.href = '/manage'
|
|
|
|
});
|
2021-11-27 04:44:32 +00:00
|
|
|
}
|