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

191 lines
5.7 KiB
JavaScript
Raw Normal View History

2021-11-22 02:07:44 +00:00
import * as Data from "./data.js";
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');
2021-11-22 02:07:44 +00:00
async function listSeasons() {
seasonDropdown.innerHTML = "";
const seasonsList = await Data.getSeasons();
seasonsList.forEach(season => {
const option = document.createElement('option');
2021-11-22 21:07:06 +00:00
option.text = (season.year - 1) + "-" + season.year;
2021-11-22 02:07:44 +00:00
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);
});
}
listDivisions();
}
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);
});
}
2021-11-22 04:59:07 +00:00
listTeams();
2021-11-22 02:07:44 +00:00
}
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);
});
}
2021-11-22 04:59:07 +00:00
listGames();
}
async function listGames() {
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;
row.appendChild(dateCell);
gamesTable.appendChild(row);
});
}
else {
noScoresMessage.textContent = "No scores available";
}
2021-11-22 04:59:07 +00:00
}
}
2021-11-22 05:07:25 +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
}
sportDropdown.onchange = (() => {
listGenders();
listTeams();
});
2021-11-22 04:59:07 +00:00
genderDropdown.onchange = listDivisions;
2021-11-22 05:07:25 +00:00
teamDropdown.onchange = listGames;
seasonDropdown.onchange = listGames;
if(addScoreButton) {
addScoreButton.addEventListener('click', () => {
window.location.href = '/manage/game';
});
}
if(manageButton) {
manageButton.addEventListener('click', () => {
window.location.href = '/manage'
});
}