Add get product data refresh
This commit is contained in:
parent
120be27e44
commit
ea36220f38
2 changed files with 34 additions and 1 deletions
|
@ -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;
|
||||
|
|
|
@ -2,7 +2,8 @@ extends layout.pug
|
|||
|
||||
block content
|
||||
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
|
||||
.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
|
||||
|
|
Loading…
Reference in a new issue