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,21 +9,31 @@ 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
if req.params.user !== req.user._id && !item.pledgedBy div
form(method='POST', action=`/wishlist/${req.params.user}/pledge/${item.id}`) if req.params.user !== req.user._id && !item.pledgedBy
.field form(method='POST', action=`/wishlist/${req.params.user}/pledge/${item.id}`)
.control .field
input.button.is-primary(type='submit' value='Pledge') .control
if item.pledgedBy === req.user._id input.button.is-primary(type='submit' value='Pledge')
form(method='POST', action=`/wishlist/${req.params.user}/unpledge/${item.id}`) if item.pledgedBy === req.user._id
.field form(method='POST', action=`/wishlist/${req.params.user}/unpledge/${item.id}`)
.control .field
input.button(type='submit' value='Unpledge') .control
if req.user._id === req.params.user input.button(type='submit' value='Unpledge')
form(method='POST', action=`/wishlist/${req.params.user}/remove/${item.id}`) if req.user._id === req.params.user
.field form.inline(method='POST', action=`/wishlist/${req.params.user}/remove/${item.id}`)
.control .field.inline
input.button.is-warning(type='submit' value='Remove') .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') form(method='POST')
.field .field
label.label Item URL or Name label.label Item URL or Name