Add get product data refresh

This commit is contained in:
Wingy 2020-11-07 15:54:53 -05:00
parent 120be27e44
commit ea36220f38
2 changed files with 34 additions and 1 deletions

View file

@ -223,6 +223,34 @@ module.exports = (db) => {
req.flash('success', `Successfully saved note!`); req.flash('success', `Successfully saved note!`);
return res.redirect(`/wishlist/${req.params.user}`); 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) => { router.post('/:user/note/remove/:id', verifyAuth(), async (req, res) => {
const doc = await db.get(req.params.user); const doc = await db.get(req.params.user);
const wishlist = doc.wishlist; const wishlist = doc.wishlist;

View file

@ -2,7 +2,8 @@ extends layout.pug
block content block content
a(href='..') <i class="fas fa-arrow-left"></i> Back to #{req.params.user}'s wishlist a(href='..') <i class="fas fa-arrow-left"></i> 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 .columns
.column .column
.field .field
@ -24,6 +25,10 @@ block content
label.label Image URL label.label Image URL
.control .control
input.input(name='image', value=item.image) 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 .field
label.label Note label.label Note
.control .control