Add notes, show addedBy
This commit is contained in:
parent
650eaafa8a
commit
a309b35b94
6 changed files with 144 additions and 37 deletions
|
@ -5,10 +5,11 @@ const yesNo = require('yes-no');
|
|||
module.exports = {
|
||||
dbUrl: process.env.DB_URL || 'db',
|
||||
defaultFailureRedirect: process.env.DEFAULT_FAILURE_REDIRECT || '/login',
|
||||
port: process.env.PORT || 3000,
|
||||
port: Number(process.env.PORT) || 3000,
|
||||
proxyServer: process.env.PROXY_SERVER || undefined,
|
||||
secret: process.env.SECRET || require('./secret'),
|
||||
sessionMaxAge: Number(process.env.SESSION_MAX_AGE) || 1000 * 60 * 60 * 24 * 7,
|
||||
siteTitle: process.env.SITE_TITLE || 'Christmas Community',
|
||||
useCDN: yesNo.parse(process.env.USE_CDN || true)
|
||||
useCDN: yesNo.parse(process.env.USE_CDN || true),
|
||||
wishlist: require('./wishlist')
|
||||
};
|
||||
|
|
5
config/wishlist/index.js
Normal file
5
config/wishlist/index.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
module.exports = {
|
||||
note: {
|
||||
rows: Number(process.env.WISHLIST_NOTE_ROWS) || 5
|
||||
}
|
||||
}
|
|
@ -68,7 +68,14 @@ module.exports = (db) => {
|
|||
const doc = await db.get(req.params.user);
|
||||
doc.wishlist.push(item);
|
||||
await db.put(doc);
|
||||
req.flash('success', (req.user._id === req.params.user ? 'Added item to wishlist' : `Pleged item for ${req.params.user}`));
|
||||
req.flash(
|
||||
'success',
|
||||
(
|
||||
req.user._id === req.params.user
|
||||
? 'Added item to wishlist'
|
||||
: `Pleged item for ${req.params.user}`
|
||||
)
|
||||
);
|
||||
res.redirect(`/wishlist/${req.params.user}`);
|
||||
});
|
||||
|
||||
|
@ -142,7 +149,6 @@ module.exports = (db) => {
|
|||
});
|
||||
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}`);
|
||||
}
|
||||
|
@ -153,5 +159,54 @@ module.exports = (db) => {
|
|||
req.flash('success', 'Successfully moved item!');
|
||||
return res.redirect(`/wishlist/${req.params.user}`);
|
||||
});
|
||||
|
||||
router.get('/:user/note/:id', verifyAuth(), (_, res) => {
|
||||
res.render('note');
|
||||
});
|
||||
router.post('/:user/note/:id', verifyAuth(), async (req, res) => {
|
||||
const doc = await db.get(req.params.user);
|
||||
const wishlist = doc.wishlist;
|
||||
for (let i=0; i < wishlist.length; i++) {
|
||||
wishlistItem = wishlist[i];
|
||||
if (wishlistItem.id !== req.params.id) continue;
|
||||
if (req.user._id !== req.params.user && req.user._id !== wishlistItem.addedBy) {
|
||||
req.flash('error', 'Invalid user');
|
||||
return res.redirect(`/wishlist/${req.params.user}`);
|
||||
}
|
||||
if (wishlistItem.note) {
|
||||
req.flash('error', 'Already has a note');
|
||||
return res.redirect(`/wishlist/${req.params.user}`);
|
||||
} else {
|
||||
wishlistItem.note = req.body.note;
|
||||
wishlist[i] = wishlistItem;
|
||||
}
|
||||
}
|
||||
doc.wishlist = wishlist;
|
||||
await db.put(doc);
|
||||
req.flash('success', 'Successfully added note!');
|
||||
return res.redirect(`/wishlist/${req.params.user}`);
|
||||
});
|
||||
router.post('/:user/note/remove/:id', verifyAuth(), async (req, res) => {
|
||||
const doc = await db.get(req.params.user);
|
||||
const wishlist = doc.wishlist;
|
||||
for (let i=0; i < wishlist.length; i++) {
|
||||
wishlistItem = wishlist[i];
|
||||
if (wishlistItem.id !== req.params.id) continue;
|
||||
if (req.user._id !== req.params.user && req.user._id !== wishlistItem.addedBy) {
|
||||
req.flash('error', 'Invalid user');
|
||||
return res.redirect(`/wishlist/${req.params.user}`);
|
||||
}
|
||||
if (wishlistItem.note) {
|
||||
wishlistItem.note = undefined;
|
||||
wishlist[i] = wishlistItem;
|
||||
} else {
|
||||
req.flash('error', 'Has no note');
|
||||
return res.redirect(`/wishlist/${req.params.user}`); }
|
||||
}
|
||||
doc.wishlist = wishlist;
|
||||
await db.put(doc);
|
||||
req.flash('success', 'Successfully removed note');
|
||||
return res.redirect(`/wishlist/${req.params.user}`);
|
||||
})
|
||||
return router;
|
||||
};
|
13
views/note.pug
Normal file
13
views/note.pug
Normal file
|
@ -0,0 +1,13 @@
|
|||
extends layout.pug
|
||||
|
||||
block content
|
||||
form(method='POST', action=`/wishlist/${req.params.user}/note/${req.params.id}`)
|
||||
.field
|
||||
label.label Note
|
||||
.control.has-icons-left
|
||||
textarea.textarea(
|
||||
name='note'
|
||||
)
|
||||
.field
|
||||
.control
|
||||
input.button.is-primary(type='submit' value='Add Note')
|
|
@ -6,9 +6,21 @@ block content
|
|||
.box
|
||||
span.overflowWrap
|
||||
if item.url
|
||||
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
|
||||
span= item.name
|
||||
hr
|
||||
if item.note
|
||||
textarea.textarea.has-fixed-size(
|
||||
rows=String(config.wishlist.note.rows),
|
||||
readonly
|
||||
)= item.note
|
||||
span.overflowWrap Added by: #{item.addedBy}
|
||||
hr
|
||||
div
|
||||
if req.params.user !== req.user._id && !item.pledgedBy
|
||||
form.inline(method='POST', action=`/wishlist/${req.params.user}/pledge/${item.id}`)
|
||||
|
@ -25,7 +37,17 @@ block content
|
|||
.field.inline
|
||||
.control.inline
|
||||
input.inline.button.is-warning(type='submit' value='Remove')
|
||||
if req.user._id === req.params.user
|
||||
if req.user._id === req.params.user || req.user._id === item.addedBy
|
||||
if (item.note)
|
||||
form.inline(method='POST', action=`/wishlist/${req.params.user}/note/remove/${item.id}`)
|
||||
.field.inline
|
||||
.control.inline
|
||||
input.inline.button.is-warning(type='submit', value='Remove note')
|
||||
else
|
||||
form.inline(method='GET', action=`/wishlist/${req.params.user}/note/${item.id}`)
|
||||
.field.inline
|
||||
.control.inline
|
||||
input.inline.button(type='submit', value='Add note')
|
||||
if index !== firstCanSee
|
||||
form.inline(method='POST', action=`/wishlist/${req.params.user}/move/up/${item.id}`)
|
||||
.field.inline
|
||||
|
@ -40,9 +62,20 @@ block content
|
|||
.field
|
||||
label.label Item URL or Name
|
||||
.control.has-icons-left
|
||||
input.input(type='text', name='itemUrlOrName', placeholder='https://www.amazon.com/dp/B00ZV9RDKK')
|
||||
input.input(
|
||||
type='text',
|
||||
name='itemUrlOrName',
|
||||
placeholder='https://www.amazon.com/dp/B00ZV9RDKK'
|
||||
)
|
||||
span.icon.is-small.is-left
|
||||
i.fas.fa-gift
|
||||
.field
|
||||
label.label Note
|
||||
.control.has-icons-left
|
||||
textarea.textarea(
|
||||
name='note',
|
||||
placeholder='Optional'
|
||||
)
|
||||
.field
|
||||
.control
|
||||
input.button(type='submit' value=(req.user._id === req.params.user ? 'Add item to wishlist' : 'Pledge item'))
|
|
@ -9,5 +9,5 @@ block content
|
|||
a(href=`/wishlist/${user.id}`)
|
||||
.box
|
||||
span= user.id
|
||||
span= `: ${totals(user.doc.wishlist).pledged}/${user.doc.wishlist.length}`
|
||||
span : #{totals(user.doc.wishlist).pledged}/#{user.doc.wishlist.length}
|
||||
progress.progress.is-info(value=totals(user.doc.wishlist).pledged, max=user.doc.wishlist.length)
|
Loading…
Reference in a new issue