Skip to content

Commit

Permalink
Merge pull request #14157 from leandrocfe/db-notifications-mark-all-a…
Browse files Browse the repository at this point in the history
…s-read

Database Notifications - Customizable "Mark All as Read" Action
  • Loading branch information
danharrin authored Feb 2, 2025
2 parents d0d020c + 57a5b51 commit aadbff7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
@props([
'notifications',
'unreadNotificationsCount',
'clearNotificationsAction',
'markAllNotificationsAsReadAction',
])

<div {{ $attributes->class('mt-2 flex gap-x-3') }}>
@if ($unreadNotificationsCount)
<x-filament::link
color="primary"
tabindex="-1"
tag="button"
wire:click="markAllNotificationsAsRead"
>
{{ __('filament-notifications::database.modal.actions.mark_all_as_read.label') }}
</x-filament::link>
@if ($unreadNotificationsCount && $markAllNotificationsAsReadAction?->isVisible())
{{ $markAllNotificationsAsReadAction }}
@endif

@if ($clearNotificationsAction?->isVisible())
{{ $clearNotificationsAction }}
@endif

<x-filament::link
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@props([
'notifications',
'unreadNotificationsCount',
'clearNotificationsAction',
'markAllNotificationsAsReadAction',
])

@php
Expand Down Expand Up @@ -33,6 +35,8 @@
<x-filament-notifications::database.modal.actions
:notifications="$notifications"
:unread-notifications-count="$unreadNotificationsCount"
:clear-notifications-action="$clearNotificationsAction"
:mark-all-notifications-as-read-action="$markAllNotificationsAsReadAction"
/>
</div>
</x-slot>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class="flex"
<x-filament-notifications::database.modal
:notifications="$notifications"
:unread-notifications-count="$unreadNotificationsCount"
:clear-notifications-action="$this->clearNotificationsAction"
:mark-all-notifications-as-read-action="$this->markAllNotificationsAsReadAction"
/>

@if ($broadcastChannel = $this->getBroadcastChannel())
Expand Down
29 changes: 28 additions & 1 deletion packages/notifications/src/Livewire/DatabaseNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
namespace Filament\Notifications\Livewire;

use Carbon\CarbonInterface;
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;
Expand All @@ -16,8 +21,10 @@
use Livewire\Component;
use Livewire\WithPagination;

class DatabaseNotifications extends Component
class DatabaseNotifications extends Component implements HasActions, HasForms
{
use InteractsWithActions;
use InteractsWithForms;
use WithPagination;

public static bool $isPaginated = true;
Expand Down Expand Up @@ -113,6 +120,26 @@ public function getTrigger(): ?View
return view($viewPath);
}

public function markAllNotificationsAsReadAction(): Action
{
return Action::make('markAllNotificationsAsRead')
->link()
->label(__('filament-notifications::database.modal.actions.mark_all_as_read.label'))
->extraAttributes(['tabindex' => '-1'])
->action('markAllNotificationsAsRead');
}

public function clearNotificationsAction(): Action
{
return Action::make('clearNotifications')
->link()
->color('danger')
->label(__('filament-notifications::database.modal.actions.clear.label'))
->extraAttributes(['tabindex' => '-1'])
->action('clearNotifications')
->close();
}

public function getUser(): Model | Authenticatable | null
{
return auth(static::$authGuard)->user();
Expand Down
3 changes: 1 addition & 2 deletions packages/panels/src/Panel/Concerns/HasComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Closure;
use Filament\Auth\Pages\EditProfile;
use Filament\Clusters\Cluster;
use Filament\Livewire\DatabaseNotifications;
use Filament\Livewire\GlobalSearch;
use Filament\Livewire\Notifications;
use Filament\Livewire\Sidebar;
Expand Down Expand Up @@ -484,7 +483,7 @@ public function livewireComponents(array $components): static
protected function registerLivewireComponents(): void
{
if (! $this->hasCachedComponents()) {
$this->queueLivewireComponentForRegistration(DatabaseNotifications::class);
$this->queueLivewireComponentForRegistration($this->getDatabaseNotificationsLivewireComponent());
$this->queueLivewireComponentForRegistration(EditProfile::class);
$this->queueLivewireComponentForRegistration(GlobalSearch::class);
$this->queueLivewireComponentForRegistration(Notifications::class);
Expand Down
28 changes: 27 additions & 1 deletion packages/panels/src/Panel/Concerns/HasNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,41 @@
namespace Filament\Panel\Concerns;

use Closure;
use Filament\Livewire\DatabaseNotifications;
use Livewire\Component;

trait HasNotifications
{
protected bool | Closure $hasDatabaseNotifications = false;

protected bool | Closure $hasLazyLoadedDatabaseNotifications = true;

protected string | Closure | null $databaseNotificationsLivewireComponent = null;

protected string | Closure | null $databaseNotificationsPolling = '30s';

public function databaseNotifications(bool | Closure $condition = true, bool | Closure $isLazy = true): static
/**
* @param class-string<Component> | Closure | null $livewireComponent
*/
public function databaseNotifications(bool | Closure $condition = true, string | Closure | null $livewireComponent = null, bool | Closure $isLazy = true): static
{
$this->hasDatabaseNotifications = $condition;
$this->databaseNotificationsLivewireComponent($livewireComponent);
$this->lazyLoadedDatabaseNotifications($isLazy);

return $this;
}

/**
* @param class-string<Component> | Closure | null $component
*/
public function databaseNotificationsLivewireComponent(string | Closure | null $component): static
{
$this->databaseNotificationsLivewireComponent = $component;

return $this;
}

public function lazyLoadedDatabaseNotifications(bool | Closure $condition = true): static
{
$this->hasLazyLoadedDatabaseNotifications = $condition;
Expand All @@ -44,6 +62,14 @@ public function hasLazyLoadedDatabaseNotifications(): bool
return (bool) $this->evaluate($this->hasLazyLoadedDatabaseNotifications);
}

/**
* @return class-string<Component>
*/
public function getDatabaseNotificationsLivewireComponent(): string
{
return $this->evaluate($this->databaseNotificationsLivewireComponent) ?? DatabaseNotifications::class;
}

public function getDatabaseNotificationsPollingInterval(): ?string
{
return $this->evaluate($this->databaseNotificationsPolling);
Expand Down

0 comments on commit aadbff7

Please sign in to comment.