From e4cf5644e0b693f28afa13d0833ee7034891385b Mon Sep 17 00:00:00 2001 From: LT Date: Fri, 29 Nov 2024 19:10:01 +0300 Subject: [PATCH 1/2] feat: Fragment metrics --- src/Laravel/src/Pages/Crud/IndexPage.php | 16 +++++++++++---- src/Laravel/src/Resources/CrudResource.php | 24 +++++++++++++++++++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/Laravel/src/Pages/Crud/IndexPage.php b/src/Laravel/src/Pages/Crud/IndexPage.php index 994ef8565..e4de1b352 100644 --- a/src/Laravel/src/Pages/Crud/IndexPage.php +++ b/src/Laravel/src/Pages/Crud/IndexPage.php @@ -116,10 +116,18 @@ protected function getMetrics(): ?ComponentContract $metrics = $this->getResource()->getMetrics(); - return $metrics - ? Div::make($metrics) - ->class('layout-metrics') - : null; + if($metrics === []) { + return null; + } + + $components = Div::make($metrics)->class('layout-metrics'); + + + if(!\is_null($fragment = $this->getResource()->getFragmentMetrics())) { + return $fragment([$components]); + } + + return $components; } /** diff --git a/src/Laravel/src/Resources/CrudResource.php b/src/Laravel/src/Resources/CrudResource.php index 6ea6e2635..a489c6a1c 100644 --- a/src/Laravel/src/Resources/CrudResource.php +++ b/src/Laravel/src/Resources/CrudResource.php @@ -4,6 +4,8 @@ namespace MoonShine\Laravel\Resources; +use Closure; +use Illuminate\Support\Collection; use MoonShine\Contracts\Core\CrudPageContract; use MoonShine\Contracts\Core\CrudResourceContract; use MoonShine\Contracts\Core\DependencyInjection\FieldsContract; @@ -12,6 +14,7 @@ use MoonShine\Contracts\Core\TypeCasts\DataWrapperContract; use MoonShine\Core\Resources\Resource; use MoonShine\Core\TypeCasts\MixedDataCaster; +use MoonShine\Laravel\Components\Fragment; use MoonShine\Laravel\Pages\Crud\DetailPage; use MoonShine\Laravel\Pages\Crud\FormPage; use MoonShine\Laravel\Pages\Crud\IndexPage; @@ -250,7 +253,22 @@ protected function metrics(): array */ public function getMetrics(): array { - return $this->metrics(); + return Collection::make($this->metrics()) + ->ensure(Metric::class) + ->toArray(); + } + + /** + * @return ?Closure(array $components): Fragment + */ + protected function fragmentMetrics(): ?Closure + { + return null; + } + + public function getFragmentMetrics(): ?Closure + { + return $this->fragmentMetrics(); } /** @@ -279,7 +297,7 @@ public function getListComponentName(): string return rescue( fn (): string => $this->getIndexPage()?->getListComponentName(), "index-table-{$this->getUriKey()}", - false + false, ); } @@ -295,7 +313,7 @@ public function getListEventName(?string $name = null, array $params = []): stri return rescue( fn (): string => AlpineJs::event($this->getIndexPage()?->getListEventName() ?? '', $name, $params), AlpineJs::event(JsEvent::TABLE_UPDATED, $name, $params), - false + false, ); } From 18ccac880ddccd2a57c27422d6db2ec0e26b669b Mon Sep 17 00:00:00 2001 From: lee-to Date: Fri, 29 Nov 2024 16:10:29 +0000 Subject: [PATCH 2/2] Fix styling --- src/Laravel/src/Pages/Crud/IndexPage.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Laravel/src/Pages/Crud/IndexPage.php b/src/Laravel/src/Pages/Crud/IndexPage.php index e4de1b352..069d8307a 100644 --- a/src/Laravel/src/Pages/Crud/IndexPage.php +++ b/src/Laravel/src/Pages/Crud/IndexPage.php @@ -116,14 +116,14 @@ protected function getMetrics(): ?ComponentContract $metrics = $this->getResource()->getMetrics(); - if($metrics === []) { + if ($metrics === []) { return null; } $components = Div::make($metrics)->class('layout-metrics'); - if(!\is_null($fragment = $this->getResource()->getFragmentMetrics())) { + if (! \is_null($fragment = $this->getResource()->getFragmentMetrics())) { return $fragment([$components]); }