diff --git a/CHANGELOG.md b/CHANGELOG.md index df820c6..cfc6cfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). * Moved SVG icons into anonymous components for easier reuse / overwriting.[PR#35](https://github.com/mikebarlow/megaphone/pull/35) * Reworked notification type templates into components. [PR#35](https://github.com/mikebarlow/megaphone/pull/35) +* Added "mark all as read" feature for unread notifications. [PR#37](https://github.com/mikebarlow/megaphone/pull/37) ## [2.0.0] - 2023-09-11 diff --git a/resources/views/popout.blade.php b/resources/views/popout.blade.php index d467ecc..ca51da5 100644 --- a/resources/views/popout.blade.php +++ b/resources/views/popout.blade.php @@ -10,9 +10,15 @@ @if ($unread->count() > 0) -

- Unread Notifications -

+
+

+ Unread Notifications +

+ + @if ($unread->count() > 1) + + @endif +
@foreach ($unread as $announcement)
diff --git a/src/Livewire/Megaphone.php b/src/Livewire/Megaphone.php index 2fcd79b..28c16a9 100644 --- a/src/Livewire/Megaphone.php +++ b/src/Livewire/Megaphone.php @@ -59,4 +59,15 @@ public function markAsRead(DatabaseNotification $notification) $notification->markAsRead(); $this->loadAnnouncements($this->getNotifiable()); } + + public function markAllRead() + { + DatabaseNotification::query() + ->where('notifiable_type', config('megaphone.model')) + ->where('notifiable_id', $this->notifiableId) + ->whereNull('read_at') + ->update(['read_at' => now()]); + + $this->loadAnnouncements($this->getNotifiable()); + } } diff --git a/tests/MegaphoneComponentTest.php b/tests/MegaphoneComponentTest.php index 15836fb..2148eaf 100644 --- a/tests/MegaphoneComponentTest.php +++ b/tests/MegaphoneComponentTest.php @@ -100,6 +100,60 @@ ->assertSet('announcements', $user->readNotifications); }); +it('can mark all notifications as read', function () { + $this->actingAs( + $user = $this->createTestUser() + ); + + $this->createTestNotification( + $user, + \MBarlow\Megaphone\Types\General::class + ); + $this->createTestNotification( + $user, + \MBarlow\Megaphone\Types\Important::class + ); + + $this->livewire(Megaphone::class) + ->call('markAllRead') + ->assertSet('unread', $user->announcements()->get()->whereNull('read_at')) + ->assertSet('announcements', $user->readNotifications); +}); + +it('doesn\'t shows mark all as read if only 1 or less', function () { + $this->actingAs( + $user = $this->createTestUser() + ); + + $this->createTestNotification( + $user, + \MBarlow\Megaphone\Types\General::class + ); + + $this->livewire(Megaphone::class) + ->assertViewIs('megaphone::megaphone') + ->assertDontSeeHtml('