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() {
|
2022-03-07 19:21:17 +00:00
|
|
|
const data = await (await fetch(`/fetch/index/dropdown`)).json();
|
|
|
|
|
|
|
|
await Form.populateSeasons(seasonDropdown, null, data);
|
|
|
|
await Form.populateSports(sportDropdown, null, data);
|
2022-03-08 04:31:04 +00:00
|
|
|
await Form.populateGenders(genderDropdown, null, null, data);
|
2022-03-07 19:21:17 +00:00
|
|
|
await Form.populateDivisions(divisionDropdown, null, null, null, data);
|
|
|
|
await Form.populateTeams(teamDropdown, null, null, data);
|
2021-11-27 05:21:52 +00:00
|
|
|
|
|
|
|
seasonDropdown.onchange = loadTable;
|
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-27 05:21:52 +00:00
|
|
|
loadTable();
|
2021-11-27 04:44:32 +00:00
|
|
|
};
|
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-27 05:21:52 +00:00
|
|
|
loadTable();
|
2021-11-27 04:44:32 +00:00
|
|
|
};
|
2021-11-22 02:07:44 +00:00
|
|
|
|
2021-11-27 05:21:52 +00:00
|
|
|
divisionDropdown.onchange = loadTable;
|
|
|
|
teamDropdown.onchange = loadTable;
|
|
|
|
|
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
|
|
|
|
2022-03-08 06:07:26 +00:00
|
|
|
const requestURL = `/fetch/index/scores?team=${+selectedTeamID}&division=${+selectedDivisionID}&season=${+selectedSeasonID}`;
|
|
|
|
const gamesList = await (await fetch(requestURL)).json();
|
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');
|
2022-03-08 06:07:26 +00:00
|
|
|
opponentCell.textContent = game.opponent.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
|
|
|
}
|