move to top button

This commit is contained in:
Wingy 2021-01-02 17:17:20 -05:00
parent 884c699c8f
commit 5b87859a82
2 changed files with 31 additions and 12 deletions

View file

@ -184,19 +184,26 @@ module.exports = (db) => {
return res.redirect(`/wishlist/${req.params.user}`) return res.redirect(`/wishlist/${req.params.user}`)
} }
const doc = await db.get(req.user._id) const doc = await db.get(req.user._id)
const wishlist = doc.wishlist let wishlist = doc.wishlist
if (req.params.direction === 'up') wishlist.reverse() if (req.params.direction === 'top') {
let moveFromIndex const item = wishlist.find(item => item.id === req.params.itemId)
wishlist.forEach(wish => { wishlist = wishlist.filter(item => item.id !== req.params.itemId)
if (wish.id === req.params.itemId) moveFromIndex = wishlist.indexOf(wish) wishlist.unshift(item)
}) } else {
const moveToIndex = wishlist.findIndex(wish => (wishlist.indexOf(wish) > moveFromIndex && wish.addedBy === req.user._id)) if (req.params.direction === 'up') wishlist.reverse()
if (moveToIndex < 0 || moveToIndex > wishlist.length) { let moveFromIndex
req.flash('error', 'Invalid move') wishlist.forEach(wish => {
return res.redirect(`/wishlist/${req.params.user}`) if (wish.id === req.params.itemId) moveFromIndex = wishlist.indexOf(wish)
})
const moveToIndex = wishlist.findIndex(wish => (wishlist.indexOf(wish) > moveFromIndex && wish.addedBy === req.user._id))
if (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]]
if (req.params.direction === 'up') wishlist.reverse()
} }
[wishlist[moveFromIndex], wishlist[moveToIndex]] = [wishlist[moveToIndex], wishlist[moveFromIndex]]
if (req.params.direction === 'up') wishlist.reverse()
doc.wishlist = wishlist doc.wishlist = wishlist
await db.put(doc) await db.put(doc)
req.flash('success', 'Successfully moved item!') req.flash('success', 'Successfully moved item!')

View file

@ -24,6 +24,7 @@ block content
th Edit Item th Edit Item
th Added By th Added By
if req.params.user === req.user._id if req.params.user === req.user._id
th Move Top
th Move Up th Move Up
th Move Down th Move Down
else else
@ -64,6 +65,17 @@ block content
i.far.fa-edit i.far.fa-edit
td.ugc(data-label='Added By')= item.addedBy td.ugc(data-label='Added By')= item.addedBy
if req.params.user === req.user._id if req.params.user === req.user._id
td(data-label='Move Item Top')
form.topForm.inline(method='POST', action=`${_CC.config.base}wishlist/${req.params.user}/move/top/${item.id}`)
.field.inline
.control.inline
button.button.is-text(
type='submit',
style='text-decoration: none;',
disabled=index === firstCanSee
)
span.icon
i.fas.fa-angle-double-up
td(data-label='Move Item Up') td(data-label='Move Item Up')
form.upForm.inline(method='POST', action=`${_CC.config.base}wishlist/${req.params.user}/move/up/${item.id}`) form.upForm.inline(method='POST', action=`${_CC.config.base}wishlist/${req.params.user}/move/up/${item.id}`)
.field.inline .field.inline