From b0fbbcdd6861b7e6c180f5010276f73f3b2ecc1e Mon Sep 17 00:00:00 2001 From: aaron Date: Fri, 25 Oct 2024 23:16:44 -0500 Subject: [PATCH] feat: issue-49 - Update ImageUpload actions and columns --- .../Resources/ImageUploadResource.php | 31 ++++++++------ .../Pages/EditImageUpload.php | 12 +++++- tests/Filament/ImageUploadResourceTest.php | 42 +++++++++++++++++++ 3 files changed, 71 insertions(+), 14 deletions(-) diff --git a/app/Filament/Resources/ImageUploadResource.php b/app/Filament/Resources/ImageUploadResource.php index 7623a40..5ca71e3 100644 --- a/app/Filament/Resources/ImageUploadResource.php +++ b/app/Filament/Resources/ImageUploadResource.php @@ -16,6 +16,7 @@ use Filament\Tables\Columns\SpatieMediaLibraryImageColumn; use Filament\Tables\Table; use Illuminate\Database\Eloquent\Builder; +use Webbingbrasil\FilamentCopyActions\Tables\Actions\CopyAction; class ImageUploadResource extends Resource { @@ -80,36 +81,40 @@ function () { public static function table(Table $table): Table { return $table + ->defaultSort('updated_at', 'desc') ->columns([ Tables\Columns\TextColumn::make('title') ->searchable(), - SpatieMediaLibraryImageColumn::make('image')->collection('image'), - Tables\Columns\TextColumn::make('foo') - ->label('Markdown URL') - ->badge() - ->getStateUsing(function (ImageUpload $record) { - return 'Copy Markdown URL'; - }) - ->copyable() - ->copyableState(fn (ImageUpload $record): string => $record->markdownUrl), + SpatieMediaLibraryImageColumn::make('image') + ->collection('image'), Tables\Columns\TextColumn::make('user.name') ->numeric() ->hidden(fn () => ! auth()->user()->isAdministrator()) ->sortable(), Tables\Columns\TextColumn::make('created_at') - ->dateTime() + ->label('Created At') + ->date() ->sortable() ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('updated_at') - ->dateTime() + ->label('Updated At') + ->date() ->sortable() - ->toggleable(isToggledHiddenByDefault: true), + ->toggleable(), ]) ->filters([ // ]) ->actions([ - Tables\Actions\EditAction::make(), + Tables\Actions\ActionGroup::make([ + Tables\Actions\EditAction::make(), + CopyAction::make('copyImageUrl') + ->label('Copy Image URL') + ->copyable(fn (ImageUpload $record): string => $record->getFirstMediaUrl('image')), + CopyAction::make('copyMarkdownUrl') + ->label('Copy Markdown URL') + ->copyable(fn (ImageUpload $record): string => $record->markdownUrl), + ]), ]) ->bulkActions([ Tables\Actions\BulkActionGroup::make([ diff --git a/app/Filament/Resources/ImageUploadResource/Pages/EditImageUpload.php b/app/Filament/Resources/ImageUploadResource/Pages/EditImageUpload.php index 699105e..f8fd3e0 100644 --- a/app/Filament/Resources/ImageUploadResource/Pages/EditImageUpload.php +++ b/app/Filament/Resources/ImageUploadResource/Pages/EditImageUpload.php @@ -3,8 +3,10 @@ namespace App\Filament\Resources\ImageUploadResource\Pages; use App\Filament\Resources\ImageUploadResource; +use App\Models\ImageUpload; use Filament\Actions; use Filament\Resources\Pages\EditRecord; +use Webbingbrasil\FilamentCopyActions\Pages\Actions\CopyAction; class EditImageUpload extends EditRecord { @@ -13,7 +15,15 @@ class EditImageUpload extends EditRecord protected function getHeaderActions(): array { return [ - Actions\DeleteAction::make(), + Actions\ActionGroup::make([ + CopyAction::make('copyImageUrl') + ->label('Copy Image URL') + ->copyable(fn (ImageUpload $record): string => $record->getFirstMediaUrl('image')), + CopyAction::make('copyMarkdownUrl') + ->label('Copy Markdown URL') + ->copyable(fn (ImageUpload $record): string => $record->markdownUrl), + Actions\DeleteAction::make(), + ]), ]; } } diff --git a/tests/Filament/ImageUploadResourceTest.php b/tests/Filament/ImageUploadResourceTest.php index e75d295..34e0338 100644 --- a/tests/Filament/ImageUploadResourceTest.php +++ b/tests/Filament/ImageUploadResourceTest.php @@ -315,4 +315,46 @@ $this->assertModelMissing($record); }); + + // Actions + + it('has copyImageUrl action on list page', function () { + $record = Model::factory() + ->for($this->nonAdmin) + ->create(); + + livewire(ListResource::class) + ->assertTableActionVisible('copyImageUrl'); + }); + + it('has copyImageUrl action on edit page', function () { + $record = Model::factory() + ->for($this->nonAdmin) + ->create(); + + livewire(EditResource::class, [ + 'record' => $record->getRouteKey(), + ]) + ->assertActionExists('copyImageUrl'); + }); + + it('has copyMarkdownUrl action on list page', function () { + $record = Model::factory() + ->for($this->nonAdmin) + ->create(); + + livewire(ListResource::class) + ->assertTableActionVisible('copyMarkdownUrl'); + }); + + it('has copyMarkdownUrl action on edit page', function () { + $record = Model::factory() + ->for($this->nonAdmin) + ->create(); + + livewire(EditResource::class, [ + 'record' => $record->getRouteKey(), + ]) + ->assertActionExists('copyMarkdownUrl'); + }); });