From 4aafccc5505fdd4d475854cf29a1cd8caf33b0e8 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Mon, 22 Nov 2021 14:50:32 -0700 Subject: [PATCH] Add functions to list seasons and sports in manage page --- public/scripts/manage.js | 116 +++++++++++++++++++++++++++++++++++++++ views/manage.pug | 13 ++--- 2 files changed, 120 insertions(+), 9 deletions(-) create mode 100644 public/scripts/manage.js diff --git a/public/scripts/manage.js b/public/scripts/manage.js new file mode 100644 index 0000000..19b700a --- /dev/null +++ b/public/scripts/manage.js @@ -0,0 +1,116 @@ +import * as Data from "./data.js"; + +const categoryDropdown = document.getElementById('category-dropdown'); +const itemsListTable = document.getElementById('items-list'); + +class Category { + constructor(name, getItems, listHeaders, listItem, addItem, submitItem, editItem) { + this.name = name; + this.getItems = getItems; + this.listHeaders = listHeaders; + this.listItem = listItem; + this.addItem = addItem; + this.submitItem = submitItem; + 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); + + itemsListTable.appendChild(headerRow); + }, + function listSeason(season, row) { + const yearCell = document.createElement('td'); + yearCell.textContent = (season.year - 1) + "-" + season.year; + row.appendChild(yearCell); + }, + async function addSeason() { + // + }, + async function submitSeason() { + // + }, + async function editSeason() { + + } +)); + +CATEGORIES.push(new Category( + "sports", + async function getSports() { + return await Data.getSports(); + }, + async function listSportHeaders() { + const headerRow = document.createElement('tr'); + + const yearsHeader = document.createElement('th'); + yearsHeader.textContent = "Name"; + headerRow.appendChild(yearsHeader); + + itemsListTable.appendChild(headerRow); + }, + function listSport(sport, row) { + const nameCell = document.createElement('td'); + nameCell.textContent = sport.name; + row.appendChild(nameCell); + }, + async function addSeason() { + // + }, + async function submitSeason() { + // + }, + async function editSeason() { + + } +)); + + + +async function listItems(category) { + itemsListTable.innerHTML = ""; + + const itemsList = await category.getItems(); + + await category.listHeaders(); + + itemsList.forEach(item => { + const row = document.createElement('tr'); + + category.listItem(item, row); + + const editCell = document.createElement('td'); + const editButton = document.createElement('button'); + editButton.textContent = "edit"; + editCell.appendChild(editButton); + row.appendChild(editCell); + + const removeCell = document.createElement('td'); + const removeButton = document.createElement('button'); + removeButton.textContent = "remove"; + removeCell.appendChild(removeButton); + row.appendChild(removeCell); + + itemsListTable.appendChild(row); + }); +} +listItems(CATEGORIES[0]); + + + +categoryDropdown.onchange = () => { + listItems(CATEGORIES[categoryDropdown.selectedIndex]); +}; \ No newline at end of file diff --git a/views/manage.pug b/views/manage.pug index 79ac0b0..7977824 100644 --- a/views/manage.pug +++ b/views/manage.pug @@ -1,7 +1,7 @@ extends layout block stylesheets - link(rel='stylesheet', href='/stylesheets/index.css') + link(rel='stylesheet', href='/stylesheets/manage.css') link(rel='stylesheet', href='/stylesheets/form.css') block content @@ -19,13 +19,8 @@ block content option(value="games") Games div h2#table-header - table - colgroup - col#score-column(span="1") - col#opponent-column(span="1") - col#date-column(span="1") - tbody#games-table - + table#items-list + button Add new... block scripts - script(src='/scripts/index.js' type="module") \ No newline at end of file + script(src='/scripts/manage.js' type="module") \ No newline at end of file