Commit c789a6b1 authored by Artem's avatar Artem

improve cart class in cart.js

parent fdb9a719
...@@ -44,7 +44,7 @@ class CartController extends Controller ...@@ -44,7 +44,7 @@ class CartController extends Controller
public function removeFromCart(Request $request) public function removeFromCart(Request $request)
{ {
if (!Session::has('cart')) { if (!Session::has('cart')) {
return ['redirectLinkWhenEmpty' => route('homepage')]; return;
} }
$cart = Session::get('cart'); $cart = Session::get('cart');
...@@ -59,6 +59,10 @@ class CartController extends Controller ...@@ -59,6 +59,10 @@ class CartController extends Controller
public function getItems() public function getItems()
{ {
if (!Session::has('cart')) {
return ['redirectLinkWhenEmpty' => route('homepage')];
}
$cart = Session::get('cart'); $cart = Session::get('cart');
if (!$cart->total_qty) { if (!$cart->total_qty) {
......
class Cart { class Cart {
constructor(cartElem) { constructor() {
this.cartHtmlElem = cartElem; const cartElem = document.querySelector('.js-cart');
this.cartElem = cartElem;
this.loaderElem = document.querySelector('.loader');
this.amountInCartElem = document.querySelector('.amount-in-cart');
this.totalPriceElem = document.querySelector('.js-total-cart-price');
this.getItemsUrl = cartElem.dataset.getItemsUrl; this.getItemsUrl = cartElem.dataset.getItemsUrl;
this.products;
this.positionsAmount;
this.totalPrice;
this.updateItemUrl;
this.removeItemUrl;
this.itemUrl;
this.hasEvents;
this.updateItemQty = this.updateItemQty.bind(this);
this.removeItemFromCart = this.removeItemFromCart.bind(this);
} }
generateProductRow(product) { generateProductRowHTML(product) {
return `<tr> return `<tr>
<td> <td>
<div class="row"> <div class="row">
...@@ -34,45 +47,40 @@ class Cart { ...@@ -34,45 +47,40 @@ class Cart {
} }
showProducts() { showProducts() {
let productsList = ''; let productsList = this.products.reduce((acc, item) => acc += this.generateProductRowHTML(item), '');
for (const item in this.products) { this.cartElem.innerHTML = productsList;
const product = this.products[item]; this.amountInCartElem.textContent = this.positionsAmount;
productsList += this.generateProductRow(product); this.totalPriceElem.textContent = 'Total € ' + this.totalPrice;
}
this.cartHtmlElem.innerHTML = productsList;
document.querySelector('.amount-in-cart').textContent = this.positionsAmount;
document.querySelector('.js-total-cart-price').textContent = 'Total € ' + this.totalPrice;
} }
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 changedQty = qtyInputElem.value;
const actionUrl = this.updateItemUrl;
const productId = qtyInputElem.dataset.productId;
const changedQty = event.target.value;
if (!changedQty || (changedQty.indexOf('e') + 1) || changedQty === '0') { if (!changedQty || (changedQty.indexOf('e') + 1) || changedQty === '0') {
event.target.value = event.target.defaultValue; event.target.value = qtyInputElem.defaultValue;
return; return;
} }
const actionUrl = this.updateItemUrl; this.loaderElem.classList.toggle('loader-show');
const productId = event.target.dataset.productId;
loaderElem.classList.toggle('loader-show');
axios.post(actionUrl, { axios.post(actionUrl, {
productId: productId, productId,
changedQty: changedQty, changedQty,
}) })
.then( (response) => { .then( (response) => {
this.show(); this.show();
toastr.success('Quantity updated'); toastr.success('Quantity updated');
}) })
.catch(function (error) { .catch( (error) => {
console.log(error); console.log(error);
loaderElem.classList.toggle('loader-show'); this.loaderElem.classList.toggle('loader-show');
toastr.error('Error, something went wrong'); toastr.error('Error, something went wrong');
}); });
} }
...@@ -84,40 +92,35 @@ class Cart { ...@@ -84,40 +92,35 @@ class Cart {
const actionUrl = event.target.dataset.action; const actionUrl = event.target.dataset.action;
const productId = event.target.dataset.productId; const productId = event.target.dataset.productId;
loaderElem.classList.toggle('loader-show'); this.loaderElem.classList.toggle('loader-show');
axios.post(actionUrl, { axios.post(actionUrl, {
productId: productId, productId,
}) })
.then( (response) => { .then( (response) => {
const redirectLinkWhenEmpty = response.data.redirectLinkWhenEmpty;
if (redirectLinkWhenEmpty) {
window.location.replace(redirectLinkWhenEmpty);
return;
}
this.show(); this.show();
toastr.success('Product deleted'); toastr.success('Product deleted');
}) })
.catch(function (error) { .catch( (error) => {
console.log(error); console.log(error);
loaderElem.classList.toggle('loader-show'); this.loaderElem.classList.toggle('loader-show');
toastr.error('Error, something went wrong'); toastr.error('Error, something went wrong');
}); });
} }
events() { events() {
this.cartHtmlElem.addEventListener('change', this.updateItemQty.bind(this)); this.cartElem.addEventListener('change', this.updateItemQty);
this.cartHtmlElem.addEventListener('click', this.removeItemFromCart.bind(this)); this.cartElem.addEventListener('click', this.removeItemFromCart);
this.hasEvents = true; this.hasEvents = true;
} }
show() { show() {
if (!loaderElem.classList.contains('loader-show')) { if (!this.loaderElem.classList.contains('loader-show')) {
loaderElem.classList.toggle('loader-show'); this.loaderElem.classList.toggle('loader-show');
} }
axios.get(this.getItemsUrl) axios.get(this.getItemsUrl)
.then( response => { .then( (response) => {
const redirectLinkWhenEmpty = response.data.redirectLinkWhenEmpty; const redirectLinkWhenEmpty = response.data.redirectLinkWhenEmpty;
if (redirectLinkWhenEmpty) { if (redirectLinkWhenEmpty) {
window.location.replace(redirectLinkWhenEmpty); window.location.replace(redirectLinkWhenEmpty);
...@@ -132,22 +135,19 @@ class Cart { ...@@ -132,22 +135,19 @@ class Cart {
this.itemUrl = response.data.item_url; this.itemUrl = response.data.item_url;
this.showProducts(); this.showProducts();
loaderElem.classList.toggle('loader-show'); this.loaderElem.classList.toggle('loader-show');
if (!this.hasEvents) { if (!this.hasEvents) {
this.events(); this.events();
} }
}) })
.catch(function (error) { .catch( (error) => {
console.log(error); console.log(error);
loaderElem.classList.toggle('loader-show'); this.loaderElem.classList.toggle('loader-show');
toastr.error('Error, something went wrong'); toastr.error('Error, something went wrong');
}); });
} }
} }
const loaderElem = document.querySelector('.loader'); const cart = new Cart();
const cartElem = document.querySelector('.js-cart');
const cart = new Cart(cartElem);
cart.show(); 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