From c8afdeb60bd9dea4e0c67f42e0d4616aaf4bee0d Mon Sep 17 00:00:00 2001 From: Boy132 Date: Fri, 17 Jan 2025 17:56:48 +0100 Subject: [PATCH] cleanup blade file and add "closeable" property --- app/Livewire/AlertBanner.php | 53 ++++++++++++++----- app/Livewire/AlertBannerContainer.php | 3 +- .../livewire/alerts/alert-banner.blade.php | 53 +++++++------------ 3 files changed, 62 insertions(+), 47 deletions(-) diff --git a/app/Livewire/AlertBanner.php b/app/Livewire/AlertBanner.php index 5d5041a402..5c99d4ee63 100644 --- a/app/Livewire/AlertBanner.php +++ b/app/Livewire/AlertBanner.php @@ -2,12 +2,13 @@ namespace App\Livewire; +use Closure; use Filament\Notifications\Concerns; use Filament\Support\Concerns\EvaluatesClosures; -use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Str; +use Livewire\Wireable; -final class AlertBanner implements Arrayable +final class AlertBanner implements Wireable { use Concerns\HasBody; use Concerns\HasIcon; @@ -16,7 +17,9 @@ final class AlertBanner implements Arrayable use Concerns\HasTitle; use EvaluatesClosures; - public static function make(?string $id = null): static + protected bool|Closure $closable = false; + + public static function make(?string $id = null): AlertBanner { $static = new self(); $static->id($id ?? Str::orderedUuid()); @@ -24,7 +27,7 @@ public static function make(?string $id = null): static return $static; } - public function toArray(): array + public function toLivewire(): array { return [ 'id' => $this->getId(), @@ -32,26 +35,50 @@ public function toArray(): array 'body' => $this->getBody(), 'status' => $this->getStatus(), 'icon' => $this->getIcon(), + 'closeable' => $this->isCloseable(), ]; } - public static function fromArray(array $data): static + public static function fromLivewire(mixed $value): AlertBanner { - $static = static::make(); + $static = AlertBanner::make(); - $static->id($data['id']); - $static->title($data['title']); - $static->body($data['body']); - $static->status($data['status']); - $static->icon($data['icon']); + $static->id($value['id']); + $static->title($value['title']); + $static->body($value['body']); + $static->status($value['status']); + $static->icon($value['icon']); + $static->closable($value['closeable']); return $static; } - public function send(): static + public function closable(bool|Closure $closable = true): AlertBanner + { + $this->closable = $closable; + + return $this; + } + + public function isCloseable(): bool { - session()->push('alert-banners', $this->toArray()); + return $this->evaluate($this->closable); + } + + public function send(): AlertBanner + { + session()->push('alert-banners', $this->toLivewire()); return $this; } + + public function getColorClasses(): string + { + return match ($this->getStatus()) { + 'success' => 'text-success-600 dark:text-success-500', + 'warning' => 'text-warning-600 dark:text-warning-500', + 'danger' => 'text-danger-600 dark:text-danger-500', + default => 'text-info-600 dark:text-info-500', + }; + } } diff --git a/app/Livewire/AlertBannerContainer.php b/app/Livewire/AlertBannerContainer.php index 97e267bf73..5b1901c041 100644 --- a/app/Livewire/AlertBannerContainer.php +++ b/app/Livewire/AlertBannerContainer.php @@ -18,7 +18,8 @@ public function mount(): void public function pullFromSession(): void { foreach (session()->pull('alert-banners', []) as $alertBanner) { - $this->alertBanners[$alertBanner['id']] = $alertBanner; + $alertBanner = AlertBanner::fromLivewire($alertBanner); + $this->alertBanners[$alertBanner->getId()] = $alertBanner; } } diff --git a/resources/views/livewire/alerts/alert-banner.blade.php b/resources/views/livewire/alerts/alert-banner.blade.php index 2eba9051ba..68bae27062 100644 --- a/resources/views/livewire/alerts/alert-banner.blade.php +++ b/resources/views/livewire/alerts/alert-banner.blade.php @@ -1,40 +1,27 @@ @props(['alertBanner']) -@isset ($alertBanner) - @php - $status = $alertBanner['status']; +@php + $icon = $alertBanner->getIcon(); + $title = $alertBanner->getTitle(); + $body = $alertBanner->getBody(); +@endphp - $title = $alertBanner['title']; - $body = $alertBanner['body']; +
+ @if (filled($icon)) + + @endif - $icon = $alertBanner['icon'] ?? match ($status) { - "success" => "tabler-circle-check", - "warning" => "tabler-exclamation-circle", - "danger" => "tabler-circle-x", - default => "tabler-info-circle", - }; - - $colorClasses = match ($status) { - "success" => "text-success-600 dark:text-success-500", - "warning" => "text-warning-600 dark:text-warning-500", - "danger" => "text-danger-600 dark:text-danger-500", - default => "text-info-600 dark:text-info-500", - }; - @endphp - -
- @if (filled($icon)) - +
+ @if (filled($title)) +

{{str($title)->sanitizeHtml()->toHtmlString()}}

@endif -
- @if (filled($title)) -

{{str($title)->sanitizeHtml()->toHtmlString()}}

- @endif - - @if (filled($body)) -

{{str($body)->sanitizeHtml()->toHtmlString()}}

- @endif -
+ @if (filled($body)) +

{{str($body)->sanitizeHtml()->toHtmlString()}}

+ @endif
-@endisset + + @if ($alertBanner->isCloseable()) + + @endif +