movie-night-nuevo/app/Livewire/MovieList.php

81 lines
1.8 KiB
PHP
Raw Normal View History

2025-12-12 23:07:04 -06:00
<?php
namespace App\Livewire;
use App\Livewire\Forms\MovieListForm;
2025-12-13 19:33:52 -06:00
use App\Livewire\Forms\SettingsForm;
2025-12-12 23:07:04 -06:00
use App\Models\MovieList as MovieListModel;
2025-12-26 13:07:15 -06:00
use Livewire\Attributes\Locked;
2025-12-12 23:07:04 -06:00
use Livewire\Component;
class MovieList extends Component
{
2025-12-26 13:07:15 -06:00
#[locked]
2025-12-12 23:07:04 -06:00
public int $id;
public MovieListModel $list;
public $movies = [];
2025-12-13 19:33:52 -06:00
public string $filterText = "";
public $filteredMovies = [];
2025-12-12 23:07:04 -06:00
public MovieListForm $form;
2025-12-13 19:33:52 -06:00
public SettingsForm $settingsForm;
public bool $showSettings = false;
protected $listeners = [
'movie-added' => 'getList',
'list-updated' => 'getList'
];
2025-12-12 23:07:04 -06:00
public function mount($id): void
{
$this->id = $id;
$this->getList();
}
public function getList()
{
2025-12-30 21:15:42 -06:00
$list = MovieListModel::with('movies:id,poster')
2025-12-12 23:07:04 -06:00
->find($this->id);
if ($list) {
$this->list = $list;
2025-12-13 19:33:52 -06:00
$this->filteredMovies = $list->movies;
$this->settingsForm->setList($list);
2025-12-12 23:07:04 -06:00
} else {
abort(404);
}
}
2025-12-30 21:15:42 -06:00
public function deleteList(): void
{
$this->list->delete();
$this->redirectRoute('lists');
}
2025-12-13 19:33:52 -06:00
public function filterMovies(): void
2025-12-12 23:07:04 -06:00
{
2025-12-26 13:07:15 -06:00
$this->filteredMovies = collect($this->list->movies)
2025-12-13 19:33:52 -06:00
->filter(fn($movie) => stripos($movie->title, $this->filterText) !== false);
}
2025-12-12 23:07:04 -06:00
2025-12-13 19:33:52 -06:00
public function toggleSettings(): void
{
$this->showSettings = !$this->showSettings;
}
2025-12-12 23:07:04 -06:00
2025-12-13 19:33:52 -06:00
public function saveSettings(): void
{
$this->settingsForm->save();
2025-12-12 23:07:04 -06:00
$this->getList();
2025-12-13 19:33:52 -06:00
}
2025-12-30 21:15:42 -06:00
public function updatedSettingsFormIsPublic(): void
2025-12-13 19:33:52 -06:00
{
$this->settingsForm->save();
2025-12-12 23:07:04 -06:00
}
public function render()
{
return view('livewire.movie-list');
}
}