Add notes, show addedBy

This commit is contained in:
Wingysam 2018-11-30 16:44:06 -05:00
parent 650eaafa8a
commit a309b35b94
6 changed files with 144 additions and 37 deletions

View file

@ -5,10 +5,11 @@ const yesNo = require('yes-no');
module.exports = { module.exports = {
dbUrl: process.env.DB_URL || 'db', dbUrl: process.env.DB_URL || 'db',
defaultFailureRedirect: process.env.DEFAULT_FAILURE_REDIRECT || '/login', defaultFailureRedirect: process.env.DEFAULT_FAILURE_REDIRECT || '/login',
port: process.env.PORT || 3000, port: Number(process.env.PORT) || 3000,
proxyServer: process.env.PROXY_SERVER || undefined, proxyServer: process.env.PROXY_SERVER || undefined,
secret: process.env.SECRET || require('./secret'), secret: process.env.SECRET || require('./secret'),
sessionMaxAge: Number(process.env.SESSION_MAX_AGE) || 1000 * 60 * 60 * 24 * 7, sessionMaxAge: Number(process.env.SESSION_MAX_AGE) || 1000 * 60 * 60 * 24 * 7,
siteTitle: process.env.SITE_TITLE || 'Christmas Community', 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
View file

@ -0,0 +1,5 @@
module.exports = {
note: {
rows: Number(process.env.WISHLIST_NOTE_ROWS) || 5
}
}

View file

@ -68,7 +68,14 @@ module.exports = (db) => {
const doc = await db.get(req.params.user); const doc = await db.get(req.params.user);
doc.wishlist.push(item); doc.wishlist.push(item);
await db.put(doc); 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}`); 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 )); const moveToIndex = wishlist.findIndex(wish => ( wishlist.indexOf(wish) > moveFromIndex && wish.addedBy === req.user._id ));
if (moveToIndex < 0 || moveToIndex > wishlist.length) { if (moveToIndex < 0 || moveToIndex > wishlist.length) {
console.log(moveToIndex, '<', 0, '||', moveToIndex, '>', wishlist.length);
req.flash('error', 'Invalid move'); req.flash('error', 'Invalid move');
return res.redirect(`/wishlist/${req.params.user}`); return res.redirect(`/wishlist/${req.params.user}`);
} }
@ -153,5 +159,54 @@ module.exports = (db) => {
req.flash('success', 'Successfully moved item!'); req.flash('success', 'Successfully moved item!');
return res.redirect(`/wishlist/${req.params.user}`); 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; return router;
}; };

13
views/note.pug Normal file
View 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')

View file

@ -4,45 +4,78 @@ block content
each item, index 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.overflowWrap span.overflowWrap
if item.url 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}`)
.field.inline
.control.inline
input.inline.button.is-primary(type='submit' value='Pledge')
if item.pledgedBy === req.user._id
form.inline(method='POST', action=`/wishlist/${req.params.user}/unpledge/${item.id}`)
.field.inline
.control.inline
input.inline.button(type='submit' value='Unpledge')
if req.user._id === req.params.user
form.inline(method='POST', action=`/wishlist/${req.params.user}/remove/${item.id}`)
.field.inline
.control.inline
input.inline.button.is-warning(type='submit' value='Remove')
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 else
span= item.name form.inline(method='GET', action=`/wishlist/${req.params.user}/note/${item.id}`)
div .field.inline
if req.params.user !== req.user._id && !item.pledgedBy .control.inline
form.inline(method='POST', action=`/wishlist/${req.params.user}/pledge/${item.id}`) input.inline.button(type='submit', value='Add note')
.field.inline if index !== firstCanSee
.control.inline form.inline(method='POST', action=`/wishlist/${req.params.user}/move/up/${item.id}`)
input.inline.button.is-primary(type='submit' value='Pledge') .field.inline
if item.pledgedBy === req.user._id .control.inline
form.inline(method='POST', action=`/wishlist/${req.params.user}/unpledge/${item.id}`) input.inline.button(type='submit' value='Move item up')
.field.inline if index !== lastCanSee
.control.inline form.inline(method='POST', action=`/wishlist/${req.params.user}/move/down/${item.id}`)
input.inline.button(type='submit' value='Unpledge') .field.inline
if req.user._id === req.params.user .control.inline
form.inline(method='POST', action=`/wishlist/${req.params.user}/remove/${item.id}`) input.inline.button(type='submit' value='Move item down')
.field.inline
.control.inline
input.inline.button.is-warning(type='submit' value='Remove')
if req.user._id === req.params.user
if index !== firstCanSee
form.inline(method='POST', action=`/wishlist/${req.params.user}/move/up/${item.id}`)
.field.inline
.control.inline
input.inline.button(type='submit' value='Move item up')
if index !== lastCanSee
form.inline(method='POST', action=`/wishlist/${req.params.user}/move/down/${item.id}`)
.field.inline
.control.inline
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
.control.has-icons-left .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 span.icon.is-small.is-left
i.fas.fa-gift i.fas.fa-gift
.field
label.label Note
.control.has-icons-left
textarea.textarea(
name='note',
placeholder='Optional'
)
.field .field
.control .control
input.button(type='submit' value=(req.user._id === req.params.user ? 'Add item to wishlist' : 'Pledge item')) input.button(type='submit' value=(req.user._id === req.params.user ? 'Add item to wishlist' : 'Pledge item'))

View file

@ -9,5 +9,5 @@ block content
a(href=`/wishlist/${user.id}`) a(href=`/wishlist/${user.id}`)
.box .box
span= user.id 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) progress.progress.is-info(value=totals(user.doc.wishlist).pledged, max=user.doc.wishlist.length)