From d1b142c2eeb3f0d0b090913e6fe7169a48220091 Mon Sep 17 00:00:00 2001
From: Refringe
Date: Fri, 13 Sep 2024 00:08:00 -0400
Subject: [PATCH] Reworked Mod Listing
Updates the mod listing components to be a little more friendly.
---
app/Livewire/Mod/{Index.php => Listing.php} | 16 +++-
app/Models/Mod.php | 3 +
app/Providers/AppServiceProvider.php | 2 +-
.../{ModListSection.php => HomepageMods.php} | 90 +++++--------------
app/View/Components/ModList.php | 39 --------
app/View/Components/ModListStats.php | 21 -----
.../views/components/homepage-mods.blade.php | 22 +++++
resources/views/components/mod-card.blade.php | 45 +++++++---
.../mod-list-section-partial.blade.php | 6 --
.../components/mod-list-section.blade.php | 8 --
.../views/components/mod-list-stats.blade.php | 32 -------
resources/views/components/mod-list.blade.php | 9 --
.../components/page-content-title.blade.php | 2 +-
resources/views/components/time.blade.php | 5 ++
resources/views/home.blade.php | 2 +-
.../{index.blade.php => listing.blade.php} | 2 +-
resources/views/mod/index.blade.php | 2 +-
17 files changed, 101 insertions(+), 205 deletions(-)
rename app/Livewire/Mod/{Index.php => Listing.php} (88%)
rename app/View/Components/{ModListSection.php => HomepageMods.php} (61%)
delete mode 100644 app/View/Components/ModList.php
delete mode 100644 app/View/Components/ModListStats.php
create mode 100644 resources/views/components/homepage-mods.blade.php
delete mode 100644 resources/views/components/mod-list-section-partial.blade.php
delete mode 100644 resources/views/components/mod-list-section.blade.php
delete mode 100644 resources/views/components/mod-list-stats.blade.php
delete mode 100644 resources/views/components/mod-list.blade.php
create mode 100644 resources/views/components/time.blade.php
rename resources/views/livewire/mod/{index.blade.php => listing.blade.php} (99%)
diff --git a/app/Livewire/Mod/Index.php b/app/Livewire/Mod/Listing.php
similarity index 88%
rename from app/Livewire/Mod/Index.php
rename to app/Livewire/Mod/Listing.php
index 34c27fc..bc6d1d9 100644
--- a/app/Livewire/Mod/Index.php
+++ b/app/Livewire/Mod/Listing.php
@@ -4,6 +4,7 @@ namespace App\Livewire\Mod;
use App\Http\Filters\ModFilter;
use App\Models\SptVersion;
+use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Cache;
@@ -12,7 +13,7 @@ use Livewire\Attributes\Url;
use Livewire\Component;
use Livewire\WithPagination;
-class Index extends Component
+class Listing extends Component
{
use WithPagination;
@@ -87,12 +88,19 @@ class Index extends Component
];
$mods = (new ModFilter($filters))->apply()->paginate(16);
- // Check if the current page is greater than the last page. Redirect if it is.
+ $this->redirectOutOfBoundsPage($mods);
+
+ return view('livewire.mod.listing', compact('mods'));
+ }
+
+ /**
+ * Check if the current page is greater than the last page. Redirect if it is.
+ */
+ private function redirectOutOfBoundsPage(LengthAwarePaginator $mods): void
+ {
if ($mods->currentPage() > $mods->lastPage()) {
$this->redirectRoute('mods', ['page' => $mods->lastPage()]);
}
-
- return view('livewire.mod.index', compact('mods'));
}
/**
diff --git a/app/Models/Mod.php b/app/Models/Mod.php
index 3093205..6e3ce68 100644
--- a/app/Models/Mod.php
+++ b/app/Models/Mod.php
@@ -226,6 +226,9 @@ class Mod extends Model
'contains_ai_content' => 'boolean',
'contains_ads' => 'boolean',
'disabled' => 'boolean',
+ 'created_at' => 'datetime',
+ 'updated_at' => 'datetime',
+ 'deleted_at' => 'datetime',
];
}
diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php
index f1f3d51..0b307fd 100644
--- a/app/Providers/AppServiceProvider.php
+++ b/app/Providers/AppServiceProvider.php
@@ -11,8 +11,8 @@ use App\Observers\ModDependencyObserver;
use App\Observers\ModObserver;
use App\Observers\ModVersionObserver;
use App\Observers\SptVersionObserver;
+use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
-use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Number;
use Illuminate\Support\ServiceProvider;
diff --git a/app/View/Components/ModListSection.php b/app/View/Components/HomepageMods.php
similarity index 61%
rename from app/View/Components/ModListSection.php
rename to app/View/Components/HomepageMods.php
index be87d7c..54f8b3a 100644
--- a/app/View/Components/ModListSection.php
+++ b/app/View/Components/HomepageMods.php
@@ -9,34 +9,27 @@ use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\View\Component;
-class ModListSection extends Component
+class HomepageMods extends Component
{
- /**
- * The featured mods listed on the homepage.
- *
- * @var Collection
- */
- public Collection $modsFeatured;
-
- /**
- * The latest mods listed on the homepage.
- *
- * @var Collection
- */
- public Collection $modsLatest;
-
- /**
- * The last updated mods listed on the homepage.
- *
- * @var Collection
- */
- public Collection $modsUpdated;
-
- public function __construct()
+ public function render(): View
{
- $this->modsFeatured = $this->fetchFeaturedMods();
- $this->modsLatest = $this->fetchLatestMods();
- $this->modsUpdated = $this->fetchUpdatedMods();
+ return view('components.homepage-mods', [
+ 'featured' => [
+ 'title' => __('Featured Mods'),
+ 'mods' => $this->fetchFeaturedMods(),
+ 'link' => '/mods?featured=only',
+ ],
+ 'latest' => [
+ 'title' => __('Newest Mods'),
+ 'mods' => $this->fetchLatestMods(),
+ 'link' => '/mods',
+ ],
+ 'updated' => [
+ 'title' => __('Recently Updated Mods'),
+ 'mods' => $this->fetchUpdatedMods(),
+ 'link' => '/mods?order=updated',
+ ],
+ ]);
}
/**
@@ -46,9 +39,8 @@ class ModListSection extends Component
*/
private function fetchFeaturedMods(): Collection
{
- return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'downloads'])
+ return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'downloads', 'updated_at'])
->with([
- 'latestVersion',
'latestVersion.latestSptVersion:id,version,color_class',
'users:id,name',
'license:id,name,link',
@@ -66,9 +58,8 @@ class ModListSection extends Component
*/
private function fetchLatestMods(): Collection
{
- return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'created_at', 'downloads'])
+ return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'created_at', 'downloads', 'updated_at'])
->with([
- 'latestVersion',
'latestVersion.latestSptVersion:id,version,color_class',
'users:id,name',
'license:id,name,link',
@@ -85,9 +76,8 @@ class ModListSection extends Component
*/
private function fetchUpdatedMods(): Collection
{
- return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'downloads'])
+ return Mod::select(['id', 'name', 'slug', 'teaser', 'thumbnail', 'featured', 'downloads', 'updated_at'])
->with([
- 'lastUpdatedVersion',
'lastUpdatedVersion.latestSptVersion:id,version,color_class',
'users:id,name',
'license:id,name,link',
@@ -103,40 +93,4 @@ class ModListSection extends Component
->limit(6)
->get();
}
-
- public function render(): View
- {
- return view('components.mod-list-section', [
- 'sections' => $this->getSections(),
- ]);
- }
-
- /**
- * Prepare the sections for the homepage mod lists.
- *
- * @return array>
- */
- public function getSections(): array
- {
- return [
- [
- 'title' => __('Featured Mods'),
- 'mods' => $this->modsFeatured,
- 'versionScope' => 'latestVersion',
- 'link' => '/mods?featured=only',
- ],
- [
- 'title' => __('Newest Mods'),
- 'mods' => $this->modsLatest,
- 'versionScope' => 'latestVersion',
- 'link' => '/mods',
- ],
- [
- 'title' => __('Recently Updated Mods'),
- 'mods' => $this->modsUpdated,
- 'versionScope' => 'lastUpdatedVersion',
- 'link' => '/mods?order=updated',
- ],
- ];
- }
}
diff --git a/app/View/Components/ModList.php b/app/View/Components/ModList.php
deleted file mode 100644
index d4a8369..0000000
--- a/app/View/Components/ModList.php
+++ /dev/null
@@ -1,39 +0,0 @@
-
- */
- public Collection $mods;
-
- public string $versionScope;
-
- /**
- * Create a new component instance.
- *
- * @param Collection $mods
- */
- public function __construct(Collection $mods, string $versionScope)
- {
- $this->mods = $mods;
- $this->versionScope = $versionScope;
- }
-
- public function render(): View
- {
- return view('components.mod-list', [
- 'mods' => $this->mods,
- 'versionScope' => $this->versionScope,
- ]);
- }
-}
diff --git a/app/View/Components/ModListStats.php b/app/View/Components/ModListStats.php
deleted file mode 100644
index 3591b3c..0000000
--- a/app/View/Components/ModListStats.php
+++ /dev/null
@@ -1,21 +0,0 @@
-
+
+
+ @foreach ($featured['mods'] as $mod)
+
+ @endforeach
+
+
+
+
+ @foreach ($latest['mods'] as $mod)
+
+ @endforeach
+
+
+
+
+ @foreach ($updated['mods'] as $mod)
+
+ @endforeach
+
+
diff --git a/resources/views/components/mod-card.blade.php b/resources/views/components/mod-card.blade.php
index 361be56..55a12be 100644
--- a/resources/views/components/mod-card.blade.php
+++ b/resources/views/components/mod-card.blade.php
@@ -1,37 +1,56 @@
-@props(['mod', 'versionScope' => 'none'])
+@props(['mod', 'version'])
-
+
@if ($mod->featured && !request()->routeIs('home'))
{{ __('Featured!') }}
@endif
- @if (empty($mod->thumbnail))
+ @empty($mod->thumbnail)
![{{ $mod->name }}](https://placehold.co/450x450/31343C/EEE?font=source-sans-pro&text={{ $mod->name }})
@else
![{{ $mod->name }}]({{ $mod->thumbnailUrl }})
- @endif
+ @endempty
{{ $mod->name }}
- @if($versionScope !== 'none' && $mod->{$versionScope} !== null && $mod->{$versionScope}->latestSptVersion !== null)
-
- {{ $mod->{$versionScope}->latestSptVersion->first()->version_formatted }}
+
+ {{ $version->latestSptVersion->first()->version_formatted }}
- @endif
- By {{ $mod->users->pluck('name')->implode(', ') }}
+ {{ __('By :authors', ['authors' => $mod->users->pluck('name')->implode(', ')]) }}
+
+
+ {{ Str::limit($mod->teaser, 100) }}
-
{{ Str::limit($mod->teaser, 100) }}
- @if($versionScope !== 'none' && $mod->{$versionScope} !== null)
-
- @endif
+
+
+ @if ($mod->updated_at || $mod->created_at)
+
+ @endif
+
+
+ {{ Number::downloads($mod->downloads) }}
+
+
+
+
+
diff --git a/resources/views/components/mod-list-section-partial.blade.php b/resources/views/components/mod-list-section-partial.blade.php
deleted file mode 100644
index 21dc3f9..0000000
--- a/resources/views/components/mod-list-section-partial.blade.php
+++ /dev/null
@@ -1,6 +0,0 @@
-@props(['mods', 'versionScope', 'title', 'link'])
-
-
-
-
-
diff --git a/resources/views/components/mod-list-section.blade.php b/resources/views/components/mod-list-section.blade.php
deleted file mode 100644
index 15a0daa..0000000
--- a/resources/views/components/mod-list-section.blade.php
+++ /dev/null
@@ -1,8 +0,0 @@
-@foreach ($sections as $section)
- @include('components.mod-list-section-partial', [
- 'title' => $section['title'],
- 'mods' => $section['mods'],
- 'versionScope' => $section['versionScope'],
- 'link' => $section['link']
- ])
-@endforeach
diff --git a/resources/views/components/mod-list-stats.blade.php b/resources/views/components/mod-list-stats.blade.php
deleted file mode 100644
index ab2cdd2..0000000
--- a/resources/views/components/mod-list-stats.blade.php
+++ /dev/null
@@ -1,32 +0,0 @@
-class(['text-slate-700 dark:text-gray-300 text-sm']) }}>
-
-
-
-
-
- @if(!is_null($mod->updated_at))
-
- @elseif(!is_null($mod->created_at))
-
- @else
- N/A
- @endif
-
-
-
-
-
- {{ Number::downloads($mod->downloads) }}
-
-
-
-
-
diff --git a/resources/views/components/mod-list.blade.php b/resources/views/components/mod-list.blade.php
deleted file mode 100644
index 956f6c2..0000000
--- a/resources/views/components/mod-list.blade.php
+++ /dev/null
@@ -1,9 +0,0 @@
-@props(['mods', 'versionScope'])
-
-
- @foreach ($mods as $mod)
- @if ($mod->{$versionScope})
-
- @endif
- @endforeach
-
diff --git a/resources/views/components/page-content-title.blade.php b/resources/views/components/page-content-title.blade.php
index 44816ba..ae0034d 100644
--- a/resources/views/components/page-content-title.blade.php
+++ b/resources/views/components/page-content-title.blade.php
@@ -2,7 +2,7 @@
{{ __($title) }}
- @if (isset($buttonText) && isset($buttonLink))
+ @if ($buttonText && $buttonLink)
-
+
diff --git a/resources/views/livewire/mod/index.blade.php b/resources/views/livewire/mod/listing.blade.php
similarity index 99%
rename from resources/views/livewire/mod/index.blade.php
rename to resources/views/livewire/mod/listing.blade.php
index b8167e4..94a6318 100644
--- a/resources/views/livewire/mod/index.blade.php
+++ b/resources/views/livewire/mod/listing.blade.php
@@ -127,7 +127,7 @@
@if ($mods->isNotEmpty())
@foreach ($mods as $mod)
-
+
@endforeach
@else
diff --git a/resources/views/mod/index.blade.php b/resources/views/mod/index.blade.php
index e2030b6..c611868 100644
--- a/resources/views/mod/index.blade.php
+++ b/resources/views/mod/index.blade.php
@@ -1,3 +1,3 @@
- @livewire('mod.index')
+ @livewire('mod.listing')