Skip to content

Commit

Permalink
Complete create order logic for the auto order create component
Browse files Browse the repository at this point in the history
  • Loading branch information
Raccoon254 committed Jun 10, 2024
1 parent f43d1df commit f02bbdf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 30 deletions.
43 changes: 39 additions & 4 deletions app/Livewire/AutoOrderCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,51 @@

namespace App\Livewire;

use App\Models\Order;
use Illuminate\View\View;
use Livewire\Component;

class AutoOrderCreate extends Component
{
public function render(): View
public $title;
public $description;
public $totalPrice;

protected $rules = [
'title' => 'required',
'description' => 'required',
'totalPrice' => 'required|numeric',
];

public function mount()
{
$orderData = session('orderData');
//set the user_id to the authenticated user
$orderData['user_id'] = auth()->id();
return view('livewire.auto-order-create', ['orderData' => $orderData]);
$this->title = $orderData['title'] ?? '';
$this->description = $orderData['description'] ?? '';
$this->totalPrice = $orderData['total_price'] ?? 0;
}

public function createOrder()
{
$this->validate();

$orderData = [
'user_id' => auth()->id(),
'title' => $this->title,
'description' => $this->description,
'total_price' => $this->totalPrice,
'status' => 'pending',
];

Order::create($orderData);

session()->forget('orderData');

return redirect()->route('orders.index')->with('success', 'Order created successfully.');
}

public function render(): View
{
return view('livewire.auto-order-create');
}
}
49 changes: 23 additions & 26 deletions resources/views/livewire/auto-order-create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,33 @@
<div class="rounded-lg relative p-4 w-full bg-white">
<h1 class="text-3xl font-bold mb-6">Create Order</h1>

<form method="POST" action="">
@csrf
<div>
<form wire:submit.prevent="createOrder">
@csrf

<div class="mb-4">
<label for="title" class="block text-gray-700 font-bold mb-2">Title</label>
<x-text-input type="text" name="title" id="title" class="w-full"
value="{{ $orderData['title'] ?? '' }}"/>
</div>
<div class="mb-4">
<label for="title" class="block text-gray-700 font-bold mb-2">Title</label>
<x-text-input type="text" wire:model="title" id="title" class="w-full" />
@error('title') <span class="text-red-500">{{ $message }}</span> @enderror
</div>

<div class="mb-4">
<label for="description" class="block text-gray-700 font-bold mb-2">Description</label>
<textarea name="description" id="description"
class="border border-gray-300 rounded-lg w-full"
rows="3">{{ $orderData['description'] ?? '' }}</textarea>
</div>
<div class="mb-4">
<label for="description" class="block text-gray-700 font-bold mb-2">Description</label>
<textarea wire:model="description" id="description" class="border border-gray-300 rounded-lg w-full" rows="3"></textarea>
@error('description') <span class="text-red-500">{{ $message }}</span> @enderror
</div>

@php
$totalPrice = $orderData['total_price'] ?? 0;
@endphp
<div class="mb-4">
<label for="total_price" class="block text-gray-700 font-bold mb-2">Total Price</label>
<x-text-input type="number" name="total_price" id="total_price" class="w-full"
value="{{ $totalPrice }}" readonly/>
</div>
<div class="mb-4">
<label for="total_price" class="block text-gray-700 font-bold mb-2">Total Price</label>
<x-text-input type="number" wire:model="totalPrice" id="total_price" class="w-full" readonly />
@error('totalPrice') <span class="text-red-500">{{ $message }}</span> @enderror
</div>

<button type="submit"
class="custom-btn">
Create Order
</button>
</form>
<button type="submit" class="custom-btn">
Create Order
</button>
</form>
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit f02bbdf

Please sign in to comment.