From f3cb04b6bd188412f232575b96924cdb2785336f Mon Sep 17 00:00:00 2001 From: Boy132 Date: Fri, 17 Jan 2025 15:12:38 +0100 Subject: [PATCH] add id to alert banner --- app/Livewire/AlertBanner.php | 11 +++++++++-- app/Livewire/AlertBannerContainer.php | 9 ++++++++- .../livewire/alerts/alert-banner-container.blade.php | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/Livewire/AlertBanner.php b/app/Livewire/AlertBanner.php index fbed89bc78..5d5041a402 100644 --- a/app/Livewire/AlertBanner.php +++ b/app/Livewire/AlertBanner.php @@ -5,23 +5,29 @@ use Filament\Notifications\Concerns; use Filament\Support\Concerns\EvaluatesClosures; use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Support\Str; final class AlertBanner implements Arrayable { use Concerns\HasBody; use Concerns\HasIcon; + use Concerns\HasId; use Concerns\HasStatus; use Concerns\HasTitle; use EvaluatesClosures; - public static function make(): static + public static function make(?string $id = null): static { - return new self(); + $static = new self(); + $static->id($id ?? Str::orderedUuid()); + + return $static; } public function toArray(): array { return [ + 'id' => $this->getId(), 'title' => $this->getTitle(), 'body' => $this->getBody(), 'status' => $this->getStatus(), @@ -33,6 +39,7 @@ public static function fromArray(array $data): static { $static = static::make(); + $static->id($data['id']); $static->title($data['title']); $static->body($data['body']); $static->status($data['status']); diff --git a/app/Livewire/AlertBannerContainer.php b/app/Livewire/AlertBannerContainer.php index b1894c1b1e..97e267bf73 100644 --- a/app/Livewire/AlertBannerContainer.php +++ b/app/Livewire/AlertBannerContainer.php @@ -17,7 +17,14 @@ public function mount(): void public function pullFromSession(): void { - $this->alertBanners = array_merge($this->alertBanners, session()->pull('alert-banners', [])); + foreach (session()->pull('alert-banners', []) as $alertBanner) { + $this->alertBanners[$alertBanner['id']] = $alertBanner; + } + } + + public function remove(string $id): void + { + unset($this->alertBanners[$id]); } public function render(): View diff --git a/resources/views/livewire/alerts/alert-banner-container.blade.php b/resources/views/livewire/alerts/alert-banner-container.blade.php index 6183756cd4..b8a8402d5e 100644 --- a/resources/views/livewire/alerts/alert-banner-container.blade.php +++ b/resources/views/livewire/alerts/alert-banner-container.blade.php @@ -1,5 +1,5 @@
- @foreach ($alertBanners as $alertBanner) + @foreach (array_values($alertBanners) as $alertBanner) @include('livewire.alerts.alert-banner', ['alertBanner' => $alertBanner]) @endforeach