Commit 15453a4d authored by Artem's avatar Artem

last change for cart js for today, I go to sleep

parent b6aa3bfc
......@@ -58,36 +58,12 @@ class Cart {
this.loaderElem.classList.toggle('loader-show');
}
sendRequest(method, action, data, onsuccess, onerror) {
let request;
if (method === 'post') {
request = axios.post;
} else if (method === 'get') {
request = axios.get;
} else {
try {
throw new Error('Expected get or post request');
} catch (e) {
console.log(e.name + ': ' + e.message);
}
}
request(action, data)
.then( (response) => {
onsuccess(response);
})
.catch( (error) => {
onerror(error);
});
}
updateItemQty(event) {
if (!event.target.classList.contains('js-qty-input')) {
return;
}
const qtyInputElem = event.target;
const changedQty = qtyInputElem.value;
const action = this.updateItemUrl;
const productId = qtyInputElem.dataset.productId;
if (!changedQty || (changedQty.indexOf('e') + 1) || changedQty === '0') {
......@@ -97,21 +73,22 @@ class Cart {
this.toggleLoager();
const onsuccess = (response) => {
this.show();
toastr.success('Quantity updated');
};
const onerror = (error) => {
console.log(error);
this.toggleLoager();
toastr.error('Error, something went wrong');
}
this.sendUpdateItemQtyRequest({ productId, changedQty })
.then( (response) => {
this.show();
toastr.success('Quantity updated');
})
.catch( (error) => {
console.log(error);
this.toggleLoager();
toastr.error('Error, something went wrong');
});
};
this.sendRequest('post', action, { productId, changedQty }, onsuccess, onerror);
sendUpdateItemQtyRequest(data) {
return axios.post(this.updateItemUrl, data);
}
removeItemFromCart(event) {
if (!event.target.classList.contains('js-remove-btn')) {
return;
......@@ -121,18 +98,20 @@ class Cart {
this.toggleLoager();
const onsuccess = (response) => {
this.show();
toastr.success('Product deleted');
};
const onerror = (error) => {
console.log(error);
this.toggleLoager();
toastr.error('Error, something went wrong');
}
this.sendRemoveItemRequest({ productId })
.then( (response) => {
this.show();
toastr.success('Product deleted');
})
.catch( (error) => {
console.log(error);
this.toggleLoager();
toastr.error('Error, something went wrong');
});
}
this.sendRequest('post', action, { productId }, onsuccess, onerror);
sendRemoveItemRequest(data) {
return axios.post(this.removeItemUrl, data);
}
events() {
......@@ -147,32 +126,34 @@ class Cart {
this.loaderElem.classList.toggle('loader-show');
}
const onsuccess = (response) => {
const redirectLink = response.data.redirectLink;
if (redirectLink) {
window.location.replace(redirectLink);
return;
}
this.products = response.data.products;
this.positionsAmount = response.data.positionsAmount;
this.totalPrice = response.data.totalPrice;
this.showProducts();
this.toggleLoager();
if (!this.hasEvents) {
this.events();
}
};
const onerror = (error) => {
console.log(error);
this.toggleLoager();
toastr.error('Error, something went wrong');
}
this.getCurrentProducts()
.then( (response) => {
const redirectLink = response.data.redirectLink;
if (redirectLink) {
window.location.replace(redirectLink);
return;
}
this.products = response.data.products;
this.positionsAmount = response.data.positionsAmount;
this.totalPrice = response.data.totalPrice;
this.showProducts();
this.toggleLoager();
if (!this.hasEvents) {
this.events();
}
})
.catch( (error) => {
console.log(error);
this.toggleLoager();
toastr.error('Error, something went wrong');
});
}
this.sendRequest('get', this.getItemsUrl, null, onsuccess, onerror);
getCurrentProducts() {
return axios.get(this.getItemsUrl);
}
}
......
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