diff --git a/routes/manage.js b/routes/manage.js index e16f9ff..5b979fa 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -151,19 +151,25 @@ router.post('/division', checkLoginStatus.admin, async function(req, res, next) router.get('/team', checkLoginStatus.admin, function(req, res, next) { let title = req.query.team ? 'Edit Team' : 'Add Team' - res.render('manage/addteam', { title, userLoggedIn: !!req.user }); + res.render('manage/addteam', { title, userLoggedIn: !!req.user, message: req.flash('error') }); }); -router.post('/team', checkLoginStatus.admin, function(req, res, next) { - const name = req.body['name']; - const sport = req.body['sport']; - +router.post('/team', checkLoginStatus.admin, async function(req, res, next) { const id = req.body['team']; const remove = req.body['remove']; - if(remove) teams.remove(id).then(res.redirect('/manage')); - else if(id) teams.rename(id, name).then(res.redirect('/manage')); - else teams.add(name, sport).then(res.redirect("/manage")); + try { + const name = req.body['name']; + const sport = req.body['sport']; + + if(remove) teams.remove(id).then(res.redirect('/manage')); + else if(id) teams.rename(id, name).then(res.redirect('/manage')); + else teams.add(name, sport).then(res.redirect("/manage")); + } catch(err) { + console.error("ERROR: " + err.message); + req.flash("error", "An error has occurred."); + res.redirect('/manage/team' + (id ? `?team=${id}` : '')); + } }); router.get('/account', checkLoginStatus.user, (req, res, next) => { diff --git a/views/manage/addteam.pug b/views/manage/addteam.pug index 9f7262c..78746a6 100644 --- a/views/manage/addteam.pug +++ b/views/manage/addteam.pug @@ -14,6 +14,7 @@ block content label Team name span(class='form-section-input') input#name-textbox(type="text", name="name" disabled) + .error #{message} span(class='form-section') button#submit-button(type="submit" disabled) Submit span(class='form-section')