dishplanner/app/Livewire/Dishes/DishesList.php

137 lines
3.2 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Livewire\Dishes;
use App\Models\Dish;
use App\Models\User;
use Livewire\Component;
use Livewire\WithPagination;
class DishesList extends Component
{
use WithPagination;
public $showCreateModal = false;
2026-08-17 13:30:49 +02:00
public $showEditModal = false;
2026-08-17 13:30:49 +02:00
public $showDeleteModal = false;
2026-08-17 13:30:49 +02:00
public $editingDish = null;
2026-08-17 13:30:49 +02:00
public $deletingDish = null;
2026-08-17 13:30:49 +02:00
// Form fields
public $name = '';
2026-08-17 13:30:49 +02:00
public $selectedUsers = [];
2026-08-17 13:30:49 +02:00
protected $rules = [
'name' => 'required|string|max:255',
'selectedUsers' => 'array',
];
public function render()
{
$dishes = Dish::with('users')
->orderBy('name')
->paginate(10);
2026-08-17 13:30:49 +02:00
$users = User::where('planner_id', auth()->id())
->orderBy('name')
->get();
2026-08-17 13:30:49 +02:00
return view('livewire.dishes.dishes-list', [
'dishes' => $dishes,
2026-08-17 13:30:49 +02:00
'users' => $users,
]);
}
public function create()
{
$this->reset(['name', 'selectedUsers']);
$this->resetValidation();
$this->showCreateModal = true;
}
public function store()
{
$this->validate();
$dish = Dish::create([
'name' => $this->name,
'planner_id' => auth()->id(),
]);
// Attach selected users
2026-08-17 13:30:49 +02:00
if (! empty($this->selectedUsers)) {
$dish->users()->attach($this->selectedUsers);
}
$this->showCreateModal = false;
$this->reset(['name', 'selectedUsers']);
2026-08-17 13:30:49 +02:00
session()->flash('success', 'Dish created successfully.');
}
public function edit(Dish $dish)
{
$this->editingDish = $dish;
$this->name = $dish->name;
$this->selectedUsers = $dish->users->pluck('id')->toArray();
$this->resetValidation();
$this->showEditModal = true;
}
public function update()
{
$this->validate();
$this->editingDish->update([
'name' => $this->name,
]);
2026-08-17 13:30:49 +02:00
// Sync users
$this->editingDish->users()->sync($this->selectedUsers);
$this->showEditModal = false;
$this->reset(['name', 'selectedUsers', 'editingDish']);
2026-08-17 13:30:49 +02:00
session()->flash('success', 'Dish updated successfully.');
}
public function confirmDelete(Dish $dish)
{
$this->deletingDish = $dish;
$this->showDeleteModal = true;
}
public function delete()
{
$this->deletingDish->users()->detach();
$this->deletingDish->delete();
$this->showDeleteModal = false;
$this->deletingDish = null;
2026-08-17 13:30:49 +02:00
session()->flash('success', 'Dish deleted successfully.');
}
public function cancel()
{
$this->showCreateModal = false;
$this->showEditModal = false;
$this->showDeleteModal = false;
$this->reset(['name', 'selectedUsers', 'editingDish', 'deletingDish']);
}
2026-01-04 15:54:31 +01:00
public function toggleAllUsers(): void
{
$users = User::where('planner_id', auth()->id())->get();
if (count($this->selectedUsers) === $users->count()) {
$this->selectedUsers = [];
} else {
2026-08-17 13:30:49 +02:00
$this->selectedUsers = $users->pluck('id')->map(fn ($id) => (string) $id)->toArray();
2026-01-04 15:54:31 +01:00
}
}
2026-08-17 13:30:49 +02:00
}