Add moving things up & down wishlist

This commit is contained in:
Wingysam 2018-11-23 09:03:16 -05:00
parent b7db7e0ec8
commit d3f379427f
2 changed files with 45 additions and 15 deletions

View file

@ -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;
};

View file

@ -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