Commit 32f811cc authored by Artem's avatar Artem

improve remove item from cart event listener

parent cc0489ae
...@@ -46,31 +46,37 @@ class Cart { ...@@ -46,31 +46,37 @@ class Cart {
document.querySelector('.js-total-cart-price').textContent = 'Total € ' + this.totalPrice; document.querySelector('.js-total-cart-price').textContent = 'Total € ' + this.totalPrice;
} }
removeItemFromCart(e) { setRemoveItemFromCartEventListener() {
if (!e.target.classList.contains('js-remove-btn')) return; this.bindedRemoveItemFromCart = removeItemFromCart.bind(this);
const actionUrl = e.target.dataset.action;
const productId = e.target.dataset.productId;
axios.post(actionUrl, { document.querySelector('.js-cart').addEventListener('click', this.bindedRemoveItemFromCart);
productId: productId,
})
.then( (response) => {
const redirectLinkWhenCartIsEmpty = response.data;
if (redirectLinkWhenCartIsEmpty) { function removeItemFromCart(e) {
window.location.replace(redirectLinkWhenCartIsEmpty); if (!e.target.classList.contains('js-remove-btn')) return;
} else { const actionUrl = e.target.dataset.action;
document.querySelector('.js-cart').removeEventListener('click', this.removeItemFromCart); const productId = e.target.dataset.productId;
cart.show(); axios.post(actionUrl, {
productId: productId,
toastr.success('Product deleted');
}
}) })
.catch(function (error) { .then( (response) => {
console.log(error); const redirectLinkWhenCartIsEmpty = response.data;
toastr.error('Error, something went wrong');
}); if (redirectLinkWhenCartIsEmpty) {
window.location.replace(redirectLinkWhenCartIsEmpty);
} else {
document.querySelector('.js-cart').removeEventListener('click', this.bindedRemoveItemFromCart);
this.show();
toastr.success('Product deleted');
}
})
.catch(function (error) {
console.log(error);
toastr.error('Error, something went wrong');
});
}
} }
setUpdateItemQtyEventListener() { setUpdateItemQtyEventListener() {
...@@ -81,7 +87,6 @@ class Cart { ...@@ -81,7 +87,6 @@ class Cart {
return; return;
} }
// this.show();
isChanged = true; isChanged = true;
const actionUrl = this.updateItemUrl; const actionUrl = this.updateItemUrl;
const productId = event.target.dataset.productId; const productId = event.target.dataset.productId;
...@@ -97,7 +102,10 @@ class Cart { ...@@ -97,7 +102,10 @@ class Cart {
if (redirectLinkWhenCartIsEmpty) { if (redirectLinkWhenCartIsEmpty) {
window.location.replace(redirectLinkWhenCartIsEmpty); window.location.replace(redirectLinkWhenCartIsEmpty);
} else { } else {
cart.show(); if (this.bindedRemoveItemFromCart) {
document.querySelector('.js-cart').removeEventListener('click', this.bindedRemoveItemFromCart);
}
this.show();
toastr.success('Quantity updated'); toastr.success('Quantity updated');
} }
}) })
...@@ -109,22 +117,18 @@ class Cart { ...@@ -109,22 +117,18 @@ class Cart {
isChanged = false; isChanged = false;
}, 1000); }, 1000);
}); });
}); });
} }
events() { events() {
document.querySelector('.js-cart').addEventListener('click', this.removeItemFromCart ); this.setRemoveItemFromCartEventListener();
this.setUpdateItemQtyEventListener(); this.setUpdateItemQtyEventListener();
} }
show() { show() {
axios.get(this.getItemsUrl) axios.get(this.getItemsUrl)
.then( response => { .then( response => {
// this.products = response.data.cart.items;
this.products = response.data.products; this.products = response.data.products;
this.positionsAmount = response.data.cart.total_qty; this.positionsAmount = response.data.cart.total_qty;
this.totalPrice = response.data.cart.total_price; this.totalPrice = response.data.cart.total_price;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment