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 { ...@@ -58,36 +58,12 @@ class Cart {
this.loaderElem.classList.toggle('loader-show'); 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) { updateItemQty(event) {
if (!event.target.classList.contains('js-qty-input')) { if (!event.target.classList.contains('js-qty-input')) {
return; return;
} }
const qtyInputElem = event.target; const qtyInputElem = event.target;
const changedQty = qtyInputElem.value; const changedQty = qtyInputElem.value;
const action = this.updateItemUrl;
const productId = qtyInputElem.dataset.productId; const productId = qtyInputElem.dataset.productId;
if (!changedQty || (changedQty.indexOf('e') + 1) || changedQty === '0') { if (!changedQty || (changedQty.indexOf('e') + 1) || changedQty === '0') {
...@@ -97,21 +73,22 @@ class Cart { ...@@ -97,21 +73,22 @@ class Cart {
this.toggleLoager(); this.toggleLoager();
const onsuccess = (response) => { this.sendUpdateItemQtyRequest({ productId, changedQty })
.then( (response) => {
this.show(); this.show();
toastr.success('Quantity updated'); toastr.success('Quantity updated');
}; })
.catch( (error) => {
const onerror = (error) => {
console.log(error); console.log(error);
this.toggleLoager(); this.toggleLoager();
toastr.error('Error, something went wrong'); toastr.error('Error, something went wrong');
} });
};
this.sendRequest('post', action, { productId, changedQty }, onsuccess, onerror); sendUpdateItemQtyRequest(data) {
return axios.post(this.updateItemUrl, data);
} }
removeItemFromCart(event) { removeItemFromCart(event) {
if (!event.target.classList.contains('js-remove-btn')) { if (!event.target.classList.contains('js-remove-btn')) {
return; return;
...@@ -121,18 +98,20 @@ class Cart { ...@@ -121,18 +98,20 @@ class Cart {
this.toggleLoager(); this.toggleLoager();
const onsuccess = (response) => { this.sendRemoveItemRequest({ productId })
.then( (response) => {
this.show(); this.show();
toastr.success('Product deleted'); toastr.success('Product deleted');
}; })
.catch( (error) => {
const onerror = (error) => {
console.log(error); console.log(error);
this.toggleLoager(); this.toggleLoager();
toastr.error('Error, something went wrong'); toastr.error('Error, something went wrong');
});
} }
this.sendRequest('post', action, { productId }, onsuccess, onerror); sendRemoveItemRequest(data) {
return axios.post(this.removeItemUrl, data);
} }
events() { events() {
...@@ -147,7 +126,8 @@ class Cart { ...@@ -147,7 +126,8 @@ class Cart {
this.loaderElem.classList.toggle('loader-show'); this.loaderElem.classList.toggle('loader-show');
} }
const onsuccess = (response) => { this.getCurrentProducts()
.then( (response) => {
const redirectLink = response.data.redirectLink; const redirectLink = response.data.redirectLink;
if (redirectLink) { if (redirectLink) {
window.location.replace(redirectLink); window.location.replace(redirectLink);
...@@ -164,15 +144,16 @@ class Cart { ...@@ -164,15 +144,16 @@ class Cart {
if (!this.hasEvents) { if (!this.hasEvents) {
this.events(); this.events();
} }
}; })
.catch( (error) => {
const onerror = (error) => {
console.log(error); console.log(error);
this.toggleLoager(); this.toggleLoager();
toastr.error('Error, something went wrong'); 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