Skip to content

Commit

Permalink
refactor: NotificationButton
Browse files Browse the repository at this point in the history
BREAKING-CHANGE: Notification contracts moved
  • Loading branch information
lee-to committed Oct 26, 2024
1 parent 41059b8 commit d38d9ed
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 62 deletions.
3 changes: 1 addition & 2 deletions src/Laravel/src/Components/Layout/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,7 +26,6 @@ public function __construct()
{
parent::__construct();


$this->notificationService = $this->getCore()
->getContainer(MoonShineNotificationContract::class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,12 +13,11 @@
interface MoonShineNotificationContract
{
/**
* @param array{}|array{'link': string, 'label': string} $button
* @param array<int|string> $ids
*/
public function notify(
string $message,
array $button = [],
?NotificationButtonContract $button = null,
array $ids = [],
string|Color|null $color = null
): void;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace MoonShine\Laravel\Contracts\Notifications;

interface NotificationButtonContract
{
public function getLink(): string;

public function getLabel(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace MoonShine\Laravel\Notifications;
namespace MoonShine\Laravel\Contracts\Notifications;

use DateTimeInterface;

Expand All @@ -20,12 +20,5 @@ public function getIcon(): string;

public function getDate(): DateTimeInterface;

/**
* @return array{}|array{'link': string, 'label': string}
*/
public function getButton(): array;

public function getButtonLink(): ?string;

public function getButtonLabel(): ?string;
public function getButton(): ?NotificationButtonContract;
}
2 changes: 1 addition & 1 deletion src/Laravel/src/Http/Controllers/CrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests;
use MoonShine\Laravel\Contracts\Notifications\MoonShineNotificationContract;
use MoonShine\Laravel\Http\Requests\MoonShineFormRequest;
use MoonShine\Laravel\Http\Requests\Resources\DeleteFormRequest;
use MoonShine\Laravel\Http\Requests\Resources\MassDeleteFormRequest;
use MoonShine\Laravel\Http\Requests\Resources\StoreFormRequest;
use MoonShine\Laravel\Http\Requests\Resources\UpdateFormRequest;
use MoonShine\Laravel\MoonShineRequest;
use MoonShine\Laravel\Notifications\MoonShineNotificationContract;
use MoonShine\Support\Enums\ToastType;
use Symfony\Component\HttpFoundation\Response;
use Throwable;
Expand Down
2 changes: 1 addition & 1 deletion src/Laravel/src/Http/Controllers/MoonShineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use MoonShine\Contracts\Core\PageContract;
use MoonShine\Contracts\UI\TableBuilderContract;
use MoonShine\Contracts\UI\TableRowContract;
use MoonShine\Laravel\Contracts\Notifications\MoonShineNotificationContract;
use MoonShine\Laravel\Http\Responses\MoonShineJsonResponse;
use MoonShine\Laravel\Notifications\MoonShineNotificationContract;
use MoonShine\Laravel\Pages\QuickPage;
use MoonShine\Laravel\Traits\Controller\InteractsWithAuth;
use MoonShine\Laravel\Traits\Controller\InteractsWithUI;
Expand Down
13 changes: 6 additions & 7 deletions src/Laravel/src/Notifications/DatabaseNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use MoonShine\Laravel\Contracts\Notifications\NotificationButtonContract;
use MoonShine\Support\Enums\Color;
use MoonShine\Support\Traits\Makeable;

/**
* @method static static make(string $message, array $button = [], null|Color|string $color = null)
*/
final class DatabaseNotification extends Notification
{
use Queueable;
use Makeable;

public function __construct(
protected string $message,
protected array $button = [],
protected ?NotificationButtonContract $button = null,
protected null|string|Color $color = null
) {
$this->color = $this->color instanceof Color ? $this->color->value : $this->color;
Expand All @@ -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,
];
}
Expand Down
8 changes: 4 additions & 4 deletions src/Laravel/src/Notifications/MoonShineMemoryNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -16,25 +18,23 @@ final class MoonShineMemoryNotification implements MoonShineNotificationContract
private array $messages = [];

/**
* @param array{}|array{'link': string, 'label': string} $button
* @param array<int|string> $ids
*/
public static function send(
string $message,
array $button = [],
?NotificationButtonContract $button = null,
array $ids = [],
string|Color|null $color = null,
): void {
(new self())->notify($message, $button, $ids, $color);
}

/**
* @param array{}|array{'link': string, 'label': string} $button
* @param array<int|string> $ids
*/
public function notify(
string $message,
array $button = [],
?NotificationButtonContract $button = null,
array $ids = [],
string|Color|null $color = null,
): void {
Expand Down
10 changes: 5 additions & 5 deletions src/Laravel/src/Notifications/MoonShineNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,25 +19,23 @@
final class MoonShineNotification implements MoonShineNotificationContract
{
/**
* @param array{}|array{'link': string, 'label': string} $button
* @param array<int|string> $ids
*/
public static function send(
string $message,
array $button = [],
?NotificationButtonContract $button = null,
array $ids = [],
string|Color|null $color = null
): void {
app(MoonShineNotificationContract::class)->notify($message, $button, $ids, $color);
}

/**
* @param array{}|array{'link': string, 'label': string} $button
* @param array<int|string> $ids
*/
public function notify(
string $message,
array $button = [],
?NotificationButtonContract $button = null,
array $ids = [],
string|Color|null $color = null
): void {
Expand All @@ -55,7 +55,7 @@ public function notify(
)
)
->get(),
DatabaseNotification::make(
new DatabaseNotification(
$message,
$button,
$color
Expand Down
28 changes: 28 additions & 0 deletions src/Laravel/src/Notifications/NotificationButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace MoonShine\Laravel\Notifications;

use MoonShine\Laravel\Contracts\Notifications\NotificationButtonContract;

final readonly class NotificationButton implements NotificationButtonContract
{
public function __construct(
private string $label,
private string $link,
)
{

}

public function getLabel(): string
{
return $this->label;
}

public function getLink(): string
{
return $this->link;
}
}
21 changes: 10 additions & 11 deletions src/Laravel/src/Notifications/NotificationItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down
18 changes: 5 additions & 13 deletions src/Laravel/src/Notifications/NotificationMemoryItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
) {
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Laravel/src/Providers/MoonShineServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Laravel/src/Traits/Controller/InteractsWithUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,12 +17,11 @@ protected function toast(string $message, ToastType $type = ToastType::INFO): vo
}

/**
* @param array{}|array{'link': string, 'label': string} $buttons
* @param array<int|string> $ids
*/
protected function notification(
string $message,
array $buttons = [],
?NotificationButtonContract $buttons = null,
array $ids = [],
string|Color|null $color = null
): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class="notifications-remove"
<h5 class="notifications-title"></h5>
<p class="notifications-text">{{ $notification->getMessage() }}</p>

@if($notification->getButton() !== [])
@if(!is_null($notification->getButton()))
<div class="notifications-more">
<a href="{{ $notification->getButtonLink() }}">
{{ $notification->getButtonLabel() }}
<a href="{{ $notification->getButton()->getLink() }}">
{{ $notification->getButton()->getLabel() }}
</a>
</div>
@endif
Expand Down

0 comments on commit d38d9ed

Please sign in to comment.