From 8e5cb9dfcc4d6fc3054fc777dab5329cc2250494 Mon Sep 17 00:00:00 2001 From: b1ek Date: Mon, 27 Feb 2023 18:04:10 +1000 Subject: [PATCH] add guestbook panel with hide functionality --- public/static/main.css | 1 - public/static/ui/control_panel.css | 22 ++++++++++++++ routes/admin.js | 28 +++++++++++++++++- view/admin/panel.pug | 46 ++++++++++++++++++++++++++++++ view/ui/button.pug | 2 ++ 5 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 public/static/ui/control_panel.css create mode 100644 view/admin/panel.pug create mode 100644 view/ui/button.pug diff --git a/public/static/main.css b/public/static/main.css index f4f66df..4226ed3 100644 --- a/public/static/main.css +++ b/public/static/main.css @@ -61,7 +61,6 @@ a:visited { .main_contents { padding: 16px 12px; } - .flag_hr { background: linear-gradient(0,#5BCEFA 20%,#F5A9B8 20%,40%,#FFFFFF 40%,60%,#F5A9B8 60%,80%,#5BCEFA 80%); diff --git a/public/static/ui/control_panel.css b/public/static/ui/control_panel.css new file mode 100644 index 0000000..eac40f3 --- /dev/null +++ b/public/static/ui/control_panel.css @@ -0,0 +1,22 @@ +.cp_panel_panel { + border: 1px solid #c2c4c2; + padding: 4px; + min-height: 300px; + font-size: 9pt; +} +.cp_panel_panel h5 { + font-size: 1.1em; + margin: 3px 0; + padding: 0; +} +.cp_panel_panel a { + text-decoration: underline; + color: blue; + font-size: 100%; +} +.cp_gb_entry_hidden { + filter: opacity(0.7) +} +input[type=submit] { + cursor: pointer; +} \ No newline at end of file diff --git a/routes/admin.js b/routes/admin.js index be45b58..97ef572 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -27,12 +27,37 @@ async function apiLogin(req, res) { } async function panel(req, res) { + + const gb_records = await db.Guestbook.findAll({ + order: [['id', 'DESC']] + }); + res.send(await Helpers.ViewLoader.load('admin/panel.pug', { - current_route: req.originalUrl + current_route: req.originalUrl, + gb_records })); return; } +async function gb_api(req, res) { + let action = false; + const id = req.body.id; + + if (req.body.hide) action = 'hide'; + + if (!action) { + res.redirect('/admin/panel'); + return; + } + + switch (action) { + case 'hide': + const response = await db.Guestbook.update({hidden: db.Sequelize.literal('NOT hidden')}, {where: {id}}) + res.redirect('/admin/panel'); + return; + } +} + module.exports = (router) => { // login @@ -42,5 +67,6 @@ module.exports = (router) => { // panel router.get('/admin/panel', handler(panel)); + router.post('/admin/panel/gb_api', handler(gb_api)); } \ No newline at end of file diff --git a/view/admin/panel.pug b/view/admin/panel.pug new file mode 100644 index 0000000..60ea574 --- /dev/null +++ b/view/admin/panel.pug @@ -0,0 +1,46 @@ +extends ../layout/main.pug +block root + - var title = 'Admin panel' + +block head + link(rel='stylesheet' href='/static/ui/control_panel.css') + +block content + h2 Admin panel + hr + if !access_level + p(style='color:#a02020;font-weight:bold;font-size:1em') + | Warning: Your access level is unkown, so everything is displayed and some funtions may not work. + hr + - access_level = 4 + table + tr + td(class='cp_panel_panel') + h5 Guestbook panel + hr + p + a(href='/admin/panel/guestbook.editor') Edit data + br + a(href='/admin/panel/guestbook.csv') Download data (.CSV) + br + a(href='/admin/panel/guestbook.csv') Download data (SQL) + hr + table + each record of gb_records + tr(class='' + (record.hidden ? 'cp_gb_entry_hidden' : '')) + form(action='/admin/panel/gb_api' method='POST') + input(type='hidden' name='id' value=record.id) + td + a(href='/guestbook#gb_entry_' + record.id)= record.id + | : #{record.name} + td + if (record.text.length > 40) + | #{record.text.substr(0, 40)}... + else + | #{record.text} + td + if (!record.hidden) + input(type='submit' name='hide' value='Hide') + else + input(type='submit' name='hide' value='Unhide') + \ No newline at end of file diff --git a/view/ui/button.pug b/view/ui/button.pug new file mode 100644 index 0000000..ba567ee --- /dev/null +++ b/view/ui/button.pug @@ -0,0 +1,2 @@ +mixin button(text) + \ No newline at end of file