Skip to content

Commit e71ea24

Browse files
committed
implement async requests module (client JS, JSON, background requests, DOM updates.
1 parent ae755ae commit e71ea24

File tree

6 files changed

+32
-14
lines changed

6 files changed

+32
-14
lines changed

controllers/admin.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ exports.getProducts = (req, res, next) => {
184184
});
185185
};
186186

187-
exports.postDeleteProduct = (req, res, next) => {
188-
const prodId = req.body.productId;
187+
exports.deleteProduct = (req, res, next) => {
188+
const prodId = req.params.productId;
189189
Product.findById(prodId)
190190
.then(product => {
191191
if (!product) {
@@ -196,11 +196,9 @@ exports.postDeleteProduct = (req, res, next) => {
196196
})
197197
.then(() => {
198198
console.log('DESTROYED PRODUCT');
199-
res.redirect('/admin/products');
199+
res.status(200).json( { message: 'Success!' });
200200
})
201201
.catch(err => {
202-
const error = new Error(err);
203-
error.httpStatusCode = 500;
204-
return next(error);
202+
res.status(500).json({ message: 'Deleting product failed.' });
205203
});
206204
};
-2.79 KB
Binary file not shown.
-2.79 KB
Binary file not shown.

public/js/admin.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const deleteProduct = (btn) => {
2+
const prodId = btn.parentNode.querySelector('[name=productId]').value;
3+
const csrfToken = btn.parentNode.querySelector('[name=_csrf]').value;
4+
5+
const productElement = btn.closest('article');
6+
7+
fetch('/admin/product/' + prodId, {
8+
method: 'DELETE',
9+
headers: {
10+
'csrf-token': csrfToken
11+
}
12+
}).then(result => {
13+
return result.json();
14+
}).then(data => {
15+
console.log(data);
16+
productElement.parentNode.removeChild(productElement);
17+
})
18+
.catch(err => {
19+
console.log(err);
20+
});
21+
};

routes/admin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ router.post('/edit-product',
5151
isAuth,
5252
adminController.postEditProduct);
5353

54-
router.post('/delete-product', isAuth, adminController.postDeleteProduct);
54+
router.delete('/product/:productId', isAuth, adminController.deleteProduct);
5555

5656
module.exports = router;
5757

58+

views/admin/products.ejs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,9 @@
2828
</div>
2929
<div class="card__actions">
3030
<a href="/admin/edit-product/<%= product._id %>?edit=true" class="btn">Edit</a>
31-
<form action="/admin/delete-product" method="POST">
32-
<input type="hidden" value="<%= product._id %>" name="productId">
33-
<input type="hidden" name="_csrf" value="<%= csrfToken %>">
34-
<button class="btn" type="submit">Delete</button>
35-
</form>
36-
31+
<input type="hidden" value="<%= product._id %>" name="productId">
32+
<input type="hidden" name="_csrf" value="<%= csrfToken %>">
33+
<button class="btn" type="button" onclick="deleteProduct(this)">Delete</button>
3734
</div>
3835
</article>
3936
<% } %>
@@ -42,4 +39,5 @@
4239
<h1>No Products Found!</h1>
4340
<% } %>
4441
</main>
45-
<%- include('../includes/end.ejs') %>
42+
<%- include('../includes/end.ejs') %>
43+
<script src="/js/admin.js"></script>

0 commit comments

Comments
 (0)