diff --git a/src/Laravel/src/Components/Layout/Notifications.php b/src/Laravel/src/Components/Layout/Notifications.php index f14f5f329..fa45febfa 100644 --- a/src/Laravel/src/Components/Layout/Notifications.php +++ b/src/Laravel/src/Components/Layout/Notifications.php @@ -5,7 +5,7 @@ namespace MoonShine\Laravel\Components\Layout; use Illuminate\Support\Collection; -use MoonShine\Laravel\Notifications\MoonShineNotificationContract; +use MoonShine\Laravel\Contracts\Notifications\MoonShineNotificationContract; use MoonShine\UI\Components\MoonShineComponent; final class Notifications extends MoonShineComponent @@ -26,7 +26,6 @@ public function __construct() { parent::__construct(); - $this->notificationService = $this->getCore() ->getContainer(MoonShineNotificationContract::class); diff --git a/src/Laravel/src/Notifications/MoonShineNotificationContract.php b/src/Laravel/src/Contracts/Notifications/MoonShineNotificationContract.php similarity index 81% rename from src/Laravel/src/Notifications/MoonShineNotificationContract.php rename to src/Laravel/src/Contracts/Notifications/MoonShineNotificationContract.php index c8dc8e451..a359f02d3 100644 --- a/src/Laravel/src/Notifications/MoonShineNotificationContract.php +++ b/src/Laravel/src/Contracts/Notifications/MoonShineNotificationContract.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace MoonShine\Laravel\Notifications; +namespace MoonShine\Laravel\Contracts\Notifications; use Illuminate\Support\Collection; use MoonShine\Support\Enums\Color; @@ -13,12 +13,11 @@ interface MoonShineNotificationContract { /** - * @param array{}|array{'link': string, 'label': string} $button * @param array $ids */ public function notify( string $message, - array $button = [], + ?NotificationButtonContract $button = null, array $ids = [], string|Color|null $color = null ): void; diff --git a/src/Laravel/src/Contracts/Notifications/NotificationButtonContract.php b/src/Laravel/src/Contracts/Notifications/NotificationButtonContract.php new file mode 100644 index 000000000..d69ab469f --- /dev/null +++ b/src/Laravel/src/Contracts/Notifications/NotificationButtonContract.php @@ -0,0 +1,12 @@ +color = $this->color instanceof Color ? $this->color->value : $this->color; @@ -38,7 +34,10 @@ public function toArray($notifiable): array { return [ 'message' => $this->message, - 'button' => $this->button, + 'button' => !is_null($this->button) ? [ + 'label' => $this->button->getLabel(), + 'link' => $this->button->getLink(), + ] : [], 'color' => $this->color, ]; } diff --git a/src/Laravel/src/Notifications/MoonShineMemoryNotification.php b/src/Laravel/src/Notifications/MoonShineMemoryNotification.php index 802662fc6..501ab4b0f 100644 --- a/src/Laravel/src/Notifications/MoonShineMemoryNotification.php +++ b/src/Laravel/src/Notifications/MoonShineMemoryNotification.php @@ -6,6 +6,8 @@ use Illuminate\Support\Collection; use Illuminate\Support\Str; +use MoonShine\Laravel\Contracts\Notifications\MoonShineNotificationContract; +use MoonShine\Laravel\Contracts\Notifications\NotificationButtonContract; use MoonShine\Support\Enums\Color; /** @@ -16,12 +18,11 @@ final class MoonShineMemoryNotification implements MoonShineNotificationContract private array $messages = []; /** - * @param array{}|array{'link': string, 'label': string} $button * @param array $ids */ public static function send( string $message, - array $button = [], + ?NotificationButtonContract $button = null, array $ids = [], string|Color|null $color = null, ): void { @@ -29,12 +30,11 @@ public static function send( } /** - * @param array{}|array{'link': string, 'label': string} $button * @param array $ids */ public function notify( string $message, - array $button = [], + ?NotificationButtonContract $button = null, array $ids = [], string|Color|null $color = null, ): void { diff --git a/src/Laravel/src/Notifications/MoonShineNotification.php b/src/Laravel/src/Notifications/MoonShineNotification.php index abdaa3c9d..a58a40efc 100644 --- a/src/Laravel/src/Notifications/MoonShineNotification.php +++ b/src/Laravel/src/Notifications/MoonShineNotification.php @@ -8,6 +8,8 @@ use Illuminate\Notifications\DatabaseNotificationCollection; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Notification; +use MoonShine\Laravel\Contracts\Notifications\MoonShineNotificationContract; +use MoonShine\Laravel\Contracts\Notifications\NotificationButtonContract; use MoonShine\Laravel\MoonShineAuth; use MoonShine\Support\Enums\Color; @@ -17,12 +19,11 @@ final class MoonShineNotification implements MoonShineNotificationContract { /** - * @param array{}|array{'link': string, 'label': string} $button * @param array $ids */ public static function send( string $message, - array $button = [], + ?NotificationButtonContract $button = null, array $ids = [], string|Color|null $color = null ): void { @@ -30,12 +31,11 @@ public static function send( } /** - * @param array{}|array{'link': string, 'label': string} $button * @param array $ids */ public function notify( string $message, - array $button = [], + ?NotificationButtonContract $button = null, array $ids = [], string|Color|null $color = null ): void { @@ -55,7 +55,7 @@ public function notify( ) ) ->get(), - DatabaseNotification::make( + new DatabaseNotification( $message, $button, $color diff --git a/src/Laravel/src/Notifications/NotificationButton.php b/src/Laravel/src/Notifications/NotificationButton.php new file mode 100644 index 000000000..c5acfea69 --- /dev/null +++ b/src/Laravel/src/Notifications/NotificationButton.php @@ -0,0 +1,28 @@ +label; + } + + public function getLink(): string + { + return $this->link; + } +} diff --git a/src/Laravel/src/Notifications/NotificationItem.php b/src/Laravel/src/Notifications/NotificationItem.php index 37fa2b96c..7b8cd45bf 100644 --- a/src/Laravel/src/Notifications/NotificationItem.php +++ b/src/Laravel/src/Notifications/NotificationItem.php @@ -6,6 +6,8 @@ use DateTimeInterface; use Illuminate\Notifications\DatabaseNotification; +use MoonShine\Laravel\Contracts\Notifications\NotificationButtonContract; +use MoonShine\Laravel\Contracts\Notifications\NotificationItemContract; final readonly class NotificationItem implements NotificationItemContract { @@ -39,19 +41,16 @@ public function getDate(): DateTimeInterface return $this->notification->created_at ?? now(); } - public function getButton(): array + public function getButton(): ?NotificationButtonContract { - return $this->notification->data['button'] ?? []; - } + if(empty($this->notification->data['button'])) { + return null; + } - public function getButtonLink(): ?string - { - return data_get($this->getButton(), 'link'); - } - - public function getButtonLabel(): ?string - { - return data_get($this->getButton(), 'label'); + return new NotificationButton( + $this->notification->data['button']['label'], + $this->notification->data['button']['link'], + ); } public function getIcon(): string diff --git a/src/Laravel/src/Notifications/NotificationMemoryItem.php b/src/Laravel/src/Notifications/NotificationMemoryItem.php index de96b7b14..cb715155d 100644 --- a/src/Laravel/src/Notifications/NotificationMemoryItem.php +++ b/src/Laravel/src/Notifications/NotificationMemoryItem.php @@ -5,6 +5,8 @@ namespace MoonShine\Laravel\Notifications; use DateTimeInterface; +use MoonShine\Laravel\Contracts\Notifications\NotificationButtonContract; +use MoonShine\Laravel\Contracts\Notifications\NotificationItemContract; final readonly class NotificationMemoryItem implements NotificationItemContract { @@ -13,7 +15,7 @@ public function __construct( private ?string $message, private ?string $color = null, private ?DateTimeInterface $date = null, - private array $button = [], + private ?NotificationButtonContract $button = null, ) { } @@ -42,19 +44,9 @@ public function getDate(): DateTimeInterface return $this->date ?? now(); } - public function getButton(): array + public function getButton(): ?NotificationButtonContract { - return $this->button ?? []; - } - - public function getButtonLink(): ?string - { - return data_get($this->getButton(), 'link'); - } - - public function getButtonLabel(): ?string - { - return data_get($this->getButton(), 'label'); + return $this->button; } public function getIcon(): string diff --git a/src/Laravel/src/Providers/MoonShineServiceProvider.php b/src/Laravel/src/Providers/MoonShineServiceProvider.php index be85aadbe..1d0668f84 100644 --- a/src/Laravel/src/Providers/MoonShineServiceProvider.php +++ b/src/Laravel/src/Providers/MoonShineServiceProvider.php @@ -52,6 +52,7 @@ use MoonShine\Laravel\Commands\MakeTypeCastCommand; use MoonShine\Laravel\Commands\MakeUserCommand; use MoonShine\Laravel\Commands\PublishCommand; +use MoonShine\Laravel\Contracts\Notifications\MoonShineNotificationContract; use MoonShine\Laravel\DependencyInjection\AssetResolver; use MoonShine\Laravel\DependencyInjection\MoonShine; use MoonShine\Laravel\DependencyInjection\MoonShineConfigurator; @@ -65,7 +66,6 @@ use MoonShine\Laravel\MoonShineRequest; use MoonShine\Laravel\Notifications\MoonShineMemoryNotification; use MoonShine\Laravel\Notifications\MoonShineNotification; -use MoonShine\Laravel\Notifications\MoonShineNotificationContract; use MoonShine\Laravel\Resources\ModelResource; use MoonShine\Laravel\Storage\LaravelStorage; use MoonShine\MenuManager\MenuManager; diff --git a/src/Laravel/src/Traits/Controller/InteractsWithUI.php b/src/Laravel/src/Traits/Controller/InteractsWithUI.php index 3c144e6b6..045dc7bf7 100644 --- a/src/Laravel/src/Traits/Controller/InteractsWithUI.php +++ b/src/Laravel/src/Traits/Controller/InteractsWithUI.php @@ -4,6 +4,7 @@ namespace MoonShine\Laravel\Traits\Controller; +use MoonShine\Laravel\Contracts\Notifications\NotificationButtonContract; use MoonShine\Laravel\MoonShineUI; use MoonShine\Support\Enums\Color; use MoonShine\Support\Enums\ToastType; @@ -16,12 +17,11 @@ protected function toast(string $message, ToastType $type = ToastType::INFO): vo } /** - * @param array{}|array{'link': string, 'label': string} $buttons * @param array $ids */ protected function notification( string $message, - array $buttons = [], + ?NotificationButtonContract $buttons = null, array $ids = [], string|Color|null $color = null ): void { diff --git a/src/UI/resources/views/components/layout/notifications.blade.php b/src/UI/resources/views/components/layout/notifications.blade.php index 2f9c3c13d..0b99bfab2 100644 --- a/src/UI/resources/views/components/layout/notifications.blade.php +++ b/src/UI/resources/views/components/layout/notifications.blade.php @@ -37,10 +37,10 @@ class="notifications-remove"

{{ $notification->getMessage() }}

- @if($notification->getButton() !== []) + @if(!is_null($notification->getButton())) @endif