diff --git a/public/scripts/manage/sport.js b/public/scripts/manage/sport.js index c30fbce..20ad2cb 100644 --- a/public/scripts/manage/sport.js +++ b/public/scripts/manage/sport.js @@ -1,7 +1,6 @@ import * as Data from "../data.js"; -const mainHeader = document.getElementById('main-header'); const nameTextbox = document.getElementById('name-textbox'); const submitButton = document.getElementById('submit-button'); const deleteButton = document.getElementById('delete-button'); @@ -12,8 +11,6 @@ async function initializeForm() { let params = new URLSearchParams(location.search); let sportID = params.get('sport') if(sportID) { - mainHeader.textContent = "Edit Sport"; - const sportName = await Data.getSportName(sportID); nameTextbox.value = sportName; diff --git a/routes/manage.js b/routes/manage.js index 9b5fe6e..e16f9ff 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -25,6 +25,9 @@ router.get('/game', checkLoginStatus.user, function(req, res, next) { }); router.post('/game', checkLoginStatus.user, async function(req, res, next) { + const id = req.body['game']; + const remove = req.body['remove']; + try { const seasonID = req.body['year']; const sportID = req.body['sport']; @@ -37,9 +40,6 @@ router.post('/game', checkLoginStatus.user, async function(req, res, next) { const team2Score = req.body['team2-score']; const userID = req.user[0]; - const id = req.body['game']; - const remove = req.body['remove']; - const loggedInUserID = req.user[0]; const loggedInUserIsAdmin = req.user[2]; @@ -63,7 +63,7 @@ router.post('/game', checkLoginStatus.user, async function(req, res, next) { } catch(err) { console.error("ERROR: " + err.message); req.flash("error", "An error has occurred."); - res.redirect('/manage/game'); + res.redirect('/manage/game' + (id ? `?game=${id}` : '')); } }); @@ -72,12 +72,12 @@ router.get('/season', checkLoginStatus.admin, function(req, res, next) { }); router.post('/season', checkLoginStatus.admin, async function(req, res, next) { + const seasonID = req.body['season']; + const remove = req.body['remove']; + try { const year = req.body['year']; - const seasonID = req.body['season']; - const remove = req.body['remove']; - if(remove) await seasons.remove(seasonID); else await seasons.add(year); @@ -85,19 +85,22 @@ router.post('/season', checkLoginStatus.admin, async function(req, res, next) { } catch(err) { console.error("ERROR: " + err.message); req.flash("error", "An error has occurred."); - res.redirect('/manage/season'); + res.redirect('/manage/season' + (seasonID ? `?season=${seasonID}` : '')); } }); router.get('/sport', checkLoginStatus.admin, function(req, res, next) { - res.render('manage/addsport', { title: 'Add Sport', userLoggedIn: !!req.user, message: req.flash('error') }); + let title = req.query.sport ? 'Edit Sport' : 'Add Sport'; + + res.render('manage/addsport', { title, userLoggedIn: !!req.user, message: req.flash('error') }); }); router.post('/sport', checkLoginStatus.admin, async function(req, res, next) { + const id = req.body['sport']; + const remove = req.body['remove']; + try { const name = req.body['name']; - const id = req.body['sport']; - const remove = req.body['remove']; if(remove) await sports.remove(id); else if(id) await sports.rename(id, name); @@ -107,7 +110,7 @@ router.post('/sport', checkLoginStatus.admin, async function(req, res, next) { } catch(err) { console.error("ERROR: " + err.message); req.flash("error", "An error has occurred."); - res.redirect('/manage/sport'); + res.redirect('/manage/sport' + (id ? `?sport=${id}` : '')); } }); @@ -118,14 +121,14 @@ router.get('/division', checkLoginStatus.admin, function(req, res, next) { }); router.post('/division', checkLoginStatus.admin, async function(req, res, next) { + const id = req.body['division']; + const remove = req.body['remove']; + try { const name = req.body['name']; const sport = req.body['sport']; const genderName = req.body['gender']; - - const id = req.body['division']; - const remove = req.body['remove']; - + if(remove) await divisions.remove(id); else if(id) await divisions.rename(id, name); else { @@ -141,7 +144,7 @@ router.post('/division', checkLoginStatus.admin, async function(req, res, next) } catch(err) { console.error("ERROR: " + err.message); req.flash("error", "An error has occurred."); - res.redirect('/manage/division'); + res.redirect('/manage/division' + (id ? `?division=${id}` : '')); } });