Skip to content

Commit

Permalink
Merge pull request #37 from mikebarlow/mark-all-read
Browse files Browse the repository at this point in the history
Mark all read
  • Loading branch information
mikebarlow authored Sep 4, 2024
2 parents 820a678 + a0e7984 commit a6fef1f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 9 additions & 3 deletions resources/views/popout.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@
</div>

@if ($unread->count() > 0)
<h2 tabindex="0" class="focus:outline-none text-sm leading-normal pt-8 border-b pb-2 border-gray-300 text-gray-600">
Unread Notifications
</h2>
<div class="border-b pb-2 border-gray-300 text-gray-600 flex justify-between">
<h2 class="focus:outline-none text-sm leading-normal pt-8">
Unread Notifications
</h2>

@if ($unread->count() > 1)
<button class="focus:outline-none text-sm leading-normal pt-8 hover:text-indigo-700" wire:click="markAllRead()">Mark all as read</button>
@endif
</div>

@foreach ($unread as $announcement)
<div class="w-full p-3 mt-4 bg-white rounded flex flex-shrink-0 {{ $announcement->read_at === null ? "drop-shadow shadow border" : "" }}">
Expand Down
11 changes: 11 additions & 0 deletions src/Livewire/Megaphone.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
54 changes: 54 additions & 0 deletions tests/MegaphoneComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<button class="focus:outline-none text-sm leading-normal pt-8 hover:text-indigo-700" wire:click="markAllRead()">Mark all as read</button');
});

it('shows mark all as read if more than 1 notification', 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)
->assertViewIs('megaphone::megaphone')
->assertSeeHtml('<button class="focus:outline-none text-sm leading-normal pt-8 hover:text-indigo-700" wire:click="markAllRead()">Mark all as read</button');
});

it('can render the megaphone component with general notification', function () {
$this->actingAs(
$user = $this->createTestUser()
Expand Down

0 comments on commit a6fef1f

Please sign in to comment.