From 75b8be1e1e5dcd3260f9e8c468e37d8618a071ee Mon Sep 17 00:00:00 2001 From: Refringe Date: Mon, 7 Oct 2024 09:53:30 -0600 Subject: [PATCH] Mod Filter Page - Per Page Option - Validate Value This change validates the input of the per page option so that it has to be one of the provided options. If an option does not match a provided option, it's set to the closest available option. For example, manually setting it to 100000 in the URL param will have it automatically set to the largest option, 50, while manually setting it to 1 will change it to 6, the smallest option. Also made some small language adjustments to get everything to fit a little nicer in the mobile views. --- app/Livewire/Mod/Listing.php | 36 +++++++++++++++++-- .../views/livewire/mod/listing.blade.php | 14 ++++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/app/Livewire/Mod/Listing.php b/app/Livewire/Mod/Listing.php index f8ee7de..78942e2 100644 --- a/app/Livewire/Mod/Listing.php +++ b/app/Livewire/Mod/Listing.php @@ -9,6 +9,7 @@ use Illuminate\Contracts\View\View; use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Facades\Cache; use Livewire\Attributes\Computed; +use Livewire\Attributes\Locked; use Livewire\Attributes\Session; use Livewire\Attributes\Url; use Livewire\Component; @@ -32,8 +33,18 @@ class Listing extends Component #[Url] public string $order = 'created'; + /** + * The number of results to show on a single page. + */ + #[Session] #[Url] - public int $resultsPerPage = 12; + public int $perPage = 12; + + /** + * The options that are available for the per page setting. + */ + #[Locked] + public array $perPageOptions = [6, 12, 24, 50]; /** * The SPT versions filter value. @@ -83,6 +94,8 @@ class Listing extends Component */ public function render(): View { + $this->validatePerPage(); + // Fetch the mods using the filters saved to the component properties. $filters = [ 'query' => $this->query, @@ -90,13 +103,32 @@ class Listing extends Component 'order' => $this->order, 'sptVersions' => $this->sptVersions, ]; - $mods = (new ModFilter($filters))->apply()->paginate($this->resultsPerPage); + + $mods = (new ModFilter($filters))->apply()->paginate($this->perPage); $this->redirectOutOfBoundsPage($mods); return view('livewire.mod.listing', compact('mods')); } + /** + * Validate that the option selected is an option that is available by setting it to the closest available version. + */ + public function validatePerPage(): void + { + $this->perPage = collect($this->perPageOptions)->pipe(function ($data) { + $closest = null; + + foreach ($data as $item) { + if ($closest === null || abs($this->perPage - $closest) > abs($item - $this->perPage)) { + $closest = $item; + } + } + + return $closest; + }); + } + /** * Check if the current page is greater than the last page. Redirect if it is. */ diff --git a/resources/views/livewire/mod/listing.blade.php b/resources/views/livewire/mod/listing.blade.php index 820b3dc..c25c960 100644 --- a/resources/views/livewire/mod/listing.blade.php +++ b/resources/views/livewire/mod/listing.blade.php @@ -105,14 +105,14 @@
{{-- Large display can show full text --}} {{-- Only show selected number on smaller screens --}} -