Improve redirection when error occurs

main
sudoer777 2021-11-26 16:59:51 -07:00
parent ad361adab1
commit 63b6ef2c2d
2 changed files with 20 additions and 20 deletions

View File

@ -1,7 +1,6 @@
import * as Data from "../data.js"; import * as Data from "../data.js";
const mainHeader = document.getElementById('main-header');
const nameTextbox = document.getElementById('name-textbox'); const nameTextbox = document.getElementById('name-textbox');
const submitButton = document.getElementById('submit-button'); const submitButton = document.getElementById('submit-button');
const deleteButton = document.getElementById('delete-button'); const deleteButton = document.getElementById('delete-button');
@ -12,8 +11,6 @@ async function initializeForm() {
let params = new URLSearchParams(location.search); let params = new URLSearchParams(location.search);
let sportID = params.get('sport') let sportID = params.get('sport')
if(sportID) { if(sportID) {
mainHeader.textContent = "Edit Sport";
const sportName = await Data.getSportName(sportID); const sportName = await Data.getSportName(sportID);
nameTextbox.value = sportName; nameTextbox.value = sportName;

View File

@ -25,6 +25,9 @@ router.get('/game', checkLoginStatus.user, function(req, res, next) {
}); });
router.post('/game', checkLoginStatus.user, async 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 { try {
const seasonID = req.body['year']; const seasonID = req.body['year'];
const sportID = req.body['sport']; 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 team2Score = req.body['team2-score'];
const userID = req.user[0]; const userID = req.user[0];
const id = req.body['game'];
const remove = req.body['remove'];
const loggedInUserID = req.user[0]; const loggedInUserID = req.user[0];
const loggedInUserIsAdmin = req.user[2]; const loggedInUserIsAdmin = req.user[2];
@ -63,7 +63,7 @@ router.post('/game', checkLoginStatus.user, async function(req, res, next) {
} catch(err) { } catch(err) {
console.error("ERROR: " + err.message); console.error("ERROR: " + err.message);
req.flash("error", "An error has occurred."); 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) { router.post('/season', checkLoginStatus.admin, async function(req, res, next) {
const seasonID = req.body['season'];
const remove = req.body['remove'];
try { try {
const year = req.body['year']; const year = req.body['year'];
const seasonID = req.body['season'];
const remove = req.body['remove'];
if(remove) await seasons.remove(seasonID); if(remove) await seasons.remove(seasonID);
else await seasons.add(year); else await seasons.add(year);
@ -85,19 +85,22 @@ router.post('/season', checkLoginStatus.admin, async function(req, res, next) {
} catch(err) { } catch(err) {
console.error("ERROR: " + err.message); console.error("ERROR: " + err.message);
req.flash("error", "An error has occurred."); 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) { 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) { router.post('/sport', checkLoginStatus.admin, async function(req, res, next) {
const id = req.body['sport'];
const remove = req.body['remove'];
try { try {
const name = req.body['name']; const name = req.body['name'];
const id = req.body['sport'];
const remove = req.body['remove'];
if(remove) await sports.remove(id); if(remove) await sports.remove(id);
else if(id) await sports.rename(id, name); else if(id) await sports.rename(id, name);
@ -107,7 +110,7 @@ router.post('/sport', checkLoginStatus.admin, async function(req, res, next) {
} catch(err) { } catch(err) {
console.error("ERROR: " + err.message); console.error("ERROR: " + err.message);
req.flash("error", "An error has occurred."); 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) { router.post('/division', checkLoginStatus.admin, async function(req, res, next) {
const id = req.body['division'];
const remove = req.body['remove'];
try { try {
const name = req.body['name']; const name = req.body['name'];
const sport = req.body['sport']; const sport = req.body['sport'];
const genderName = req.body['gender']; const genderName = req.body['gender'];
const id = req.body['division'];
const remove = req.body['remove'];
if(remove) await divisions.remove(id); if(remove) await divisions.remove(id);
else if(id) await divisions.rename(id, name); else if(id) await divisions.rename(id, name);
else { else {
@ -141,7 +144,7 @@ router.post('/division', checkLoginStatus.admin, async function(req, res, next)
} catch(err) { } catch(err) {
console.error("ERROR: " + err.message); console.error("ERROR: " + err.message);
req.flash("error", "An error has occurred."); req.flash("error", "An error has occurred.");
res.redirect('/manage/division'); res.redirect('/manage/division' + (id ? `?division=${id}` : ''));
} }
}); });