From a309b35b94a09355c088f172975f60064b04faf9 Mon Sep 17 00:00:00 2001 From: Wingysam Date: Fri, 30 Nov 2018 16:44:06 -0500 Subject: [PATCH] Add notes, show addedBy --- config/index.js | 5 ++- config/wishlist/index.js | 5 +++ routes/wishlist/index.js | 59 +++++++++++++++++++++++- views/note.pug | 13 ++++++ views/wishlist.pug | 97 +++++++++++++++++++++++++++------------- views/wishlists.pug | 2 +- 6 files changed, 144 insertions(+), 37 deletions(-) create mode 100644 config/wishlist/index.js create mode 100644 views/note.pug diff --git a/config/index.js b/config/index.js index e174926..95be6a3 100644 --- a/config/index.js +++ b/config/index.js @@ -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') }; diff --git a/config/wishlist/index.js b/config/wishlist/index.js new file mode 100644 index 0000000..b293cba --- /dev/null +++ b/config/wishlist/index.js @@ -0,0 +1,5 @@ +module.exports = { + note: { + rows: Number(process.env.WISHLIST_NOTE_ROWS) || 5 + } +} \ No newline at end of file diff --git a/routes/wishlist/index.js b/routes/wishlist/index.js index 2316add..892b4a8 100644 --- a/routes/wishlist/index.js +++ b/routes/wishlist/index.js @@ -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; }; \ No newline at end of file diff --git a/views/note.pug b/views/note.pug new file mode 100644 index 0000000..da25fd7 --- /dev/null +++ b/views/note.pug @@ -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') \ No newline at end of file diff --git a/views/wishlist.pug b/views/wishlist.pug index 0dc11dd..7a2e3c0 100644 --- a/views/wishlist.pug +++ b/views/wishlist.pug @@ -4,45 +4,78 @@ block content each item, index in wishlist if req.user._id === item.addedBy || req.params.user !== req.user._id .box - span.overflowWrap - if item.url - a(href=item.url, rel='noopener noreferrer', target='_blank')= (item.name ? item.name : item.url) + span.overflowWrap + if 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 - span= item.name - 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 - 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.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 + .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') .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')) \ No newline at end of file diff --git a/views/wishlists.pug b/views/wishlists.pug index a110817..3890f91 100644 --- a/views/wishlists.pug +++ b/views/wishlists.pug @@ -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) \ No newline at end of file