Skip to content

Commit e0238d5

Browse files
scott graysonscott grayson
authored andcommitted
feat: implement separate create buttons with modal forms
- Add separate 'Create Folder' and 'Upload File' modal actions - Create folder modal: name input only - Upload file modal: file upload only - Location is automatically set to current folder (cannot be changed) - Edit form simplified to only allow name changes - Remove type and parent_id from edit form (immutable after creation) - Fix auth()->id() calls to use auth()->user()?->id
1 parent 75c3d1a commit e0238d5

File tree

6 files changed

+202
-23
lines changed

6 files changed

+202
-23
lines changed

src/Resources/LibraryItemResource.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,43 @@ public static function form(Schema $schema): Schema
5151
]);
5252
}
5353

54+
public static function folderForm(Schema $schema): Schema
55+
{
56+
return $schema
57+
->components([
58+
\Filament\Forms\Components\TextInput::make('name')
59+
->label('Folder Name')
60+
->required()
61+
->maxLength(255)
62+
->placeholder('Enter folder name'),
63+
]);
64+
}
65+
66+
public static function fileForm(Schema $schema): Schema
67+
{
68+
return $schema
69+
->components([
70+
\Filament\Forms\Components\FileUpload::make('file')
71+
->label('Upload File')
72+
->required()
73+
->acceptedFileTypes(['*'])
74+
->maxSize(10240) // 10MB
75+
->disk('public')
76+
->directory('library-files')
77+
->visibility('private'),
78+
]);
79+
}
80+
81+
public static function editForm(Schema $schema): Schema
82+
{
83+
return $schema
84+
->components([
85+
\Filament\Forms\Components\TextInput::make('name')
86+
->required()
87+
->maxLength(255),
88+
]);
89+
}
90+
5491
public static function table(Table $table): Table
5592
{
5693
return $table
@@ -90,8 +127,8 @@ public static function table(Table $table): Table
90127
])
91128
->actions([
92129
ViewAction::make()
93-
->url(fn (LibraryItem $record): string =>
94-
$record->type === 'folder'
130+
->url(fn (LibraryItem $record): string =>
131+
$record->type === 'folder'
95132
? static::getUrl('index', ['parent' => $record->id])
96133
: static::getUrl('view', ['record' => $record])
97134
),
@@ -117,6 +154,8 @@ public static function getPages(): array
117154
return [
118155
'index' => Pages\ListLibraryItems::route('/'),
119156
'create' => Pages\CreateLibraryItem::route('/create'),
157+
'create-folder' => Pages\CreateFolder::route('/create-folder'),
158+
'create-file' => Pages\CreateFile::route('/create-file'),
120159
'view' => Pages\ViewLibraryItem::route('/{record}'),
121160
'edit' => Pages\EditLibraryItem::route('/{record}/edit'),
122161
];

src/Resources/Pages/CreateFile.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Tapp\FilamentLibrary\Resources\Pages;
4+
5+
use Filament\Resources\Pages\CreateRecord;
6+
use Tapp\FilamentLibrary\Resources\LibraryItemResource;
7+
8+
class CreateFile extends CreateRecord
9+
{
10+
protected static string $resource = LibraryItemResource::class;
11+
12+
public ?int $parentId = null;
13+
14+
public function mount(): void
15+
{
16+
parent::mount();
17+
18+
$this->parentId = request()->get('parent');
19+
}
20+
21+
protected function mutateFormDataBeforeCreate(array $data): array
22+
{
23+
$data['type'] = 'file';
24+
$data['parent_id'] = $this->parentId;
25+
$data['created_by'] = auth()->user()?->id;
26+
27+
return $data;
28+
}
29+
30+
protected function getRedirectUrl(): string
31+
{
32+
return $this->parentId
33+
? static::getResource()::getUrl('index', ['parent' => $this->parentId])
34+
: static::getResource()::getUrl('index');
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Tapp\FilamentLibrary\Resources\Pages;
4+
5+
use Filament\Resources\Pages\CreateRecord;
6+
use Tapp\FilamentLibrary\Resources\LibraryItemResource;
7+
8+
class CreateFolder extends CreateRecord
9+
{
10+
protected static string $resource = LibraryItemResource::class;
11+
12+
public ?int $parentId = null;
13+
14+
public function mount(): void
15+
{
16+
parent::mount();
17+
18+
$this->parentId = request()->get('parent');
19+
}
20+
21+
protected function mutateFormDataBeforeCreate(array $data): array
22+
{
23+
$data['type'] = 'folder';
24+
$data['parent_id'] = $this->parentId;
25+
$data['created_by'] = auth()->user()?->id;
26+
27+
return $data;
28+
}
29+
30+
protected function getRedirectUrl(): string
31+
{
32+
return $this->parentId
33+
? static::getResource()::getUrl('index', ['parent' => $this->parentId])
34+
: static::getResource()::getUrl('index');
35+
}
36+
}

src/Resources/Pages/CreateLibraryItem.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class CreateLibraryItem extends CreateRecord
1515
public function mount(): void
1616
{
1717
parent::mount();
18-
18+
1919
$this->parentId = request()->get('parent');
20-
20+
2121
if ($this->parentId) {
2222
$this->form->fill([
2323
'parent_id' => $this->parentId,
@@ -30,9 +30,9 @@ protected function mutateFormDataBeforeCreate(array $data): array
3030
if ($this->parentId) {
3131
$data['parent_id'] = $this->parentId;
3232
}
33-
33+
3434
$data['created_by'] = auth()->id();
35-
35+
3636
return $data;
3737
}
3838
}

src/Resources/Pages/EditLibraryItem.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Filament\Resources\Pages\EditRecord;
66
use Tapp\FilamentLibrary\Resources\LibraryItemResource;
7+
use Filament\Actions\DeleteAction;
78

89
class EditLibraryItem extends EditRecord
910
{
@@ -12,7 +13,29 @@ class EditLibraryItem extends EditRecord
1213
protected function getHeaderActions(): array
1314
{
1415
return [
15-
//
16+
DeleteAction::make(),
17+
];
18+
}
19+
20+
protected function mutateFormDataBeforeFill(array $data): array
21+
{
22+
// Remove fields that shouldn't be editable
23+
unset($data['type']);
24+
unset($data['parent_id']);
25+
unset($data['created_by']);
26+
27+
return $data;
28+
}
29+
30+
protected function getForms(): array
31+
{
32+
return [
33+
'form' => $this->form(
34+
$this->makeForm()
35+
->schema(static::getResource()::editForm($this->makeFormSchema())->getComponents())
36+
->statePath('data')
37+
->model($this->getRecord())
38+
),
1639
];
1740
}
1841
}

src/Resources/Pages/ListLibraryItems.php

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Tapp\FilamentLibrary\Models\LibraryItem;
88
use Filament\Actions\CreateAction;
99
use Filament\Actions\Action;
10+
use Filament\Forms\Components\TextInput;
11+
use Filament\Forms\Components\FileUpload;
1012

1113
class ListLibraryItems extends ListRecords
1214
{
@@ -18,9 +20,9 @@ class ListLibraryItems extends ListRecords
1820
public function mount(): void
1921
{
2022
parent::mount();
21-
23+
2224
$this->parentId = request()->get('parent');
23-
25+
2426
if ($this->parentId) {
2527
$this->parentFolder = LibraryItem::find($this->parentId);
2628
}
@@ -43,27 +45,70 @@ protected function getHeaderActions(): array
4345
->color('gray');
4446
}
4547

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-
);
48+
// Add "Create Folder" modal action
49+
$actions[] = Action::make('create_folder')
50+
->label('Create Folder')
51+
->icon('heroicon-o-folder-plus')
52+
->color('success')
53+
->form([
54+
TextInput::make('name')
55+
->label('Folder Name')
56+
->required()
57+
->maxLength(255)
58+
->placeholder('Enter folder name'),
59+
])
60+
->action(function (array $data): void {
61+
LibraryItem::create([
62+
'name' => $data['name'],
63+
'type' => 'folder',
64+
'parent_id' => $this->parentId,
65+
'created_by' => auth()->user()?->id,
66+
]);
67+
68+
$this->redirect(static::getResource()::getUrl('index', $this->parentId ? ['parent' => $this->parentId] : []));
69+
});
70+
71+
// Add "Upload File" modal action
72+
$actions[] = Action::make('upload_file')
73+
->label('Upload File')
74+
->icon('heroicon-o-document-plus')
75+
->color('primary')
76+
->form([
77+
FileUpload::make('file')
78+
->label('Upload File')
79+
->required()
80+
->acceptedFileTypes(['*'])
81+
->maxSize(10240) // 10MB
82+
->disk('public')
83+
->directory('library-files')
84+
->visibility('private'),
85+
])
86+
->action(function (array $data): void {
87+
$file = $data['file'];
88+
89+
LibraryItem::create([
90+
'name' => $file->getClientOriginalName(),
91+
'type' => 'file',
92+
'parent_id' => $this->parentId,
93+
'created_by' => auth()->user()?->id,
94+
]);
95+
96+
$this->redirect(static::getResource()::getUrl('index', $this->parentId ? ['parent' => $this->parentId] : []));
97+
});
5398

5499
return $actions;
55100
}
56101

57102
protected function getTableQuery(): \Illuminate\Database\Eloquent\Builder
58103
{
59104
$query = parent::getTableQuery();
60-
105+
61106
if ($this->parentId) {
62107
$query->where('parent_id', $this->parentId);
63108
} else {
64109
$query->whereNull('parent_id');
65110
}
66-
111+
67112
return $query;
68113
}
69114

@@ -72,7 +117,7 @@ public function getTitle(): string
72117
if ($this->parentFolder) {
73118
return $this->parentFolder->name;
74119
}
75-
120+
76121
return 'All Folders';
77122
}
78123

@@ -81,21 +126,21 @@ public function getBreadcrumbs(): array
81126
$breadcrumbs = [
82127
static::getResource()::getUrl() => 'All Folders',
83128
];
84-
129+
85130
if ($this->parentFolder) {
86131
$current = $this->parentFolder;
87132
$path = [];
88-
133+
89134
while ($current) {
90135
array_unshift($path, $current);
91136
$current = $current->parent;
92137
}
93-
138+
94139
foreach ($path as $folder) {
95140
$breadcrumbs[static::getResource()::getUrl('index', ['parent' => $folder->id])] = $folder->name;
96141
}
97142
}
98-
143+
99144
return $breadcrumbs;
100145
}
101146
}

0 commit comments

Comments
 (0)