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}`); 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; return router;
}; };

View file

@ -9,6 +9,7 @@ block content
a(href=item.url, rel='noopener noreferrer', target='_blank')= (item.name ? item.name : item.url) a(href=item.url, rel='noopener noreferrer', target='_blank')= (item.name ? item.name : item.url)
else else
span= item.name span= item.name
div
if req.params.user !== req.user._id && !item.pledgedBy if req.params.user !== req.user._id && !item.pledgedBy
form(method='POST', action=`/wishlist/${req.params.user}/pledge/${item.id}`) form(method='POST', action=`/wishlist/${req.params.user}/pledge/${item.id}`)
.field .field
@ -20,10 +21,19 @@ block content
.control .control
input.button(type='submit' value='Unpledge') input.button(type='submit' value='Unpledge')
if req.user._id === req.params.user if req.user._id === req.params.user
form(method='POST', action=`/wishlist/${req.params.user}/remove/${item.id}`) form.inline(method='POST', action=`/wishlist/${req.params.user}/remove/${item.id}`)
.field .field.inline
.control .control.inline
input.button.is-warning(type='submit' value='Remove') 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') form(method='POST')
.field .field
label.label Item URL or Name label.label Item URL or Name