Check to make sure move is valid
This commit is contained in:
parent
d3f379427f
commit
53869bcf6c
2 changed files with 20 additions and 6 deletions
|
@ -34,9 +34,17 @@ module.exports = (db) => {
|
||||||
router.get('/:user', verifyAuth(), async (req, res) => {
|
router.get('/:user', verifyAuth(), async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const dbUser = await db.get(req.params.user);
|
const dbUser = await db.get(req.params.user);
|
||||||
res.render('wishlist', { title: `Wishlist - ${dbUser._id}`, wishlist: dbUser.wishlist });
|
const wishlistReverse = [...dbUser.wishlist].reverse();
|
||||||
|
const lastCanSeeValue = wishlistReverse.find(element => (element.addedBy === req.params.user));
|
||||||
|
const lastCanSee = dbUser.wishlist.indexOf(lastCanSeeValue);
|
||||||
|
res.render('wishlist', {
|
||||||
|
title: `Wishlist - ${dbUser._id}`,
|
||||||
|
wishlist: dbUser.wishlist,
|
||||||
|
lastCanSee
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.redirect('/wishlist');
|
req.flash('error', error);
|
||||||
|
return res.redirect('/wishlist');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -131,6 +139,11 @@ module.exports = (db) => {
|
||||||
if (wish.id === req.params.itemId) return moveFromIndex = wishlist.indexOf(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 ));
|
const moveToIndex = wishlist.findIndex(wish => ( wishlist.indexOf(wish) > moveFromIndex && wish.addedBy === req.user._id ));
|
||||||
|
if (moveToIndex < 0 || moveToIndex > wishlist.length) {
|
||||||
|
console.log(moveToIndex, '<', 0, '||', moveToIndex, '>', wishlist.length);
|
||||||
|
req.flash('error', 'Invalid move');
|
||||||
|
return res.redirect(`/wishlist/${req.params.user}`);
|
||||||
|
}
|
||||||
[ wishlist[moveFromIndex], wishlist[moveToIndex] ] = [ wishlist[moveToIndex], wishlist[moveFromIndex] ];
|
[ wishlist[moveFromIndex], wishlist[moveToIndex] ] = [ wishlist[moveToIndex], wishlist[moveFromIndex] ];
|
||||||
if (req.params.direction === 'up') wishlist.reverse();
|
if (req.params.direction === 'up') wishlist.reverse();
|
||||||
doc.wishlist = wishlist;
|
doc.wishlist = wishlist;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
extends layout.pug
|
extends layout.pug
|
||||||
|
|
||||||
block content
|
block content
|
||||||
for item in wishlist
|
each item, index in wishlist
|
||||||
if req.user._id === item.addedBy || req.params.user !== req.user._id
|
if req.user._id === item.addedBy || req.params.user !== req.user._id
|
||||||
.box
|
.box
|
||||||
span
|
span
|
||||||
|
@ -25,15 +25,16 @@ block content
|
||||||
.field.inline
|
.field.inline
|
||||||
.control.inline
|
.control.inline
|
||||||
input.inline.button.is-warning(type='submit' value='Remove')
|
input.inline.button.is-warning(type='submit' value='Remove')
|
||||||
if req.user._id === req.params.user
|
- console.log(lastCanSee);
|
||||||
|
if req.user._id === req.params.user && index !== 0 && index !== lastCanSee
|
||||||
form.inline(method='POST', action=`/wishlist/${req.params.user}/move/up/${item.id}`)
|
form.inline(method='POST', action=`/wishlist/${req.params.user}/move/up/${item.id}`)
|
||||||
.field.inline
|
.field.inline
|
||||||
.control.inline
|
.control.inline
|
||||||
input.inline.button(type='submit' value='Move up')
|
input.inline.button(type='submit' value='Move item up')
|
||||||
form.inline(method='POST', action=`/wishlist/${req.params.user}/move/down/${item.id}`)
|
form.inline(method='POST', action=`/wishlist/${req.params.user}/move/down/${item.id}`)
|
||||||
.field.inline
|
.field.inline
|
||||||
.control.inline
|
.control.inline
|
||||||
input.inline.button(type='submit' value='Move down')
|
input.inline.button(type='submit' value='Move item down')
|
||||||
form(method='POST')
|
form(method='POST')
|
||||||
.field
|
.field
|
||||||
label.label Item URL or Name
|
label.label Item URL or Name
|
||||||
|
|
Loading…
Reference in a new issue