Commit fdb9a719 authored by Artem's avatar Artem

improve event listeners in cart.js

parent efc191ea
const loaderElem = document.querySelector('.loader');
class Cart {
constructor(cartElem) {
this.cartHtmlElem = cartElem;
......@@ -48,90 +46,75 @@ class Cart {
document.querySelector('.js-total-cart-price').textContent = 'Total € ' + this.totalPrice;
}
setRemoveItemFromCartEventListener() {
this.bindedRemoveItemFromCart = removeItemFromCart.bind(this);
updateItemQty(event) {
if (!event.target.classList.contains('js-qty-input')) {
return;
}
document.querySelector('.js-cart').addEventListener('click', this.bindedRemoveItemFromCart);
const changedQty = event.target.value;
if (!changedQty || (changedQty.indexOf('e') + 1) || changedQty === '0') {
event.target.value = event.target.defaultValue;
return;
}
function removeItemFromCart(event) {
if (!event.target.classList.contains('js-remove-btn')) return;
const actionUrl = event.target.dataset.action;
const productId = event.target.dataset.productId;
const actionUrl = this.updateItemUrl;
const productId = event.target.dataset.productId;
loaderElem.classList.toggle('loader-show');
loaderElem.classList.toggle('loader-show');
axios.post(actionUrl, {
productId: productId,
axios.post(actionUrl, {
productId: productId,
changedQty: changedQty,
})
.then( (response) => {
this.show();
toastr.success('Quantity updated');
})
.then( (response) => {
const redirectLinkWhenEmpty = response.data.redirectLinkWhenEmpty;
if (redirectLinkWhenEmpty) {
window.location.replace(redirectLinkWhenEmpty);
return;
}
this.removeEventListeners();
this.show();
loaderElem.classList.toggle('loader-show');
toastr.success('Product deleted');
})
.catch(function (error) {
console.log(error);
loaderElem.classList.toggle('loader-show');
toastr.error('Error, something went wrong');
});
}
.catch(function (error) {
console.log(error);
loaderElem.classList.toggle('loader-show');
toastr.error('Error, something went wrong');
});
}
setUpdateItemQtyEventListener() {
this.bindedUpdateItemQty = bindedUpdateItemQty.bind(this);
document.querySelector('.js-cart').addEventListener('change', this.bindedUpdateItemQty);
function bindedUpdateItemQty(event) {
if (!event.target.classList.contains('js-qty-input')) return;
const changedQty = event.target.value;
if (!changedQty || (changedQty.indexOf('e') + 1) || changedQty === '0') {
event.target.value = event.target.defaultValue;
return;
}
const actionUrl = this.updateItemUrl;
const productId = event.target.dataset.productId;
removeItemFromCart(event) {
if (!event.target.classList.contains('js-remove-btn')) {
return;
}
const actionUrl = event.target.dataset.action;
const productId = event.target.dataset.productId;
loaderElem.classList.toggle('loader-show');
loaderElem.classList.toggle('loader-show');
axios.post(actionUrl, {
productId: productId,
changedQty: event.target.value,
axios.post(actionUrl, {
productId: productId,
})
.then( (response) => {
const redirectLinkWhenEmpty = response.data.redirectLinkWhenEmpty;
if (redirectLinkWhenEmpty) {
window.location.replace(redirectLinkWhenEmpty);
return;
}
this.show();
toastr.success('Product deleted');
})
.then( (response) => {
this.removeEventListeners();
this.show();
loaderElem.classList.toggle('loader-show');
toastr.success('Quantity updated');
})
.catch(function (error) {
console.log(error);
loaderElem.classList.toggle('loader-show');
toastr.error('Error, something went wrong');
});
};
}
removeEventListeners() {
this.cartHtmlElem.removeEventListener('change', this.bindedUpdateItemQty);
this.cartHtmlElem.removeEventListener('click', this.bindedRemoveItemFromCart);
.catch(function (error) {
console.log(error);
loaderElem.classList.toggle('loader-show');
toastr.error('Error, something went wrong');
});
}
events() {
this.setRemoveItemFromCartEventListener();
this.setUpdateItemQtyEventListener();
this.cartHtmlElem.addEventListener('change', this.updateItemQty.bind(this));
this.cartHtmlElem.addEventListener('click', this.removeItemFromCart.bind(this));
this.hasEvents = true;
}
show() {
loaderElem.classList.toggle('loader-show');
if (!loaderElem.classList.contains('loader-show')) {
loaderElem.classList.toggle('loader-show');
}
axios.get(this.getItemsUrl)
.then( response => {
......@@ -150,7 +133,10 @@ class Cart {
this.showProducts();
loaderElem.classList.toggle('loader-show');
this.events();
if (!this.hasEvents) {
this.events();
}
})
.catch(function (error) {
console.log(error);
......@@ -160,7 +146,8 @@ class Cart {
}
}
const cartElem = document.querySelector('.js-cart');
const loaderElem = document.querySelector('.loader');
const cartElem = document.querySelector('.js-cart');
const cart = new Cart(cartElem);
cart.show();
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