From e3415d358b295eedaf7ac3dbc825073c1c5e32ea Mon Sep 17 00:00:00 2001 From: Faizal Nugraha Date: Fri, 20 Sep 2024 14:39:30 +0700 Subject: [PATCH] ref: ModelActivity widget --- app/Filament/Resources/ActivityResource.php | 3 +- .../Widgets/ModelActivity.php | 30 +++++++++---------- .../Resources/RoleResource/Pages/EditRole.php | 2 +- .../Resources/RoleResource/Pages/ViewRole.php | 2 +- .../Resources/UserResource/Pages/EditUser.php | 2 +- .../Resources/UserResource/Pages/ViewUser.php | 2 +- 6 files changed, 21 insertions(+), 20 deletions(-) diff --git a/app/Filament/Resources/ActivityResource.php b/app/Filament/Resources/ActivityResource.php index 8259168..cdb849f 100644 --- a/app/Filament/Resources/ActivityResource.php +++ b/app/Filament/Resources/ActivityResource.php @@ -93,7 +93,8 @@ public static function table(Table $table): Table return __('To: :date', ['date' => $data['to']]); }), - ], layout: Tables\Enums\FiltersLayout::AboveContent); + ], layout: Tables\Enums\FiltersLayout::AboveContent) + ->paginationPageOptions([10, 25, 50, 100]); } public static function getPages(): array diff --git a/app/Filament/Resources/ActivityResource/Widgets/ModelActivity.php b/app/Filament/Resources/ActivityResource/Widgets/ModelActivity.php index f9aec6f..39eac51 100644 --- a/app/Filament/Resources/ActivityResource/Widgets/ModelActivity.php +++ b/app/Filament/Resources/ActivityResource/Widgets/ModelActivity.php @@ -12,8 +12,8 @@ class ModelActivity extends BaseWidget { + public ?Model $record = null; public ?Model $causer = null; - public ?Model $subject = null; public static function canView(): bool { @@ -26,24 +26,23 @@ public function getHeading(): ?string return __('User Activity'); } - if ($this->subject) { - return str(class_basename($this->subject)) - ->headline() - ->append(__(' History')); - } - - return null; + return str(class_basename($this->record)) + ->headline() + ->append(__(' History')); } public function table(Table $table): Table { $query = Activity::query() - ->when($this->causer, function ($q, $causer) { - $q->causedBy($causer); - }) - ->when($this->subject, function ($q, $subject) { - $q->forSubject($subject); - }); + ->when( + $this->causer, + function ($q, $causer) { + $q->causedBy($causer); + }, + function ($q) { + $q->forSubject($this->record); + } + ); return $table ->heading($this->getHeading()) @@ -69,6 +68,7 @@ public function table(Table $table): Table ->hidden(fn(Model $model) => (new $model->subject_type) instanceof Pivot) ->extraAttributes(['class' => 'overflow-x-auto']) ->collapsible(), - ]); + ]) + ->paginationPageOptions([10, 25, 50, 100]); } } diff --git a/app/Filament/Resources/RoleResource/Pages/EditRole.php b/app/Filament/Resources/RoleResource/Pages/EditRole.php index bd4adc5..cdd331a 100644 --- a/app/Filament/Resources/RoleResource/Pages/EditRole.php +++ b/app/Filament/Resources/RoleResource/Pages/EditRole.php @@ -22,7 +22,7 @@ protected function getHeaderActions(): array protected function getFooterWidgets(): array { return [ - ActivityResource\Widgets\ModelActivity::make(['subject' => $this->record]), + ActivityResource\Widgets\ModelActivity::make(), ]; } } diff --git a/app/Filament/Resources/RoleResource/Pages/ViewRole.php b/app/Filament/Resources/RoleResource/Pages/ViewRole.php index b9bd3eb..ea8c477 100644 --- a/app/Filament/Resources/RoleResource/Pages/ViewRole.php +++ b/app/Filament/Resources/RoleResource/Pages/ViewRole.php @@ -22,7 +22,7 @@ protected function getHeaderActions(): array protected function getFooterWidgets(): array { return [ - ActivityResource\Widgets\ModelActivity::make(['subject' => $this->record]), + ActivityResource\Widgets\ModelActivity::make(), ]; } } diff --git a/app/Filament/Resources/UserResource/Pages/EditUser.php b/app/Filament/Resources/UserResource/Pages/EditUser.php index 935c367..0b605a6 100644 --- a/app/Filament/Resources/UserResource/Pages/EditUser.php +++ b/app/Filament/Resources/UserResource/Pages/EditUser.php @@ -23,7 +23,7 @@ protected function getFooterWidgets(): array { return [ ActivityResource\Widgets\ModelActivity::make(['causer' => $this->record]), - ActivityResource\Widgets\ModelActivity::make(['subject' => $this->record]), + ActivityResource\Widgets\ModelActivity::make(), ]; } } diff --git a/app/Filament/Resources/UserResource/Pages/ViewUser.php b/app/Filament/Resources/UserResource/Pages/ViewUser.php index 72e7eb7..9db85ad 100644 --- a/app/Filament/Resources/UserResource/Pages/ViewUser.php +++ b/app/Filament/Resources/UserResource/Pages/ViewUser.php @@ -24,7 +24,7 @@ protected function getFooterWidgets(): array { return [ ActivityResource\Widgets\ModelActivity::make(['causer' => $this->record]), - ActivityResource\Widgets\ModelActivity::make(['subject' => $this->record]), + ActivityResource\Widgets\ModelActivity::make(), ]; } }