diff --git a/routes/wishlist/index.js b/routes/wishlist/index.js index 0b72fa4..49ab018 100644 --- a/routes/wishlist/index.js +++ b/routes/wishlist/index.js @@ -223,6 +223,34 @@ module.exports = (db) => { req.flash('success', `Successfully saved note!`); return res.redirect(`/wishlist/${req.params.user}`); }); + router.post('/:user/refresh/: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.url) { + req.flash('error', 'Item has no URL.') + return res.redirect(`/wishlist/${req.params.user}/note/${req.params.id}`) + } + + const productData = await getProductName(wishlistItem.url) + for (field of [ 'name', 'price', 'image' ]) { + if (productData[field]) wishlistItem[field] = productData[field] + } + + wishlist[i] = wishlistItem; + } + doc.wishlist = wishlist; + await db.put(doc); + req.flash('success', `Successfully refreshed data!`); + return res.redirect(`/wishlist/${req.params.user}/note/${req.params.id}`); + }); router.post('/:user/note/remove/:id', verifyAuth(), async (req, res) => { const doc = await db.get(req.params.user); const wishlist = doc.wishlist; diff --git a/views/note.pug b/views/note.pug index f25226d..e9879e5 100644 --- a/views/note.pug +++ b/views/note.pug @@ -2,7 +2,8 @@ extends layout.pug block content a(href='..') Back to #{req.params.user}'s wishlist - form.inline(method='POST', action=`/wishlist/${req.params.user}/note/${req.params.id}`) + form(id='refreshform', method='POST', action=`${_CC.config.base}wishlist/${req.params.user}/refresh/${req.params.id}`) + form.inline(method='POST', action=`${_CC.config.base}wishlist/${req.params.user}/note/${req.params.id}`) .columns .column .field @@ -24,6 +25,10 @@ block content label.label Image URL .control input.input(name='image', value=item.image) + .column.is-narrow + .field + label.label Get Product Data + input.is-ghost.button(type='submit', value='Refresh Data' style='width: 100%;', form='refreshform') .field label.label Note .control