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/manage.js

410 lines
14 KiB
JavaScript
Raw Normal View History

import * as Data from "./data.js";
const categoryDropdown = document.getElementById('category-dropdown');
const itemsListTable = document.getElementById('items-list');
const addNewButton = document.getElementById('add-new-button');
2022-03-09 17:20:53 +00:00
const loadingSpan = document.getElementById('loading-message');
2021-11-22 23:58:36 +00:00
function getGenderLetter(genderName) {
return genderName == "female" ? "F" : "M";
}
class Category {
constructor(name, getItems, listHeaders, listItem, addItem, editItem) {
this.name = name;
this.getItems = getItems;
this.listHeaders = listHeaders;
this.listItem = listItem;
this.addItem = addItem;
this.editItem = editItem;
}
}
const CATEGORIES = [];
CATEGORIES.push(new Category(
"seasons",
async function getSeasons() {
return await Data.getSeasons();
},
async function listSeasonHeaders() {
const headerRow = document.createElement('tr');
const yearsHeader = document.createElement('th');
yearsHeader.textContent = "Years";
headerRow.appendChild(yearsHeader);
2021-11-23 00:13:29 +00:00
const spacerHeader = document.createElement('th');
spacerHeader.classList.add('spacer-column');
headerRow.appendChild(spacerHeader);
itemsListTable.appendChild(headerRow);
},
function listSeason(season, row) {
const yearCell = document.createElement('td');
yearCell.textContent = (season.year - 1) + "-" + season.year;
row.appendChild(yearCell);
2021-11-23 00:13:29 +00:00
const spacerCell = document.createElement('td');
row.appendChild(spacerCell);
},
async function addSeason() {
2021-11-23 21:48:19 +00:00
window.location.href = "/manage/season";
},
2021-11-23 21:48:19 +00:00
async function editSeason(id) {
const verified = confirm(`This season will be removed.`);
if(verified) {
const form = document.createElement('form');
form.action = "/manage/season";
form.method = "POST";
form.style.visibility = "hidden";
itemsListTable.appendChild(form);
const remove = document.createElement('input');
remove.setAttribute('name', 'remove');
remove.setAttribute('value', 1);
form.appendChild(remove);
const seasonID = document.createElement('input');
seasonID.setAttribute('name', 'season');
seasonID.setAttribute('value', id);
form.appendChild(seasonID);
form.submit();
}
}
));
CATEGORIES.push(new Category(
"sports",
async function getSports() {
return await Data.getSports();
},
async function listSportHeaders() {
const headerRow = document.createElement('tr');
const nameHeader = document.createElement('th');
nameHeader.textContent = "Name";
headerRow.appendChild(nameHeader);
2021-11-23 00:13:29 +00:00
const spacerHeader = document.createElement('th');
spacerHeader.classList.add('spacer-column');
headerRow.appendChild(spacerHeader);
itemsListTable.appendChild(headerRow);
},
function listSport(sport, row) {
const nameCell = document.createElement('td');
nameCell.textContent = sport.name;
row.appendChild(nameCell);
2021-11-23 00:13:29 +00:00
const spacerCell = document.createElement('td');
row.appendChild(spacerCell);
},
async function addSport() {
window.location.href = `/manage/sport`;
},
async function editSport(id) {
window.location.href = `/manage/sport?sport=${id}`
}
));
CATEGORIES.push(new Category(
"divisions",
async function getDivisions() {
return await (await fetch('/fetch/manage/divisions')).json();
},
async function listDivisionHeaders() {
const headerRow = document.createElement('tr');
const nameHeader = document.createElement('th');
nameHeader.textContent = "Name";
headerRow.appendChild(nameHeader);
const genderHeader = document.createElement('th');
headerRow.appendChild(genderHeader);
2021-11-23 00:13:29 +00:00
const spacerHeader = document.createElement('th');
spacerHeader.classList.add('spacer-column');
headerRow.appendChild(spacerHeader);
const sportHeader = document.createElement('th');
sportHeader.textContent = "Sport";
headerRow.appendChild(sportHeader);
itemsListTable.appendChild(headerRow);
},
function listDivision(division, row) {
const nameCell = document.createElement('td');
nameCell.textContent = division.name;
row.appendChild(nameCell);
const genderCell = document.createElement('td');
2021-11-22 23:58:36 +00:00
const gender = getGenderLetter(division.gender.name);
genderCell.textContent = gender;
row.appendChild(genderCell);
2021-11-23 00:13:29 +00:00
const spacerCell = document.createElement('td');
row.appendChild(spacerCell);
const sportCell = document.createElement('td');
let sportName = division.sport.name;
sportCell.textContent = sportName;
row.appendChild(sportCell);
},
2021-11-23 02:11:16 +00:00
async function addDivision() {
2021-11-23 06:23:57 +00:00
window.location.href = "/manage/division";
},
2021-11-23 06:23:57 +00:00
async function editDivision(id) {
window.location.href = `/manage/division?division=${id}`
}
));
CATEGORIES.push(new Category(
"teams",
async function getTeams() {
2022-03-09 17:32:47 +00:00
return await (await fetch('/fetch/manage/teams')).json();
},
async function listTeamHeaders() {
const headerRow = document.createElement('tr');
const nameHeader = document.createElement('th');
nameHeader.textContent = "Name";
headerRow.appendChild(nameHeader);
2021-11-23 00:13:29 +00:00
const spacerHeader = document.createElement('th');
spacerHeader.classList.add('spacer-column');
headerRow.appendChild(spacerHeader);
const sportHeader = document.createElement('th');
sportHeader.textContent = "Sport";
headerRow.appendChild(sportHeader);
itemsListTable.appendChild(headerRow);
},
function listTeam(team, row) {
const nameCell = document.createElement('td');
nameCell.textContent = team.name;
row.appendChild(nameCell);
2021-11-23 00:13:29 +00:00
const spacerCell = document.createElement('td');
row.appendChild(spacerCell);
const sportCell = document.createElement('td');
2022-03-09 17:32:47 +00:00
let sportName = team.sport.name;
sportCell.textContent = sportName;
row.appendChild(sportCell);
},
2021-11-23 02:17:23 +00:00
async function addTeam() {
2021-11-23 06:45:24 +00:00
window.location.href = "/manage/team";
},
2021-11-23 06:45:24 +00:00
async function editTeam(id) {
window.location.href = `/manage/team?team=${id}`;
2021-11-22 23:27:50 +00:00
}
));
CATEGORIES.push(new Category(
"games",
async function getGames() {
2022-03-09 17:52:02 +00:00
return await (await fetch('/fetch/manage/games')).json();
2021-11-22 23:27:50 +00:00
},
async function listGameHeaders() {
const headerRow = document.createElement('tr');
const teamsHeader = document.createElement('th');
teamsHeader.textContent = "Teams";
headerRow.appendChild(teamsHeader);
const scoreHeader = document.createElement('th');
headerRow.appendChild(scoreHeader);
2021-11-23 00:13:29 +00:00
const spacerHeader = document.createElement('th');
spacerHeader.classList.add('spacer-column');
headerRow.appendChild(spacerHeader);
2021-11-22 23:27:50 +00:00
const sportNameHeader = document.createElement('th');
sportNameHeader.textContent = "Sport";
headerRow.appendChild(sportNameHeader);
const dateHeader = document.createElement('th');
dateHeader.textContent = "Date";
headerRow.appendChild(dateHeader);
const submitterHeader = document.createElement('th');
submitterHeader.textContent = "Submitter";
headerRow.appendChild(submitterHeader);
2021-11-22 23:27:50 +00:00
itemsListTable.appendChild(headerRow);
},
function listGame(game, row) {
const teamsCell = document.createElement('td');
const team1NameSpan = document.createElement('span');
2022-03-09 17:52:02 +00:00
let team1Name = game.team1.name;
team1NameSpan.textContent = team1Name;
2021-11-22 23:27:50 +00:00
teamsCell.appendChild(team1NameSpan);
const team2NameSpan = document.createElement('span');
2022-03-09 17:52:02 +00:00
let team2Name = game.team2.name;
team2NameSpan.textContent = team2Name;
2021-11-22 23:27:50 +00:00
teamsCell.appendChild(team2NameSpan);
row.appendChild(teamsCell);
const scoresCell = document.createElement('td');
const team1ScoreSpan = document.createElement('span');
team1ScoreSpan.textContent = game.team1Score;
scoresCell.appendChild(team1ScoreSpan);
const team2ScoreSpan = document.createElement('span');
team2ScoreSpan.textContent = game.team2Score;
scoresCell.appendChild(team2ScoreSpan);
row.appendChild(scoresCell);
2021-11-23 00:13:29 +00:00
const spacerCell = document.createElement('td');
row.appendChild(spacerCell);
2021-11-22 23:27:50 +00:00
const sportCell = document.createElement('td');
2021-11-22 23:58:36 +00:00
const sportSpan = document.createElement('span');
const divisionSpan = document.createElement('span');
2021-11-23 00:13:29 +00:00
divisionSpan.classList.add('flat-content');
2021-11-22 23:58:36 +00:00
const divisionNameSpan = document.createElement('span');
const divisionGenderSpan = document.createElement('span');
divisionSpan.appendChild(divisionNameSpan);
divisionSpan.appendChild(divisionGenderSpan);
2022-03-09 17:52:02 +00:00
let divisionName = game.division.name;
let sportName = game.sport.name;
divisionNameSpan.textContent = divisionName;
sportSpan.textContent = sportName;
divisionGenderSpan.textContent = getGenderLetter(game.division.gender.name);
2021-11-22 23:58:36 +00:00
sportCell.appendChild(sportSpan);
sportCell.appendChild(divisionSpan);
2021-11-22 23:27:50 +00:00
row.appendChild(sportCell);
2021-11-22 23:58:36 +00:00
const dateCell = document.createElement('td');
const yearSpan = document.createElement('span');
yearSpan.textContent = game.date.slice(0,4);
dateCell.appendChild(yearSpan);
const dateSpan = document.createElement('span');
dateSpan.textContent = game.date.slice(5);
dateCell.appendChild(dateSpan);
row.appendChild(dateCell);
const submitterCell = document.createElement('td');
if(game.submitterID) {
2022-03-09 17:52:02 +00:00
let submitterName = game.submitter.name;
submitterCell.textContent = submitterName;
} else {
submitterCell.textContent = game.submitterName;
}
row.appendChild(submitterCell);
2021-11-22 23:27:50 +00:00
},
async function addGame() {
2021-11-23 07:49:11 +00:00
window.location.href = "/manage/game";
},
2021-11-23 07:49:11 +00:00
async function editGame(id) {
window.location.href = `/manage/game?game=${id}`;
}
));
CATEGORIES.push(new Category(
"accounts",
async function getAccounts() {
return await Data.getAccounts();
},
async function listAccountHeaders() {
const headerRow = document.createElement('tr');
const nameHeader = document.createElement('th');
nameHeader.textContent = "Name";
headerRow.appendChild(nameHeader);
const emailHeader = document.createElement('th');
emailHeader.textContent = "Email";
headerRow.appendChild(emailHeader);
const spacerHeader = document.createElement('th');
spacerHeader.classList.add('spacer-column');
headerRow.appendChild(spacerHeader);
const adminHeader = document.createElement('th');
adminHeader.textContent = "Admin?";
headerRow.appendChild(adminHeader);
itemsListTable.appendChild(headerRow);
},
function listAccount(account, row) {
const nameCell = document.createElement('td');
nameCell.textContent = account.name;
row.appendChild(nameCell);
const emailCell = document.createElement('td');
emailCell.textContent = account.email;
row.appendChild(emailCell);
const spacerCell = document.createElement('td');
row.appendChild(spacerCell);
const adminCell = document.createElement('td');
adminCell.textContent = account.isAdmin;
row.appendChild(adminCell);
},
async function addAccount() {
window.location.href = "/manage/account";
},
async function editAccount(id) {
window.location.href = `/manage/account?account=${id}`;
}
));
async function listItems(category) {
2022-03-09 17:20:53 +00:00
loadingSpan.textContent = "Loading...";
itemsListTable.innerHTML = "";
const itemsList = await category.getItems();
await category.listHeaders();
itemsList.forEach(item => {
const row = document.createElement('tr');
category.listItem(item, row);
2021-11-22 23:58:36 +00:00
const manageCell = document.createElement('td');
const editSpan = document.createElement('span');
const editButton = document.createElement('button');
2021-11-27 06:47:25 +00:00
editButton.textContent = "✎";
editButton.style['font-size'] = '1.25em';
editButton.addEventListener('click', () => {
CATEGORIES[categoryDropdown.selectedIndex].editItem(item.id);
});
2021-11-22 23:58:36 +00:00
editSpan.appendChild(editButton);
manageCell.appendChild(editSpan);
2021-11-22 23:58:36 +00:00
row.appendChild(manageCell);
itemsListTable.appendChild(row);
});
2022-03-09 17:20:53 +00:00
loadingSpan.textContent = '';
}
if(window.location.hash) {
let correctIndex;
let index = 0;
CATEGORIES.forEach(category => {
if(window.location.hash == '#' + category.name) correctIndex = index;
index++;
})
if(correctIndex || correctIndex === 0) categoryDropdown.selectedIndex = correctIndex;
}
2021-11-22 23:58:36 +00:00
listItems(CATEGORIES[categoryDropdown.selectedIndex]);
categoryDropdown.onchange = () => {
listItems(CATEGORIES[categoryDropdown.selectedIndex]);
};
addNewButton.addEventListener('click', () => CATEGORIES[categoryDropdown.selectedIndex].addItem());