Skip to content

Commit a2590b6

Browse files
author
thapacodes4u
committed
Refactor: Using right URL for dynamic section & Page filtering accordingly & test cases
1 parent b84cbc2 commit a2590b6

3 files changed

Lines changed: 43 additions & 8 deletions

File tree

src/Admin/Filament/Resources/PageResource.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Illuminate\Database\Eloquent\Builder;
3131
use Illuminate\Database\Eloquent\SoftDeletingScope;
3232
use Illuminate\Support\Str;
33+
use Livewire\Attributes\Url;
3334

3435
class PageResource extends Resource
3536
{
@@ -47,6 +48,12 @@ class PageResource extends Resource
4748

4849
protected static ?string $navigationLabel = 'Pages';
4950

51+
// /**
52+
// * @var array<string, mixed> | null
53+
// */
54+
// #[Url()]
55+
// public ?array $tableFilters = null;
56+
5057
public static function form(Form $form): Form
5158
{
5259
return $form
@@ -163,6 +170,10 @@ public static function form(Form $form): Form
163170
public static function table(Table $table): Table
164171
{
165172
return $table
173+
->modifyQueryUsing(fn (Builder $query) => $query->when(
174+
request()->get('section'),
175+
fn (Builder $q, $sectionId) => $q->where('section_id', $sectionId)
176+
))
166177
->columns([
167178
TextColumn::make('title')
168179
->label('Page Title')
@@ -216,11 +227,6 @@ public static function table(Table $table): Table
216227
->toggleable(),
217228
])
218229
->filters([
219-
SelectFilter::make('section')
220-
->relationship('section', 'name')
221-
->searchable()
222-
->preload(),
223-
224230
SelectFilter::make('status')
225231
->options(PageStatus::class),
226232

src/CmsPlugin.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Eclipse\Cms;
44

5-
use Eclipse\Cms\Admin\Filament\Resources\SectionResource;
5+
use Eclipse\Cms\Admin\Filament\Resources\PageResource;
66
use Eclipse\Cms\Models\Section;
77
use Eclipse\Common\Foundation\Plugins\Plugin;
88
use Exception;
@@ -27,8 +27,8 @@ public function getSectionNavigationItems(): array
2727
->get()
2828
->map(fn (Section $section): NavigationItem => NavigationItem::make($section->getTranslation('name', app()->getLocale()))
2929
->url(
30-
fn (): string => SectionResource::getUrl('edit', [
31-
'record' => $section->id,
30+
fn (): string => PageResource::getUrl('index', [
31+
'section' => $section->id,
3232
])
3333
)
3434
->icon('heroicon-o-arrow-turn-down-right')

tests/Feature/Filament/Resources/PageResourceTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Eclipse\Cms\Admin\Filament\Resources\PageResource;
4+
use Eclipse\Cms\CmsPlugin;
45
use Eclipse\Cms\Enums\PageStatus;
56
use Eclipse\Cms\Models\Page;
67
use Eclipse\Cms\Models\Section;
@@ -152,3 +153,31 @@
152153

153154
expect($page->fresh()->trashed())->toBeTrue();
154155
});
156+
157+
test('pages can be filtered by section via URL parameter', function () {
158+
$section1 = Section::factory()->create(['name' => ['en' => 'Section 1']]);
159+
$section2 = Section::factory()->create(['name' => ['en' => 'Section 2']]);
160+
161+
$page1 = Page::factory()->forSection($section1)->create();
162+
$page2 = Page::factory()->forSection($section2)->create();
163+
164+
$response = $this->get(PageResource::getUrl('index').'?section='.$section1->id);
165+
166+
$response->assertSuccessful();
167+
$response->assertSee($page1->title);
168+
$response->assertDontSee($page2->title);
169+
});
170+
171+
test('section navigation items generate correct URLs', function () {
172+
$section = Section::factory()->create(['name' => ['en' => 'Test Section']]);
173+
174+
$plugin = new CmsPlugin;
175+
$navigationItems = $plugin->getSectionNavigationItems();
176+
177+
expect($navigationItems)->toHaveCount(1);
178+
179+
$item = $navigationItems[0];
180+
expect($item->getLabel())->toBe('Test Section');
181+
expect($item->getUrl())->toContain('section='.$section->id);
182+
expect($item->getGroup())->toBe('CMS');
183+
});

0 commit comments

Comments
 (0)