From d3f379427fe8c40c36fd2cb724f55af261603d66 Mon Sep 17 00:00:00 2001 From: Wingysam Date: Fri, 23 Nov 2018 09:03:16 -0500 Subject: [PATCH] Add moving things up & down wishlist --- routes/wishlist/index.js | 20 ++++++++++++++++++++ views/wishlist.pug | 40 +++++++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/routes/wishlist/index.js b/routes/wishlist/index.js index c481df0..49fec34 100644 --- a/routes/wishlist/index.js +++ b/routes/wishlist/index.js @@ -118,5 +118,25 @@ module.exports = (db) => { return res.redirect(`/wishlist/${req.params.user}`); }); + router.post('/:user/move/:direction/:itemId', verifyAuth(), async (req, res) => { + if (req.user._id !== req.params.user) { + req.flash('error', 'Not correct user'); + return res.redirect(`/wishlist/${req.params.user}`); + } + const doc = await db.get(req.user._id); + const wishlist = doc.wishlist; + if (req.params.direction === 'up') wishlist.reverse(); + let moveFromIndex; + wishlist.forEach(wish => { + if (wish.id === req.params.itemId) return moveFromIndex = wishlist.indexOf(wish); + }); + const moveToIndex = wishlist.findIndex(wish => ( wishlist.indexOf(wish) > moveFromIndex && wish.addedBy === req.user._id )); + [ wishlist[moveFromIndex], wishlist[moveToIndex] ] = [ wishlist[moveToIndex], wishlist[moveFromIndex] ]; + if (req.params.direction === 'up') wishlist.reverse(); + doc.wishlist = wishlist; + await db.put(doc); + req.flash('success', 'Successfully moved item!'); + return res.redirect(`/wishlist/${req.params.user}`); + }); return router; }; \ No newline at end of file diff --git a/views/wishlist.pug b/views/wishlist.pug index faef950..43965e1 100644 --- a/views/wishlist.pug +++ b/views/wishlist.pug @@ -9,21 +9,31 @@ block content a(href=item.url, rel='noopener noreferrer', target='_blank')= (item.name ? item.name : item.url) else span= item.name - if req.params.user !== req.user._id && !item.pledgedBy - form(method='POST', action=`/wishlist/${req.params.user}/pledge/${item.id}`) - .field - .control - input.button.is-primary(type='submit' value='Pledge') - if item.pledgedBy === req.user._id - form(method='POST', action=`/wishlist/${req.params.user}/unpledge/${item.id}`) - .field - .control - input.button(type='submit' value='Unpledge') - if req.user._id === req.params.user - form(method='POST', action=`/wishlist/${req.params.user}/remove/${item.id}`) - .field - .control - input.button.is-warning(type='submit' value='Remove') + div + if req.params.user !== req.user._id && !item.pledgedBy + form(method='POST', action=`/wishlist/${req.params.user}/pledge/${item.id}`) + .field + .control + input.button.is-primary(type='submit' value='Pledge') + if item.pledgedBy === req.user._id + form(method='POST', action=`/wishlist/${req.params.user}/unpledge/${item.id}`) + .field + .control + input.button(type='submit' value='Unpledge') + if req.user._id === req.params.user + form.inline(method='POST', action=`/wishlist/${req.params.user}/remove/${item.id}`) + .field.inline + .control.inline + input.inline.button.is-warning(type='submit' value='Remove') + if req.user._id === req.params.user + form.inline(method='POST', action=`/wishlist/${req.params.user}/move/up/${item.id}`) + .field.inline + .control.inline + input.inline.button(type='submit' value='Move up') + form.inline(method='POST', action=`/wishlist/${req.params.user}/move/down/${item.id}`) + .field.inline + .control.inline + input.inline.button(type='submit' value='Move down') form(method='POST') .field label.label Item URL or Name