Skip to content

Commit 986f2cc

Browse files
scott graysonscott grayson
authored andcommitted
handle duplicate slugs and update delete redirects
1 parent efb3449 commit 986f2cc

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

src/Models/LibraryItem.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,9 @@ protected static function generateUniqueSlug(string $name, ?int $parentId = null
220220
$slug = $baseSlug;
221221
$counter = 1;
222222

223-
while (static::where('slug', $slug)
223+
// Check for existing slugs (including soft-deleted ones)
224+
while (static::withTrashed()
225+
->where('slug', $slug)
224226
->where('parent_id', $parentId)
225227
->when($excludeId, fn ($q) => $q->where('id', '!=', $excludeId))
226228
->exists()) {

src/Resources/LibraryItemResource.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Filament\Tables;
88
use Filament\Tables\Table;
99
use Filament\Actions\EditAction;
10+
use Filament\Actions\DeleteAction;
1011
use Filament\Actions\BulkActionGroup;
1112
use Filament\Actions\DeleteBulkAction;
1213
use Filament\Actions\RestoreAction;
@@ -135,14 +136,35 @@ public static function table(Table $table): Table
135136
])
136137
->actions([
137138
EditAction::make(),
139+
DeleteAction::make()
140+
->successRedirectUrl(function (LibraryItem $record) {
141+
// Redirect to the parent folder after deletion
142+
$parentId = $record->parent_id;
143+
return static::getUrl('index', $parentId ? ['parent' => $parentId] : []);
144+
}),
138145
RestoreAction::make(),
139-
ForceDeleteAction::make(),
146+
ForceDeleteAction::make()
147+
->successRedirectUrl(function (LibraryItem $record) {
148+
// Redirect to the parent folder after deletion
149+
$parentId = $record->parent_id;
150+
return static::getUrl('index', $parentId ? ['parent' => $parentId] : []);
151+
}),
140152
])
141153
->bulkActions([
142154
BulkActionGroup::make([
143-
DeleteBulkAction::make(),
155+
DeleteBulkAction::make()
156+
->successRedirectUrl(function () {
157+
// For bulk actions, redirect to current folder (maintain current location)
158+
$currentParent = request()->get('parent');
159+
return static::getUrl('index', $currentParent ? ['parent' => $currentParent] : []);
160+
}),
144161
RestoreBulkAction::make(),
145-
ForceDeleteBulkAction::make(),
162+
ForceDeleteBulkAction::make()
163+
->successRedirectUrl(function () {
164+
// For bulk actions, redirect to current folder (maintain current location)
165+
$currentParent = request()->get('parent');
166+
return static::getUrl('index', $currentParent ? ['parent' => $currentParent] : []);
167+
}),
146168
]),
147169
])
148170
->recordUrl(function (LibraryItem $record): string {

src/Resources/Pages/EditLibraryItem.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ protected function getHeaderActions(): array
3535
}
3636

3737

38-
$actions[] = DeleteAction::make();
38+
$actions[] = DeleteAction::make()
39+
->successRedirectUrl(function () {
40+
// Redirect to the parent folder after deletion
41+
$parentId = $this->getRecord()->parent_id;
42+
return static::getResource()::getUrl('index', $parentId ? ['parent' => $parentId] : []);
43+
});
3944

4045
return $actions;
4146
}

src/Resources/Pages/ListLibraryItems.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ protected function getHeaderActions(): array
4444
: static::getResource()::getUrl('index')
4545
)
4646
->color('gray');
47+
48+
// Add "Edit" action for the current folder
49+
$actions[] = Action::make('edit_folder')
50+
->label('Edit')
51+
->icon('heroicon-o-pencil')
52+
->color('gray')
53+
->url(fn (): string =>
54+
static::getResource()::getUrl('edit', ['record' => $this->parentFolder])
55+
);
4756
}
4857

4958
// Add "+ New" dropdown action group
@@ -79,12 +88,13 @@ protected function getHeaderActions(): array
7988
->maxSize(10240) // 10MB
8089
->disk('public')
8190
->directory('library-files')
82-
->visibility('private'),
91+
->visibility('private')
92+
->preserveFilenames(), // This should preserve original filenames
8393
])
8494
->action(function (array $data): void {
8595
$filePath = $data['file'];
8696

87-
// Extract filename from the path
97+
// Extract filename from the stored path - this should preserve the original name
8898
$fileName = basename($filePath);
8999

90100
LibraryItem::create([

0 commit comments

Comments
 (0)