Move cart into a separate page

This commit is contained in:
bad 2021-12-08 16:39:56 +01:00
parent 3c6d22573e
commit 09d737dfab
6 changed files with 48 additions and 27 deletions

View file

@ -8,6 +8,7 @@ use Exception;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class CartController extends Controller class CartController extends Controller
{ {
@ -19,8 +20,11 @@ class CartController extends Controller
*/ */
public function show() public function show()
{ {
throw new Exception("UNIMPLEMENTED"); $user = Auth::user();
//return view("product/view", ["product" => $product]); if (!$user) {
throw new AccessDeniedHttpException("Not logged in");
}
return view("cart", ["user" => $user]);
} }
public function addToCart(Product $product) public function addToCart(Product $product)

View file

@ -37,6 +37,11 @@ nav {
justify-content: center; justify-content: center;
} }
.cart-amount {
padding: 0;
margin: 0;
}
main { main {
display: flex; display: flex;

View file

@ -0,0 +1,34 @@
@push('head')
<link rel="stylesheet" href="{{ mix("css/user.css") }}">
@endpush
@extends('layouts.app')
@section('title', "Sklep")
@section('main')
<div>
<h2>Your cart:</h2>
<div>
<div class="cart-items">
@foreach($user->cart as $product)
<div class="cart-item">
<x-product :product="$product" />
<form class="remove-cart-form" action="{{ route("removeFromCart", $product) }}" method="post">
@csrf
<button type="submit"> Remove from cart </button>
</form>
</div>
@endforeach()
</div>
<form action="{{ route("order.create") }}" method="post">
@csrf
@foreach($user->cart as $product)
<input type="hidden" name="products[{{ $loop->index }}]" value="{{ $product->uuid }}">
@endforeach()
<button type="submit"> Order </button>
</form>
</div>
</div>
@endsection()

View file

@ -18,7 +18,8 @@
<div> <div>
@auth @auth
<a href="{{ route("user") }}"> Hello {{ Auth::user()->name }} </a> <a href="{{ route("cart") }}"> <i class="fa fa-shopping-cart"></i> <span class="cart-amount">{{count(Auth::user()->cart) }}</span> </a>
<a href="{{ route("user") }}"> {{ Auth::user()->name }} </a>
@endauth @endauth
@guest @guest
<a href="{{ route("login") }}">Login</a> <a href="{{ route("login") }}">Login</a>

View file

@ -13,28 +13,4 @@
<p> <p>
<a href="/logout"> Logout </a> <a href="/logout"> Logout </a>
</p> </p>
<div>
<h2>Your cart:</h2>
<div>
<div class="cart-items">
@foreach($user->cart as $product)
<div class="cart-item">
<x-product :product="$product" />
<form class="remove-cart-form" action="{{ route("removeFromCart", $product) }}" method="post">
@csrf
<button type="submit"> Remove from cart </button>
</form>
</div>
@endforeach()
</div>
<form action="{{ route("order.create") }}" method="post">
@csrf
@foreach($user->cart as $product)
<input type="hidden" name="products[{{ $loop->index }}]" value="{{ $product->uuid }}">
@endforeach()
<button type="submit"> Order </button>
</form>
</div>
</div>
@endsection() @endsection()

View file

@ -33,6 +33,7 @@ Route::post('/login', [LoginController::class, "authenticate"])->name("login");
Route::post('/register', [LoginController::class, "register"])->name("register"); Route::post('/register', [LoginController::class, "register"])->name("register");
Route::any("/logout", [LoginController::class, "logout"])->name("logout"); Route::any("/logout", [LoginController::class, "logout"])->name("logout");
Route::get("/cart", [CartController::class, "show"])->name("cart")->middleware("auth");
Route::post("/product/{product}/addToCart", [CartController::class, "addToCart"])->name("addToCart")->middleware("auth"); Route::post("/product/{product}/addToCart", [CartController::class, "addToCart"])->name("addToCart")->middleware("auth");
Route::post("/product/{product}/removeFromCart", [CartController::class, "removeFromCart"])->name("removeFromCart")->middleware("auth"); Route::post("/product/{product}/removeFromCart", [CartController::class, "removeFromCart"])->name("removeFromCart")->middleware("auth");