61 lines
1.3 KiB
PHP
61 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use App\Livewire\Forms\MovieListForm;
|
|
use App\Models\MovieList;
|
|
use Exception;
|
|
use Livewire\Component;
|
|
use function Laravel\Prompts\select;
|
|
|
|
class MovieLists extends Component
|
|
{
|
|
public MovieListForm $form;
|
|
public $lists;
|
|
public $sharedLists;
|
|
|
|
public function mount()
|
|
{
|
|
$this->lists = collect();
|
|
$this->sharedLists = collect();
|
|
|
|
$this->getLists();
|
|
}
|
|
|
|
public function getLists()
|
|
{
|
|
$user = auth()->user();
|
|
if ($user) {
|
|
$this->lists = MovieList::where('user_id', $user->id)
|
|
->select("name", "id", "is_public")
|
|
->get();
|
|
} else {
|
|
$this->redirectRoute('login');
|
|
}
|
|
}
|
|
|
|
public function addList(): void
|
|
{
|
|
if (!auth()->check()) {
|
|
$this->redirectRoute('login');
|
|
return;
|
|
}
|
|
|
|
$user = auth()->user();
|
|
$validated = $this->form->validate();
|
|
|
|
try {
|
|
MovieList::create(array_merge($validated, ["user_id" => $user->id]));
|
|
} catch (Exception $e) {
|
|
$this->addError("new-list", "Could not create movie list");
|
|
}
|
|
|
|
$this->getLists();
|
|
$this->form->reset();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.lists');
|
|
}
|
|
}
|