Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark all read #37

Merged
merged 6 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading