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

Config options for polling #38

Merged
merged 5 commits into from
Sep 10, 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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-20.04]
php: ['8.1', '8.2']
php: ['8.1', '8.2', '8.3']

name: P${{ matrix.php }}
steps:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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)
* Added support for `wire:poll` to give the impression of a live component. [PR#38](https://github.com/mikebarlow/megaphone/pull/38)

## [2.0.0] - 2023-09-11

Expand Down
15 changes: 15 additions & 0 deletions config/megaphone.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,19 @@
* show a dot instead
*/
'showCount' => true,

/*
* Enable Livewire Poll feature for auto updating.
* See livewire docs for poll option descriptions
* @link https://livewire.laravel.com/docs/wire-poll
*/
'poll' => [
'enabled' => false,

'options' => [
'time' => '15s',
'keepAlive' => false,
'viewportVisible' => false,
],
],
];
2 changes: 1 addition & 1 deletion resources/views/megaphone.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="megaphone">
<div class="megaphone" @megaphonePoll>
<div class="relative w-12 h-12" x-data="{ open: false }">
@include('megaphone::icon')
@include('megaphone::popout')
Expand Down
6 changes: 1 addition & 5 deletions src/Livewire/Megaphone.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public function mount(Request $request)
if (empty($this->notifiableId) && $request->user() !== null) {
$this->notifiableId = $request->user()->id;
}

$this->loadAnnouncements($this->getNotifiable());
$this->showCount = config('megaphone.showCount', true);
}

Expand All @@ -51,13 +49,13 @@ public function loadAnnouncements($notifiable)

public function render()
{
$this->loadAnnouncements($this->getNotifiable());
return view('megaphone::megaphone');
}

public function markAsRead(DatabaseNotification $notification)
{
$notification->markAsRead();
$this->loadAnnouncements($this->getNotifiable());
}

public function markAllRead()
Expand All @@ -67,7 +65,5 @@ public function markAllRead()
->where('notifiable_id', $this->notifiableId)
->whereNull('read_at')
->update(['read_at' => now()]);

$this->loadAnnouncements($this->getNotifiable());
}
}
63 changes: 50 additions & 13 deletions src/MegaphoneServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,64 @@

class MegaphoneServiceProvider extends ServiceProvider
{
public function register()
{
$this->registerConfigs();
}

public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
ClearOldNotifications::class,
]);
}
$this->bootBlade();
$this->bootConsole();
$this->bootLivewireComponents();
$this->bootPublishes();
$this->bootViews();
}

$this->loadViewsFrom(__DIR__.'/../resources/views', 'megaphone');
protected function registerConfigs()
{
$this->mergeConfigFrom(
__DIR__.'/../config/megaphone.php',
'megaphone'
);
}

protected function bootBlade()
{
Blade::componentNamespace('MBarlow\\Megaphone\\Components', 'megaphone');

Livewire::component(
'megaphone',
Megaphone::class
Blade::directive(
'megaphonePoll',
function () {
return '<?php
if (config("megaphone.poll.enabled", false)) {
$poll = "wire:poll";
$poll .= (! empty($time = config("megaphone.poll.options.time"))) ? ".$time" : "";
$poll .= (config("megaphone.poll.options.keepAlive", false)) ? ".keep-alive" : "";
$poll .= (config("megaphone.poll.options.viewportVisible", false)) ? ".visible" : "";
echo $poll;
}?>';
}
);
}

Livewire::component(
'megaphone-admin',
MegaphoneAdmin::class
);
protected function bootConsole()
{
if ($this->app->runningInConsole()) {
$this->commands([
ClearOldNotifications::class,
]);
}
}

protected function bootLivewireComponents()
{
Livewire::component('megaphone', Megaphone::class);
Livewire::component('megaphone-admin', MegaphoneAdmin::class);
}

protected function bootPublishes()
{
$this->publishes([
__DIR__.'/../public' => public_path('vendor/megaphone'),
__DIR__.'/../config/megaphone.php' => config_path('megaphone.php'),
Expand All @@ -55,4 +87,9 @@ public function boot()
__DIR__.'/../resources/views' => resource_path('views/vendor/megaphone'),
], 'megaphone-views');
}

protected function bootViews()
{
$this->loadViewsFrom(__DIR__.'/../resources/views', 'megaphone');
}
}
Loading