Add type filter to download search page

This commit is contained in:
2025-10-15 18:50:49 +02:00
parent 444feac1e0
commit 6c44d83e6b
2 changed files with 118 additions and 14 deletions

View File

@@ -5,15 +5,37 @@ namespace App\Livewire;
use App\Models\Downloads;
use Livewire\Component;
use Livewire\WithPagination;
use Livewire\Attributes\Url;
class DownloadsSearch extends Component
{
use WithPagination;
#[Url(history: true)]
public $fileSearch;
public $order = 'created_at_desc';
public $options = [
'FHD' => true,
'FHD 48fps' => true,
];
public $isOpen = false;
// To toggle individual option selection
public function toggleOption($option)
{
$this->options[$option] = !$this->options[$option];
$this->resetPage();
}
// To toggle dropdown visibility
public function toggleDropdown()
{
$this->isOpen = !$this->isOpen;
}
protected $queryString = [
'fileSearch' => ['except' => '', 'as' => 'fS'],
'order' => ['except' => '', 'as' => 'order'],
@@ -24,6 +46,29 @@ class DownloadsSearch extends Component
$this->resetPage();
}
// Map the selected options to database types
private function getSelectedTypes()
{
$types = [];
// Map the options to their corresponding database values
foreach ($this->options as $label => $selected) {
if ($selected) {
if ($label === 'FHD') {
$types[] = 'FHD';
} elseif ($label === 'FHD 48fps') {
$types[] = 'FHDi';
} elseif ($label === 'UHD' && auth()->user()->is_patreon) {
$types[] = 'UHD';
} elseif ($label === 'UHD 48fps' && auth()->user()->is_patreon) {
$types[] = 'UHDi';
}
}
}
return $types;
}
public function clicked($downloadId)
{
$download = Downloads::find($downloadId);
@@ -36,6 +81,17 @@ class DownloadsSearch extends Component
cache()->forget("episode_{$download->episode->id}_download_{$download->type}");
}
public function mount()
{
if (!auth()->user()->is_patreon) {
return;
}
// Add patreon options
$this->options['UHD'] = true;
$this->options['UHD 48fps'] = true;
}
public function render()
{
$orderby = 'created_at';
@@ -72,7 +128,7 @@ class DownloadsSearch extends Component
}
$downloads = Downloads::when($this->fileSearch != '', fn ($query) => $query->where('url', 'like', '%'.$this->fileSearch.'%'))
->when(!auth()->user()->is_patreon, fn ($query) => $query->whereIn('type', ['FHD', 'FHDi']))
->whereIn('type', $this->getSelectedTypes())
->whereNotNull('size')
->orderBy($orderby, $orderdirection)
->paginate(20);