Commit e323d301 authored by Artem's avatar Artem

improve cart methods

parent 1495ab98
......@@ -60,14 +60,14 @@ class CartController extends Controller
public function getItems()
{
if (!Session::has('cart')) {
return ['redirectLinkWhenEmpty' => route('homepage')];
return ['redirectLink' => route('homepage')];
}
$cart = Session::get('cart');
if (!$cart->total_qty) {
Session::forget('cart');
return ['redirectLinkWhenEmpty' => route('homepage')];
return ['redirectLink' => route('homepage')];
}
$cart->updateCurrentItems();
......@@ -79,11 +79,9 @@ class CartController extends Controller
}
$response = [
'cart' => $cart,
'positionsAmount' => $cart->total_qty,
'totalPrice' => $cart->total_price,
'products' => $cart_products,
'update_item_url' => route('cart.updateQtyInCart'),
'remove_item_url' => route('cart.removeFromCart'),
'item_url' => '/product/'
];
return json_encode($response);
......
......@@ -9,9 +9,9 @@ class Cart {
this.products;
this.positionsAmount;
this.totalPrice;
this.updateItemUrl;
this.removeItemUrl;
this.itemUrl;
this.updateItemUrl = cartElem.dataset.updateItemUrl;
this.removeItemUrl = cartElem.dataset.removeItemUrl;
this.itemUrl = cartElem.dataset.itemUrl;
this.hasEvents;
this.updateItemQty = this.updateItemQty.bind(this);
this.removeItemFromCart = this.removeItemFromCart.bind(this);
......@@ -68,7 +68,7 @@ class Cart {
return;
}
this.loaderElem.classList.toggle('loader-show');
this.toggleLoager();
axios.post(actionUrl, {
productId,
......@@ -80,7 +80,7 @@ class Cart {
})
.catch( (error) => {
console.log(error);
this.loaderElem.classList.toggle('loader-show');
this.toggleLoager();
toastr.error('Error, something went wrong');
});
}
......@@ -89,12 +89,12 @@ class Cart {
if (!event.target.classList.contains('js-remove-btn')) {
return;
}
const actionUrl = event.target.dataset.action;
const productId = event.target.dataset.productId;
this.loaderElem.classList.toggle('loader-show');
const { action, productId } = event.target.dataset;
axios.post(actionUrl, {
this.toggleLoager();
axios.post(action, {
productId,
})
.then( (response) => {
......@@ -103,7 +103,7 @@ class Cart {
})
.catch( (error) => {
console.log(error);
this.loaderElem.classList.toggle('loader-show');
this.toggleLoager();
toastr.error('Error, something went wrong');
});
}
......@@ -114,6 +114,10 @@ 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');
......@@ -121,21 +125,18 @@ class Cart {
axios.get(this.getItemsUrl)
.then( (response) => {
const redirectLinkWhenEmpty = response.data.redirectLinkWhenEmpty;
if (redirectLinkWhenEmpty) {
window.location.replace(redirectLinkWhenEmpty);
const redirectLink = response.data.redirectLink;
if (redirectLink) {
window.location.replace(redirectLink);
return;
}
this.products = response.data.products;
this.positionsAmount = response.data.cart.total_qty;
this.totalPrice = response.data.cart.total_price;
this.updateItemUrl = response.data.update_item_url;
this.removeItemUrl = response.data.remove_item_url;
this.itemUrl = response.data.item_url;
this.positionsAmount = response.data.positionsAmount;
this.totalPrice = response.data.totalPrice;
this.showProducts();
this.loaderElem.classList.toggle('loader-show');
this.toggleLoager();
if (!this.hasEvents) {
this.events();
......@@ -143,7 +144,7 @@ class Cart {
})
.catch( (error) => {
console.log(error);
this.loaderElem.classList.toggle('loader-show');
this.toggleLoager();
toastr.error('Error, something went wrong');
});
}
......
......@@ -61,7 +61,12 @@
</tr>
</thead>
<tbody class="js-cart" data-get-items-url="{{ route('cart.getItems') }}">
<tbody class="js-cart"
data-get-items-url="{{ route('cart.getItems') }}"
data-update-item-url="{{ route('cart.updateQtyInCart') }}"
data-remove-item-url="{{ route('cart.removeFromCart') }}"
data-item-url="{{ route('productPath') }}"
>
{{-- @foreach($products as $product)
<tr>
<td>
......
......@@ -18,7 +18,7 @@ Auth::routes();
Route::get('/', 'ProductController@index')->name('homepage');
Route::prefix('product')->group(function() {
Route::post('/', 'ProductController@store');
Route::post('/', 'ProductController@store')->name('productPath');
Route::get('/create', 'ProductController@create');
Route::get('/{product}', 'ProductController@show')->name('product.show');
Route::get('/{product}/edit', 'ProductController@edit')->name('product.edit');
......
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