Commit 32f811cc authored by Artem's avatar Artem

improve remove item from cart event listener

parent cc0489ae
......@@ -46,31 +46,37 @@ class Cart {
document.querySelector('.js-total-cart-price').textContent = 'Total € ' + this.totalPrice;
}
removeItemFromCart(e) {
if (!e.target.classList.contains('js-remove-btn')) return;
const actionUrl = e.target.dataset.action;
const productId = e.target.dataset.productId;
setRemoveItemFromCartEventListener() {
this.bindedRemoveItemFromCart = removeItemFromCart.bind(this);
axios.post(actionUrl, {
productId: productId,
})
.then( (response) => {
const redirectLinkWhenCartIsEmpty = response.data;
document.querySelector('.js-cart').addEventListener('click', this.bindedRemoveItemFromCart);
if (redirectLinkWhenCartIsEmpty) {
window.location.replace(redirectLinkWhenCartIsEmpty);
} else {
document.querySelector('.js-cart').removeEventListener('click', this.removeItemFromCart);
function removeItemFromCart(e) {
if (!e.target.classList.contains('js-remove-btn')) return;
const actionUrl = e.target.dataset.action;
const productId = e.target.dataset.productId;
cart.show();
toastr.success('Product deleted');
}
axios.post(actionUrl, {
productId: productId,
})
.catch(function (error) {
console.log(error);
toastr.error('Error, something went wrong');
});
.then( (response) => {
const redirectLinkWhenCartIsEmpty = response.data;
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() {
......@@ -81,7 +87,6 @@ class Cart {
return;
}
// this.show();
isChanged = true;
const actionUrl = this.updateItemUrl;
const productId = event.target.dataset.productId;
......@@ -97,7 +102,10 @@ class Cart {
if (redirectLinkWhenCartIsEmpty) {
window.location.replace(redirectLinkWhenCartIsEmpty);
} else {
cart.show();
if (this.bindedRemoveItemFromCart) {
document.querySelector('.js-cart').removeEventListener('click', this.bindedRemoveItemFromCart);
}
this.show();
toastr.success('Quantity updated');
}
})
......@@ -109,22 +117,18 @@ class Cart {
isChanged = false;
}, 1000);
});
});
}
events() {
document.querySelector('.js-cart').addEventListener('click', this.removeItemFromCart );
this.setRemoveItemFromCartEventListener();
this.setUpdateItemQtyEventListener();
}
show() {
axios.get(this.getItemsUrl)
.then( response => {
// this.products = response.data.cart.items;
this.products = response.data.products;
this.positionsAmount = response.data.cart.total_qty;
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