Commit b6aa3bfc authored by Artem's avatar Artem

improve cart js, include sendRequest method to cart class

parent e323d301
......@@ -54,13 +54,40 @@ class Cart {
this.totalPriceElem.textContent = 'Total € ' + this.totalPrice;
}
toggleLoager() {
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 actionUrl = this.updateItemUrl;
const action = this.updateItemUrl;
const productId = qtyInputElem.dataset.productId;
if (!changedQty || (changedQty.indexOf('e') + 1) || changedQty === '0') {
......@@ -70,21 +97,21 @@ class Cart {
this.toggleLoager();
axios.post(actionUrl, {
productId,
changedQty,
})
.then( (response) => {
this.show();
toastr.success('Quantity updated');
})
.catch( (error) => {
console.log(error);
this.toggleLoager();
toastr.error('Error, something went wrong');
});
const onsuccess = (response) => {
this.show();
toastr.success('Quantity updated');
};
const onerror = (error) => {
console.log(error);
this.toggleLoager();
toastr.error('Error, something went wrong');
}
this.sendRequest('post', action, { productId, changedQty }, onsuccess, onerror);
}
removeItemFromCart(event) {
if (!event.target.classList.contains('js-remove-btn')) {
return;
......@@ -94,18 +121,18 @@ class Cart {
this.toggleLoager();
axios.post(action, {
productId,
})
.then( (response) => {
this.show();
toastr.success('Product deleted');
})
.catch( (error) => {
console.log(error);
this.toggleLoager();
toastr.error('Error, something went wrong');
});
const onsuccess = (response) => {
this.show();
toastr.success('Product deleted');
};
const onerror = (error) => {
console.log(error);
this.toggleLoager();
toastr.error('Error, something went wrong');
}
this.sendRequest('post', action, { productId }, onsuccess, onerror);
}
events() {
......@@ -114,39 +141,38 @@ class Cart {
this.hasEvents = true;
}
toggleLoager() {
this.loaderElem.classList.toggle('loader-show');
}
show() {
if (!this.loaderElem.classList.contains('loader-show')) {
this.loaderElem.classList.toggle('loader-show');
}
axios.get(this.getItemsUrl)
.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');
});
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.sendRequest('get', this.getItemsUrl, null, onsuccess, onerror);
}
}
......
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