From 6d278e38fb9107cd23488486d506c49b425848e5 Mon Sep 17 00:00:00 2001 From: "Leandro C. Ferreira" Date: Thu, 5 Sep 2024 10:19:23 -0300 Subject: [PATCH 1/8] docs --- packages/panels/docs/06-notifications.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/panels/docs/06-notifications.md b/packages/panels/docs/06-notifications.md index 107dfe10b10..9f4a20da62a 100644 --- a/packages/panels/docs/06-notifications.md +++ b/packages/panels/docs/06-notifications.md @@ -33,6 +33,27 @@ public function panel(Panel $panel): Panel } ``` +## Customizing the "Mark All as Read" Action + +To customize the "Mark All as Read" button, you can use the `databaseNotificationsMarkAllAsReadAction()` method, passing a closure that returns an action. All methods that are available to [customize action trigger buttons](../../actions/trigger-button) can be used: + +```php +use Filament\Actions\Action; +use Filament\Panel; + +public function panel(Panel $panel): Panel +{ + return $panel + // ... + ->databaseNotifications() + ->databaseNotificationsMarkAllAsReadAction(fn (Action $action) => $action + ->button() + ->color('success') + ->label('Read All') + ); +} +``` + ## Setting up websockets in a panel The Panel Builder comes with a level of inbuilt support for real-time broadcast and database notifications. However there are a number of areas you will need to install and configure to wire everything up and get it working. From a554061298453a0bc1af3686462a2066853513e6 Mon Sep 17 00:00:00 2001 From: "Leandro C. Ferreira" Date: Thu, 5 Sep 2024 10:40:12 -0300 Subject: [PATCH 2/8] These changes introduce the databaseNotificationsMarkAllAsReadAction() method, which allows customization of the Mark All as Read action within the database notifications modal. --- .../database/modal/actions.blade.php | 12 +++------ .../components/database/modal/index.blade.php | 2 ++ .../views/database-notifications.blade.php | 2 ++ .../src/Livewire/DatabaseNotifications.php | 8 ++++++ packages/panels/src/Facades/Filament.php | 1 + packages/panels/src/FilamentManager.php | 5 ++++ .../src/Livewire/DatabaseNotifications.php | 6 +++++ .../src/Panel/Concerns/HasNotifications.php | 27 +++++++++++++++++++ 8 files changed, 54 insertions(+), 9 deletions(-) diff --git a/packages/notifications/resources/views/components/database/modal/actions.blade.php b/packages/notifications/resources/views/components/database/modal/actions.blade.php index 2f058146aaf..f1e40cc0655 100644 --- a/packages/notifications/resources/views/components/database/modal/actions.blade.php +++ b/packages/notifications/resources/views/components/database/modal/actions.blade.php @@ -1,18 +1,12 @@ @props([ 'notifications', 'unreadNotificationsCount', + 'markAllNotificationsAsReadAction' ])
class('mt-2 flex gap-x-3') }}> - @if ($unreadNotificationsCount) - - {{ __('filament-notifications::database.modal.actions.mark_all_as_read.label') }} - + @if ($unreadNotificationsCount && $markAllNotificationsAsReadAction) + {{ ($markAllNotificationsAsReadAction)->isVisible() ? $markAllNotificationsAsReadAction : '' }} @endif
diff --git a/packages/notifications/resources/views/database-notifications.blade.php b/packages/notifications/resources/views/database-notifications.blade.php index c03b24e842e..b3e3bfdc51f 100644 --- a/packages/notifications/resources/views/database-notifications.blade.php +++ b/packages/notifications/resources/views/database-notifications.blade.php @@ -1,6 +1,7 @@ @php $notifications = $this->getNotifications(); $unreadNotificationsCount = $this->getUnreadNotificationsCount(); + $markAllNotificationsAsReadAction = $this->getMarkAllNotificationsAsReadAction(); @endphp
@if ($broadcastChannel = $this->getBroadcastChannel()) diff --git a/packages/notifications/src/Livewire/DatabaseNotifications.php b/packages/notifications/src/Livewire/DatabaseNotifications.php index 0290bd8f987..4092c4c2027 100644 --- a/packages/notifications/src/Livewire/DatabaseNotifications.php +++ b/packages/notifications/src/Livewire/DatabaseNotifications.php @@ -3,6 +3,7 @@ namespace Filament\Notifications\Livewire; use Carbon\CarbonInterface; +use Filament\Actions\Action; use Filament\Notifications\Notification; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Contracts\Pagination\Paginator; @@ -28,6 +29,8 @@ class DatabaseNotifications extends Component public static ?string $authGuard = null; + public static ?Action $markAllNotificationsAsReadAction = null; + #[On('databaseNotificationsSent')] public function refresh(): void {} @@ -113,6 +116,11 @@ public function getTrigger(): ?View return view($viewPath); } + public function getMarkAllNotificationsAsReadAction(): Action + { + return static::$markAllNotificationsAsReadAction; + } + public function getUser(): Model | Authenticatable | null { return auth(static::$authGuard)->user(); diff --git a/packages/panels/src/Facades/Filament.php b/packages/panels/src/Facades/Filament.php index a0c3bed23e5..09c6fdc627a 100644 --- a/packages/panels/src/Facades/Filament.php +++ b/packages/panels/src/Facades/Filament.php @@ -105,6 +105,7 @@ * @method static string | null getUrl(Model | null $tenant = null) * @method static string getVerifyEmailUrl(MustVerifyEmail | Model | Authenticatable $user, array $parameters = []) * @method static array getWidgets() + * @method static Action getDatabaseNotificationsMarkAllAsReadAction() * @method static bool hasBreadcrumbs() * @method static bool hasCollapsibleNavigationGroups() * @method static bool hasDarkMode() diff --git a/packages/panels/src/FilamentManager.php b/packages/panels/src/FilamentManager.php index 777e27a0c99..1f586074a9e 100644 --- a/packages/panels/src/FilamentManager.php +++ b/packages/panels/src/FilamentManager.php @@ -589,6 +589,11 @@ public function getWidgets(): array return $this->getCurrentPanel()->getWidgets(); } + public function getDatabaseNotificationsMarkAllAsReadAction(): Action + { + return $this->currentPanel->getDatabaseNotificationsMarkAllAsReadAction(); + } + public function hasBreadcrumbs(): bool { return $this->getCurrentPanel()->hasBreadcrumbs(); diff --git a/packages/panels/src/Livewire/DatabaseNotifications.php b/packages/panels/src/Livewire/DatabaseNotifications.php index 8ce0e9d67b8..dd4bb155bb5 100644 --- a/packages/panels/src/Livewire/DatabaseNotifications.php +++ b/packages/panels/src/Livewire/DatabaseNotifications.php @@ -2,6 +2,7 @@ namespace Filament\Livewire; +use Filament\Actions\Action; use Filament\Facades\Filament; use Filament\Notifications\Livewire\DatabaseNotifications as BaseComponent; use Illuminate\Contracts\Auth\Authenticatable; @@ -24,4 +25,9 @@ public function getTrigger(): View { return view('filament-panels::components.topbar.database-notifications-trigger'); } + + public function getMarkAllNotificationsAsReadAction(): Action + { + return Filament::getDatabaseNotificationsMarkAllAsReadAction(); + } } diff --git a/packages/panels/src/Panel/Concerns/HasNotifications.php b/packages/panels/src/Panel/Concerns/HasNotifications.php index a8686661707..1228f698632 100644 --- a/packages/panels/src/Panel/Concerns/HasNotifications.php +++ b/packages/panels/src/Panel/Concerns/HasNotifications.php @@ -3,6 +3,7 @@ namespace Filament\Panel\Concerns; use Closure; +use Filament\Actions\Action; trait HasNotifications { @@ -10,6 +11,8 @@ trait HasNotifications protected string | Closure | null $databaseNotificationsPolling = '30s'; + protected ?Closure $modifyDatabaseNotificationsMarkAllAsReadUsing = null; + public function databaseNotifications(bool | Closure $condition = true): static { $this->hasDatabaseNotifications = $condition; @@ -24,6 +27,13 @@ public function databaseNotificationsPolling(string | Closure | null $interval): return $this; } + public function databaseNotificationsMarkAllAsReadAction(?Closure $callback): static + { + $this->modifyDatabaseNotificationsMarkAllAsReadUsing = $callback; + + return $this; + } + public function hasDatabaseNotifications(): bool { return (bool) $this->evaluate($this->hasDatabaseNotifications); @@ -33,4 +43,21 @@ public function getDatabaseNotificationsPollingInterval(): ?string { return $this->evaluate($this->databaseNotificationsPolling); } + + public function getDatabaseNotificationsMarkAllAsReadAction(): Action + { + $action = Action::make('markAllNotificationsAsRead') + ->link() + ->label(__('filament-notifications::database.modal.actions.mark_all_as_read.label')) + ->extraAttributes(['tabindex' => '-1']) + ->action('markAllNotificationsAsRead'); + + if ($this->modifyDatabaseNotificationsMarkAllAsReadUsing) { + $action = $this->evaluate($this->modifyDatabaseNotificationsMarkAllAsReadUsing, [ + 'action' => $action, + ]) ?? $action; + } + + return $action; + } } From 364d1c8b5b43efcb98cb77e74ec6ad2afe872e28 Mon Sep 17 00:00:00 2001 From: "Leandro C. Ferreira" Date: Thu, 5 Sep 2024 11:57:02 -0300 Subject: [PATCH 3/8] Mark All as Read action within the database notifications modal. --- .../src/Livewire/DatabaseNotifications.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/notifications/src/Livewire/DatabaseNotifications.php b/packages/notifications/src/Livewire/DatabaseNotifications.php index 4092c4c2027..3d089bb0cc5 100644 --- a/packages/notifications/src/Livewire/DatabaseNotifications.php +++ b/packages/notifications/src/Livewire/DatabaseNotifications.php @@ -118,10 +118,18 @@ public function getTrigger(): ?View public function getMarkAllNotificationsAsReadAction(): Action { - return static::$markAllNotificationsAsReadAction; + if ($action = static::$markAllNotificationsAsReadAction) { + return $action; + } + + return Action::make('markAllNotificationsAsRead') + ->link() + ->label(__('filament-notifications::database.modal.actions.mark_all_as_read.label')) + ->extraAttributes(['tabindex' => '-1']) + ->action('markAllNotificationsAsRead'); } - public function getUser(): Model | Authenticatable | null + public function getUser(): Model|Authenticatable|null { return auth(static::$authGuard)->user(); } From ccbf56dc3202cfb52d48d4acd730da6216ccac57 Mon Sep 17 00:00:00 2001 From: leandrocfe Date: Thu, 5 Sep 2024 15:07:50 +0000 Subject: [PATCH 4/8] chore: fix code style --- .../resources/views/components/database/modal/actions.blade.php | 2 +- .../resources/views/components/database/modal/index.blade.php | 2 +- packages/notifications/src/Livewire/DatabaseNotifications.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/notifications/resources/views/components/database/modal/actions.blade.php b/packages/notifications/resources/views/components/database/modal/actions.blade.php index f1e40cc0655..fa5e62347be 100644 --- a/packages/notifications/resources/views/components/database/modal/actions.blade.php +++ b/packages/notifications/resources/views/components/database/modal/actions.blade.php @@ -1,7 +1,7 @@ @props([ 'notifications', 'unreadNotificationsCount', - 'markAllNotificationsAsReadAction' + 'markAllNotificationsAsReadAction', ])
class('mt-2 flex gap-x-3') }}> diff --git a/packages/notifications/resources/views/components/database/modal/index.blade.php b/packages/notifications/resources/views/components/database/modal/index.blade.php index 2664f6bc9c4..f569e2fd099 100644 --- a/packages/notifications/resources/views/components/database/modal/index.blade.php +++ b/packages/notifications/resources/views/components/database/modal/index.blade.php @@ -1,7 +1,7 @@ @props([ 'notifications', 'unreadNotificationsCount', - 'markAllNotificationsAsReadAction' + 'markAllNotificationsAsReadAction', ]) @php diff --git a/packages/notifications/src/Livewire/DatabaseNotifications.php b/packages/notifications/src/Livewire/DatabaseNotifications.php index 3d089bb0cc5..ac5735e12b7 100644 --- a/packages/notifications/src/Livewire/DatabaseNotifications.php +++ b/packages/notifications/src/Livewire/DatabaseNotifications.php @@ -129,7 +129,7 @@ public function getMarkAllNotificationsAsReadAction(): Action ->action('markAllNotificationsAsRead'); } - public function getUser(): Model|Authenticatable|null + public function getUser(): Model | Authenticatable | null { return auth(static::$authGuard)->user(); } From e89eae3c27fa437f879c0fa0830e099cc91fa3b3 Mon Sep 17 00:00:00 2001 From: Leandro Ferreira Date: Wed, 4 Dec 2024 11:23:20 -0300 Subject: [PATCH 5/8] rename action --- .../resources/views/database-notifications.blade.php | 2 +- .../notifications/src/Livewire/DatabaseNotifications.php | 7 +++++-- packages/panels/src/Livewire/DatabaseNotifications.php | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/notifications/resources/views/database-notifications.blade.php b/packages/notifications/resources/views/database-notifications.blade.php index b3e3bfdc51f..061c5a8f44b 100644 --- a/packages/notifications/resources/views/database-notifications.blade.php +++ b/packages/notifications/resources/views/database-notifications.blade.php @@ -1,7 +1,7 @@ @php $notifications = $this->getNotifications(); $unreadNotificationsCount = $this->getUnreadNotificationsCount(); - $markAllNotificationsAsReadAction = $this->getMarkAllNotificationsAsReadAction(); + $markAllNotificationsAsReadAction = $this->markAllNotificationsAsReadAction(); @endphp
Date: Wed, 4 Dec 2024 14:32:07 +0000 Subject: [PATCH 6/8] chore: fix code style --- packages/notifications/src/Livewire/DatabaseNotifications.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/notifications/src/Livewire/DatabaseNotifications.php b/packages/notifications/src/Livewire/DatabaseNotifications.php index a69fd40a1fa..15025caad11 100644 --- a/packages/notifications/src/Livewire/DatabaseNotifications.php +++ b/packages/notifications/src/Livewire/DatabaseNotifications.php @@ -21,8 +21,8 @@ class DatabaseNotifications extends Component implements HasActions { - use WithPagination; use InteractsWithActions; + use WithPagination; public static bool $isPaginated = true; From d71eb2d8e38aad7b213581a10768bc9842a0f20a Mon Sep 17 00:00:00 2001 From: Leandro Ferreira Date: Mon, 9 Dec 2024 09:30:33 -0300 Subject: [PATCH 7/8] render the action in the view --- .../resources/views/database-notifications.blade.php | 3 +-- .../notifications/src/Livewire/DatabaseNotifications.php | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/notifications/resources/views/database-notifications.blade.php b/packages/notifications/resources/views/database-notifications.blade.php index 061c5a8f44b..758658be8e2 100644 --- a/packages/notifications/resources/views/database-notifications.blade.php +++ b/packages/notifications/resources/views/database-notifications.blade.php @@ -1,7 +1,6 @@ @php $notifications = $this->getNotifications(); $unreadNotificationsCount = $this->getUnreadNotificationsCount(); - $markAllNotificationsAsReadAction = $this->markAllNotificationsAsReadAction(); @endphp
@if ($broadcastChannel = $this->getBroadcastChannel()) diff --git a/packages/notifications/src/Livewire/DatabaseNotifications.php b/packages/notifications/src/Livewire/DatabaseNotifications.php index 15025caad11..0bc94385928 100644 --- a/packages/notifications/src/Livewire/DatabaseNotifications.php +++ b/packages/notifications/src/Livewire/DatabaseNotifications.php @@ -6,6 +6,8 @@ use Filament\Actions\Action; use Filament\Actions\Concerns\InteractsWithActions; use Filament\Actions\Contracts\HasActions; +use Filament\Forms\Concerns\InteractsWithForms; +use Filament\Forms\Contracts\HasForms; use Filament\Notifications\Notification; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Contracts\Pagination\Paginator; @@ -19,9 +21,10 @@ use Livewire\Component; use Livewire\WithPagination; -class DatabaseNotifications extends Component implements HasActions +class DatabaseNotifications extends Component implements HasForms, HasActions { use InteractsWithActions; + use InteractsWithForms; use WithPagination; public static bool $isPaginated = true; From 5dca5ede194c9e9059553c1bd9376e386834ce4b Mon Sep 17 00:00:00 2001 From: leandrocfe Date: Mon, 9 Dec 2024 12:41:08 +0000 Subject: [PATCH 8/8] chore: fix code style --- packages/notifications/src/Livewire/DatabaseNotifications.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/notifications/src/Livewire/DatabaseNotifications.php b/packages/notifications/src/Livewire/DatabaseNotifications.php index 0bc94385928..4d0e14c533d 100644 --- a/packages/notifications/src/Livewire/DatabaseNotifications.php +++ b/packages/notifications/src/Livewire/DatabaseNotifications.php @@ -21,7 +21,7 @@ use Livewire\Component; use Livewire\WithPagination; -class DatabaseNotifications extends Component implements HasForms, HasActions +class DatabaseNotifications extends Component implements HasActions, HasForms { use InteractsWithActions; use InteractsWithForms;