From 977ec9ec0eb063f3cae37f3bda3b44f64240172b Mon Sep 17 00:00:00 2001 From: lguichard Date: Fri, 2 Feb 2024 01:33:35 +0100 Subject: [PATCH 1/4] Add support extendsInfolist hook --- docs/admin/extending/pages.md | 32 +++++++++- .../OrderResource/Pages/ManageOrder.php | 2 +- .../src/Support/Pages/BaseViewRecord.php | 4 ++ .../Pages/Concerns/ExtendsInfolist.php | 18 ++++++ .../Support/Extending/ViewPageExtension.php | 62 +++++++++++++++++++ 5 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 packages/admin/src/Support/Pages/Concerns/ExtendsInfolist.php create mode 100644 tests/admin/Feature/Support/Extending/ViewPageExtension.php diff --git a/docs/admin/extending/pages.md b/docs/admin/extending/pages.md index 037fd84c37..e5cbb0c798 100644 --- a/docs/admin/extending/pages.md +++ b/docs/admin/extending/pages.md @@ -246,10 +246,23 @@ An example of extending a view page. use Filament\Actions; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Table; +use Filament\Infolists\Infolist; +use Filament\Infolists\Components\TextEntry; use Lunar\Admin\Support\Extending\ViewPageExtension; +use Lunar\Admin\Filament\Widgets; class MyViewExtension extends ViewPageExtension { + public function headerWidgets(array $widgets): array + { + $widgets = [ + ...$widgets, + Widgets\Dashboard\Orders\OrderStatsOverview::make(), + ]; + + return $widgets; + } + public function headerActions(array $actions): array { $actions = [ @@ -261,7 +274,24 @@ class MyViewExtension extends ViewPageExtension return $actions; } - + + public function extendsInfolist(Infolist $infolist): Infolist + { + return $infolist->schema([ + ...$infolist->getComponents(true), + TextEntry::make('custom_title'), + ]); + } + + public function footerWidgets(array $widgets): array + { + $widgets = [ + ...$widgets, + Widgets\Dashboard\Orders\LatestOrdersTable::make(), + ]; + + return $widgets; + } } // Typically placed in your AppServiceProvider file... diff --git a/packages/admin/src/Filament/Resources/OrderResource/Pages/ManageOrder.php b/packages/admin/src/Filament/Resources/OrderResource/Pages/ManageOrder.php index 74c7473c2d..af8e6f0671 100644 --- a/packages/admin/src/Filament/Resources/OrderResource/Pages/ManageOrder.php +++ b/packages/admin/src/Filament/Resources/OrderResource/Pages/ManageOrder.php @@ -77,7 +77,7 @@ public function getTitle(): string|Htmlable return "{$label} #".$this->record->id; } - public function infolist(Infolist $infolist): Infolist + public function getDefaultInfolist(Infolist $infolist): Infolist { return $infolist ->schema([ diff --git a/packages/admin/src/Support/Pages/BaseViewRecord.php b/packages/admin/src/Support/Pages/BaseViewRecord.php index 6cadd9df8e..3e864d4ae8 100644 --- a/packages/admin/src/Support/Pages/BaseViewRecord.php +++ b/packages/admin/src/Support/Pages/BaseViewRecord.php @@ -6,6 +6,10 @@ abstract class BaseViewRecord extends ViewRecord { + use Concerns\ExtendsFooterWidgets; use Concerns\ExtendsHeaderActions; + use Concerns\ExtendsHeaderWidgets; + use Concerns\ExtendsInfolist; + use \Lunar\Admin\Support\Concerns\CallsHooks; use \Lunar\Admin\Support\Concerns\CallsHooks; } diff --git a/packages/admin/src/Support/Pages/Concerns/ExtendsInfolist.php b/packages/admin/src/Support/Pages/Concerns/ExtendsInfolist.php new file mode 100644 index 0000000000..25d91e971e --- /dev/null +++ b/packages/admin/src/Support/Pages/Concerns/ExtendsInfolist.php @@ -0,0 +1,18 @@ +getDefaultInfolist($infolist)); + } + + protected function getDefaultInfolist(Infolist $infolist): Infolist + { + return $infolist; + } +} diff --git a/tests/admin/Feature/Support/Extending/ViewPageExtension.php b/tests/admin/Feature/Support/Extending/ViewPageExtension.php new file mode 100644 index 0000000000..2014bc59e5 --- /dev/null +++ b/tests/admin/Feature/Support/Extending/ViewPageExtension.php @@ -0,0 +1,62 @@ +group('extending.view'); + +beforeEach(function () { + $this->asStaff(); + + $currency = \Lunar\Models\Currency::factory()->create([ + 'default' => true, + ]); + + $country = \Lunar\Models\Country::factory()->create(); + + $this->order = \Lunar\Models\Order::factory() + ->for(\Lunar\Models\Customer::factory()) + ->has(\Lunar\Models\OrderAddress::factory()->state([ + 'type' => 'shipping', + 'country_id' => $country->id, + ]), 'shippingAddress') + ->has(\Lunar\Models\OrderAddress::factory()->state([ + 'type' => 'billing', + 'country_id' => $country->id, + ]), 'billingAddress') + ->create([ + 'currency_code' => $currency->code, + 'meta' => [ + 'additional_info' => Str::random(), + ], + ]); + +}); + +it('can extend Infolist', function () { + $class = new class extends \Lunar\Admin\Support\Extending\ViewPageExtension + { + public function extendsInfolist(Infolist $infolist): Infolist + { + return $infolist->schema([ + ...$infolist->getComponents(true), + \Filament\Infolists\Components\TextEntry::make('custom_title') + ->label('custom_title'), + ]); + } + }; + + LunarPanel::registerExtension($class, ManageOrder::class); + + \Livewire\Livewire::test(ManageOrder::class, [ + 'record' => $this->order->getRouteKey(), + ]) + ->assertSee($this->order->reference) + ->assertSee('custom_title'); +}); From ea86681f9c782f315202a81ace07a46d8c385146 Mon Sep 17 00:00:00 2001 From: lguichard Date: Fri, 2 Feb 2024 01:37:22 +0100 Subject: [PATCH 2/4] Cleaning --- tests/admin/Feature/Support/Extending/ViewPageExtension.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/admin/Feature/Support/Extending/ViewPageExtension.php b/tests/admin/Feature/Support/Extending/ViewPageExtension.php index 2014bc59e5..f1c2af22ff 100644 --- a/tests/admin/Feature/Support/Extending/ViewPageExtension.php +++ b/tests/admin/Feature/Support/Extending/ViewPageExtension.php @@ -2,11 +2,8 @@ use Illuminate\Support\Str; use Filament\Infolists\Infolist; -use Filament\Widgets\StatsOverviewWidget as BaseWidget; -use Filament\Widgets\StatsOverviewWidget\Stat; use Lunar\Admin\Filament\Resources\OrderResource\Pages\ManageOrder; use Lunar\Admin\Support\Facades\LunarPanel; -use Lunar\Admin\Filament\Widgets; uses(\Lunar\Tests\Admin\Feature\Filament\TestCase::class) ->group('extending.view'); From be11eb5a8fe795aa3d07da9e32cc2ae874ba1c13 Mon Sep 17 00:00:00 2001 From: lguichard Date: Fri, 2 Feb 2024 01:37:37 +0100 Subject: [PATCH 3/4] Clean --- tests/admin/Feature/Support/Extending/ViewPageExtension.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/admin/Feature/Support/Extending/ViewPageExtension.php b/tests/admin/Feature/Support/Extending/ViewPageExtension.php index f1c2af22ff..4a531d9037 100644 --- a/tests/admin/Feature/Support/Extending/ViewPageExtension.php +++ b/tests/admin/Feature/Support/Extending/ViewPageExtension.php @@ -1,7 +1,7 @@ $this->order->getRouteKey(), ]) - ->assertSee($this->order->reference) - ->assertSee('custom_title'); + ->assertSee($this->order->reference) + ->assertSee('custom_title'); }); From 91259943e10186b556f26b0d9e8eb3078c4a8cb8 Mon Sep 17 00:00:00 2001 From: Lionel Guichard Date: Mon, 22 Apr 2024 11:54:45 +0200 Subject: [PATCH 4/4] Fixes --- .../Filament/Resources/OrderResource/Pages/ManageOrder.php | 4 ++-- packages/admin/src/Support/Pages/BaseViewRecord.php | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/admin/src/Filament/Resources/OrderResource/Pages/ManageOrder.php b/packages/admin/src/Filament/Resources/OrderResource/Pages/ManageOrder.php index e66fd1f612..3b7173be8d 100644 --- a/packages/admin/src/Filament/Resources/OrderResource/Pages/ManageOrder.php +++ b/packages/admin/src/Filament/Resources/OrderResource/Pages/ManageOrder.php @@ -284,7 +284,7 @@ public static function getOrderSummaryInfolist(): Infolists\Components\Component ]); } - public function infolist(Infolist $infolist): Infolist + public function getDefaultInfolist(Infolist $infolist): Infolist { return $infolist ->schema([ @@ -894,4 +894,4 @@ public function intents(): \Illuminate\Support\Collection { return $this->record->transactions()->whereType('intent')->whereSuccess(true)->get(); } -} \ No newline at end of file +} diff --git a/packages/admin/src/Support/Pages/BaseViewRecord.php b/packages/admin/src/Support/Pages/BaseViewRecord.php index 10f9a98a32..725ad6bbe8 100644 --- a/packages/admin/src/Support/Pages/BaseViewRecord.php +++ b/packages/admin/src/Support/Pages/BaseViewRecord.php @@ -12,6 +12,5 @@ abstract class BaseViewRecord extends ViewRecord use Concerns\ExtendsHeadings; use Concerns\ExtendsInfolist; use \Lunar\Admin\Support\Concerns\CallsHooks; - use \Lunar\Admin\Support\Concerns\CallsHooks; }