Skip to content

Commit

Permalink
use livewire component with polling for alert banner container
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy132 committed Jan 17, 2025
1 parent 4fc0760 commit a22abb7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 18 deletions.
17 changes: 13 additions & 4 deletions app/Livewire/AlertBanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,21 @@ public function toArray(): array
];
}

public function send(): static
public static function fromArray(array $data): static
{
$alerts = session()->get('alert-banners', []);
$alerts[] = $this->toArray();
$static = static::make();

$static->title($data['title']);
$static->body($data['body']);
$static->status($data['status']);
$static->icon($data['icon']);

return $static;
}

session()->flash('alert-banners', $alerts);
public function send(): static
{
session()->push('alert-banners', $this->toArray());

return $this;
}
Expand Down
27 changes: 27 additions & 0 deletions app/Livewire/AlertBannerContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Livewire;

use Illuminate\Contracts\View\View;
use Livewire\Component;

class AlertBannerContainer extends Component
{
public array $alertBanners;

public function mount(): void
{
$this->alertBanners = [];
$this->pullFromSession();
}

public function pullFromSession(): void
{
$this->alertBanners = array_merge($this->alertBanners, session()->pull('alert-banners', []));
}

public function render(): View
{
return view('livewire.alerts.alert-banner-container');
}
}
4 changes: 2 additions & 2 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ public function boot(Application $app, SoftwareVersionService $versionService):
);

FilamentView::registerRenderHook(
PanelsRenderHook::CONTENT_START,
fn () => view('filament.alerts.alert-banner-container'),
PanelsRenderHook::PAGE_START,
fn () => Blade::render('@livewire(\App\Livewire\AlertBannerContainer::class)'),
);

FilamentView::registerRenderHook(
Expand Down
11 changes: 0 additions & 11 deletions resources/views/filament/alerts/alert-banner-container.blade.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div wire:poll.1s="pullFromSession" id="alert-banner-container" class="flex flex-col gap-4">
@foreach ($alertBanners as $alertBanner)
@include('livewire.alerts.alert-banner', ['alertBanner' => $alertBanner])
@endforeach
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
};
@endphp

<div class="{{$colorClasses}} flex p-4 rounded-xl shadow-lg bg-white dark:bg-gray-900 ring-1 ring-gray-950/5 dark:ring-white/10">
<div class="{{$colorClasses}} flex p-4 mt-3 rounded-xl shadow-lg bg-white dark:bg-gray-900 ring-1 ring-gray-950/5 dark:ring-white/10">
@if (filled($icon))
<x-filament::icon :icon="$icon" class="h-8 w-8 mr-2" color="{{$status}}" />
@endif
Expand Down

0 comments on commit a22abb7

Please sign in to comment.