Skip to content

Commit 75c3d1a

Browse files
scott graysonscott grayson
authored andcommitted
feat: implement folder navigation and file viewing
- Add ViewAction as default row action - Create ViewLibraryItem page for file viewing - Implement folder navigation with parent parameter - Add breadcrumb navigation - Add 'Up One Level' action for subfolders - Update CreateLibraryItem to handle parent context - Show folder contents when clicking on folders - Show file view page when clicking on files
1 parent 1d7ef03 commit 75c3d1a

File tree

4 files changed

+139
-2
lines changed

4 files changed

+139
-2
lines changed

src/Resources/LibraryItemResource.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Filament\Tables\Table;
99
use Filament\Actions\EditAction;
1010
use Filament\Actions\DeleteAction;
11+
use Filament\Actions\ViewAction;
1112
use Filament\Actions\BulkActionGroup;
1213
use Filament\Actions\DeleteBulkAction;
1314
use Tapp\FilamentLibrary\Models\LibraryItem;
@@ -88,6 +89,12 @@ public static function table(Table $table): Table
8889
]),
8990
])
9091
->actions([
92+
ViewAction::make()
93+
->url(fn (LibraryItem $record): string =>
94+
$record->type === 'folder'
95+
? static::getUrl('index', ['parent' => $record->id])
96+
: static::getUrl('view', ['record' => $record])
97+
),
9198
EditAction::make(),
9299
DeleteAction::make(),
93100
])
@@ -110,6 +117,7 @@ public static function getPages(): array
110117
return [
111118
'index' => Pages\ListLibraryItems::route('/'),
112119
'create' => Pages\CreateLibraryItem::route('/create'),
120+
'view' => Pages\ViewLibraryItem::route('/{record}'),
113121
'edit' => Pages\EditLibraryItem::route('/{record}/edit'),
114122
];
115123
}

src/Resources/Pages/CreateLibraryItem.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,35 @@
44

55
use Filament\Resources\Pages\CreateRecord;
66
use Tapp\FilamentLibrary\Resources\LibraryItemResource;
7+
use Tapp\FilamentLibrary\Models\LibraryItem;
78

89
class CreateLibraryItem extends CreateRecord
910
{
1011
protected static string $resource = LibraryItemResource::class;
12+
13+
public ?int $parentId = null;
14+
15+
public function mount(): void
16+
{
17+
parent::mount();
18+
19+
$this->parentId = request()->get('parent');
20+
21+
if ($this->parentId) {
22+
$this->form->fill([
23+
'parent_id' => $this->parentId,
24+
]);
25+
}
26+
}
27+
28+
protected function mutateFormDataBeforeCreate(array $data): array
29+
{
30+
if ($this->parentId) {
31+
$data['parent_id'] = $this->parentId;
32+
}
33+
34+
$data['created_by'] = auth()->id();
35+
36+
return $data;
37+
}
1138
}

src/Resources/Pages/ListLibraryItems.php

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,98 @@
44

55
use Filament\Resources\Pages\ListRecords;
66
use Tapp\FilamentLibrary\Resources\LibraryItemResource;
7+
use Tapp\FilamentLibrary\Models\LibraryItem;
8+
use Filament\Actions\CreateAction;
9+
use Filament\Actions\Action;
710

811
class ListLibraryItems extends ListRecords
912
{
1013
protected static string $resource = LibraryItemResource::class;
1114

15+
public ?int $parentId = null;
16+
public ?LibraryItem $parentFolder = null;
17+
18+
public function mount(): void
19+
{
20+
parent::mount();
21+
22+
$this->parentId = request()->get('parent');
23+
24+
if ($this->parentId) {
25+
$this->parentFolder = LibraryItem::find($this->parentId);
26+
}
27+
}
28+
1229
protected function getHeaderActions(): array
1330
{
14-
return [
15-
//
31+
$actions = [];
32+
33+
// Add "Up One Level" action if we're in a subfolder
34+
if ($this->parentId && $this->parentFolder) {
35+
$actions[] = Action::make('up_one_level')
36+
->label('Up One Level')
37+
->icon('heroicon-o-arrow-up')
38+
->url(fn (): string =>
39+
$this->parentFolder->parent_id
40+
? static::getResource()::getUrl('index', ['parent' => $this->parentFolder->parent_id])
41+
: static::getResource()::getUrl('index')
42+
)
43+
->color('gray');
44+
}
45+
46+
// Add "Create" action
47+
$actions[] = CreateAction::make()
48+
->url(fn (): string =>
49+
$this->parentId
50+
? static::getResource()::getUrl('create', ['parent' => $this->parentId])
51+
: static::getResource()::getUrl('create')
52+
);
53+
54+
return $actions;
55+
}
56+
57+
protected function getTableQuery(): \Illuminate\Database\Eloquent\Builder
58+
{
59+
$query = parent::getTableQuery();
60+
61+
if ($this->parentId) {
62+
$query->where('parent_id', $this->parentId);
63+
} else {
64+
$query->whereNull('parent_id');
65+
}
66+
67+
return $query;
68+
}
69+
70+
public function getTitle(): string
71+
{
72+
if ($this->parentFolder) {
73+
return $this->parentFolder->name;
74+
}
75+
76+
return 'All Folders';
77+
}
78+
79+
public function getBreadcrumbs(): array
80+
{
81+
$breadcrumbs = [
82+
static::getResource()::getUrl() => 'All Folders',
1683
];
84+
85+
if ($this->parentFolder) {
86+
$current = $this->parentFolder;
87+
$path = [];
88+
89+
while ($current) {
90+
array_unshift($path, $current);
91+
$current = $current->parent;
92+
}
93+
94+
foreach ($path as $folder) {
95+
$breadcrumbs[static::getResource()::getUrl('index', ['parent' => $folder->id])] = $folder->name;
96+
}
97+
}
98+
99+
return $breadcrumbs;
17100
}
18101
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Tapp\FilamentLibrary\Resources\Pages;
4+
5+
use Filament\Resources\Pages\ViewRecord;
6+
use Tapp\FilamentLibrary\Resources\LibraryItemResource;
7+
8+
class ViewLibraryItem extends ViewRecord
9+
{
10+
protected static string $resource = LibraryItemResource::class;
11+
12+
protected function getHeaderActions(): array
13+
{
14+
return [
15+
\Filament\Actions\EditAction::make(),
16+
\Filament\Actions\DeleteAction::make(),
17+
];
18+
}
19+
}

0 commit comments

Comments
 (0)