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";
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;

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) {
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) {
try {
const year = req.body['year'];
const seasonID = req.body['season'];
const remove = req.body['remove'];
try {
const year = req.body['year'];
if(remove) await seasons.remove(seasonID);
else await seasons.add(year);
@ -85,20 +85,23 @@ 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) {
try {
const name = req.body['name'];
const id = req.body['sport'];
const remove = req.body['remove'];
try {
const name = req.body['name'];
if(remove) await sports.remove(id);
else if(id) await sports.rename(id, name);
else await sports.add(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}` : ''));
}
});