This repository has been archived on 2024-04-05. You can view files and clone it, but cannot push or open issues/pull-requests.
score-tracker/public/scripts/index.js

135 lines
4.8 KiB
JavaScript
Raw Normal View History

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
2022-03-08 17:38:28 +00:00
const dropdownsDiv = document.getElementById('dropdowns-div');
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');
const addScoreButton = document.getElementById('add-score-button');
const manageButton = document.getElementById('manage-button');
2022-03-08 17:38:28 +00:00
const loadingSpan = document.getElementById('loading');
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);
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);
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);
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);
loadTable();
2021-11-27 04:44:32 +00:00
};
2021-11-22 02:07:44 +00:00
divisionDropdown.onchange = loadTable;
teamDropdown.onchange = loadTable;
2022-03-08 17:38:28 +00:00
loadingSpan.textContent = '';
dropdownsDiv.style.visibility = 'visible';
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}`;
2022-03-08 06:19:07 +00:00
noScoresMessage.textContent = "Loading scores...";
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();
2022-03-08 06:19:07 +00:00
noScoresMessage.textContent = "";
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;
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
}
if(addScoreButton) {
addScoreButton.addEventListener('click', () => {
window.location.href = '/manage/game';
});
}
if(manageButton) {
manageButton.addEventListener('click', () => {
window.location.href = '/manage'
});
2021-11-27 04:44:32 +00:00
}