Commit 32f811cc authored by Artem's avatar Artem

improve remove item from cart event listener

parent cc0489ae
...@@ -46,7 +46,12 @@ class Cart { ...@@ -46,7 +46,12 @@ 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() {
this.bindedRemoveItemFromCart = removeItemFromCart.bind(this);
document.querySelector('.js-cart').addEventListener('click', this.bindedRemoveItemFromCart);
function removeItemFromCart(e) {
if (!e.target.classList.contains('js-remove-btn')) return; if (!e.target.classList.contains('js-remove-btn')) return;
const actionUrl = e.target.dataset.action; const actionUrl = e.target.dataset.action;
const productId = e.target.dataset.productId; const productId = e.target.dataset.productId;
...@@ -60,9 +65,9 @@ class Cart { ...@@ -60,9 +65,9 @@ class Cart {
if (redirectLinkWhenCartIsEmpty) { if (redirectLinkWhenCartIsEmpty) {
window.location.replace(redirectLinkWhenCartIsEmpty); window.location.replace(redirectLinkWhenCartIsEmpty);
} else { } else {
document.querySelector('.js-cart').removeEventListener('click', this.removeItemFromCart); document.querySelector('.js-cart').removeEventListener('click', this.bindedRemoveItemFromCart);
cart.show(); this.show();
toastr.success('Product deleted'); toastr.success('Product deleted');
} }
...@@ -72,6 +77,7 @@ class Cart { ...@@ -72,6 +77,7 @@ class Cart {
toastr.error('Error, something went wrong'); toastr.error('Error, something went wrong');
}); });
} }
}
setUpdateItemQtyEventListener() { setUpdateItemQtyEventListener() {
document.querySelectorAll('.js-qty-input').forEach(changeQtyInput => { document.querySelectorAll('.js-qty-input').forEach(changeQtyInput => {
...@@ -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