2025-01-17 23:03:34 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Livewire;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Contracts\View\View;
|
2025-03-28 23:50:34 +01:00
|
|
|
use Livewire\Attributes\On;
|
2025-01-17 23:03:34 +01:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
|
|
class AlertBannerContainer extends Component
|
|
|
|
|
{
|
2026-02-15 15:34:09 -05:00
|
|
|
public AlertBannerCollection $alertBanners;
|
2025-01-17 23:03:34 +01:00
|
|
|
|
|
|
|
|
public function mount(): void
|
|
|
|
|
{
|
2026-02-15 15:34:09 -05:00
|
|
|
$this->alertBanners = new AlertBannerCollection();
|
|
|
|
|
|
|
|
|
|
foreach (session()->pull('alert-banners', []) as $alertBanner) {
|
|
|
|
|
// Alerts created during Livewire requests should have been consumed by the event handler on the same page.
|
|
|
|
|
if (!empty($alertBanner['from_livewire'])) {
|
|
|
|
|
// If they weren't, then discard them instead of showing on the wrong page.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$alertBanner = AlertBanner::fromArray($alertBanner);
|
|
|
|
|
$this->alertBanners->put($alertBanner->getId(), $alertBanner);
|
|
|
|
|
}
|
2025-01-17 23:03:34 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-28 23:50:34 +01:00
|
|
|
#[On('alertBannerSent')]
|
2025-01-17 23:03:34 +01:00
|
|
|
public function pullFromSession(): void
|
|
|
|
|
{
|
|
|
|
|
foreach (session()->pull('alert-banners', []) as $alertBanner) {
|
2026-02-15 15:34:09 -05:00
|
|
|
unset($alertBanner['from_livewire']);
|
2025-07-31 23:54:53 +02:00
|
|
|
$alertBanner = AlertBanner::fromArray($alertBanner);
|
|
|
|
|
$this->alertBanners->put($alertBanner->getId(), $alertBanner);
|
2025-01-17 23:03:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function remove(string $id): void
|
|
|
|
|
{
|
2025-07-31 23:54:53 +02:00
|
|
|
if ($this->alertBanners->has($id)) {
|
|
|
|
|
$this->alertBanners->forget($id);
|
|
|
|
|
}
|
2025-01-17 23:03:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function render(): View
|
|
|
|
|
{
|
|
|
|
|
return view('livewire.alerts.alert-banner-container');
|
|
|
|
|
}
|
|
|
|
|
}
|