From 1391808793826d8cf833e48032a903c6165dc755 Mon Sep 17 00:00:00 2001 From: HolgerHatGarKeineNode Date: Sun, 25 Jan 2026 19:25:22 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Add=20dynamic=20category-based?= =?UTF-8?q?=20filtering=20for=20news=20and=20improve=20UI=20interactions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 🆕 Introduce `selectedCategory` state with URL binding for category filtering - 🪄 Add computed `filteredNews` property to handle filtered results efficiently - 🎛️ Implement category toggle buttons and "Clear Filter" functionality in UI - 🌟 Improve category display with badges and contextual feedback for empty states - 🔄 Refactor repeated news loading into a single `loadNews` method --- .../views/livewire/association/news.blade.php | 107 ++++++++++++++---- 1 file changed, 85 insertions(+), 22 deletions(-) diff --git a/resources/views/livewire/association/news.blade.php b/resources/views/livewire/association/news.blade.php index f30df3d..5ce2927 100644 --- a/resources/views/livewire/association/news.blade.php +++ b/resources/views/livewire/association/news.blade.php @@ -4,8 +4,10 @@ use App\Enums\NewsCategory; use App\Models\Notification; use App\Support\NostrAuth; use Illuminate\Support\Collection; +use Livewire\Attributes\Computed; use Livewire\Attributes\Layout; use Livewire\Attributes\Title; +use Livewire\Attributes\Url; use Livewire\Component; use Livewire\WithFileUploads; @@ -17,6 +19,9 @@ class extends Component { public Collection|array $news = []; + #[Url(as: 'kategorie')] + public ?int $selectedCategory = null; + public array $form = [ 'category' => '', 'name' => '', @@ -49,13 +54,40 @@ class extends Component { $this->canEdit = true; } - $this->news = \App\Models\Notification::query() - ->with(['einundzwanzigPleb.profile']) - ->latest() - ->get(); + $this->loadNews(); } } + #[Computed] + public function filteredNews(): Collection|array + { + if ($this->selectedCategory === null) { + return $this->news; + } + + return collect($this->news)->filter( + fn ($item) => $item->category->value === $this->selectedCategory + ); + } + + public function filterByCategory(?int $category): void + { + $this->selectedCategory = $this->selectedCategory === $category ? null : $category; + } + + public function clearFilter(): void + { + $this->selectedCategory = null; + } + + private function loadNews(): void + { + $this->news = Notification::query() + ->with(['einundzwanzigPleb.profile']) + ->latest() + ->get(); + } + public function save(): void { $this->validate([ @@ -81,11 +113,7 @@ class extends Component { } $this->reset(['form', 'file']); - - $this->news = \App\Models\Notification::query() - ->with(['einundzwanzigPleb.profile']) - ->latest() - ->get(); + $this->loadNews(); } public function confirmDelete(int $id): void @@ -97,11 +125,7 @@ class extends Component { { $news = Notification::query()->findOrFail($this->confirmDeleteId); $news->delete(); - - $this->news = \App\Models\Notification::query() - ->with(['einundzwanzigPleb.profile']) - ->latest() - ->get(); + $this->loadNews(); } public function removeFile(): void @@ -143,17 +167,38 @@ class extends Component {
- Menu + Kategorien
@@ -168,7 +213,7 @@ class extends Component {
- @forelse($news as $post) + @forelse($this->filteredNews as $post)
@@ -179,6 +224,17 @@ class extends Component {
+ +
+ +

{{ $post->name }} @@ -252,7 +308,14 @@ class extends Component { @empty -

Keine News vorhanden.

+ @if($selectedCategory !== null) +

Keine News in dieser Kategorie vorhanden.

+ + Alle anzeigen + + @else +

Keine News vorhanden.

+ @endif
@endforelse