Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom notification object #9311

Merged
merged 5 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/notifications/docs/05-customizing-notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace App\Notifications;

use Filament\Notifications\Notification as BaseNotification;

class Notification extends BaseNotification
class CustomNotification extends BaseNotification
{
protected string $size = 'md';

Expand All @@ -84,7 +84,7 @@ class Notification extends BaseNotification

public static function fromArray(array $data): static
{
return parent::fromArray()->size($data['size']);
return parent::fromArray($data)->size($data['size']);
}

public function size(string $size): static
Expand All @@ -104,10 +104,10 @@ class Notification extends BaseNotification
Next, you should bind your custom `Notification` class into the container inside a service provider's `boot()` method:

```php
use App\Notifications\Notification;
use App\Notifications\CustomNotification;
use Filament\Notifications\Notification as BaseNotification;

$this->app->bind(BaseNotification::class, Notification::class);
$this->app->bind(BaseNotification::class, CustomNotification::class);
```

You can now use your custom `Notification` class in the same way as you would with the default `Notification` object.
5 changes: 5 additions & 0 deletions packages/notifications/src/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public function toArray(): array
public static function fromArray(array $data): static
{
$static = static::make($data['id'] ?? Str::random());
wychoong marked this conversation as resolved.
Show resolved Hide resolved

if (is_subclass_of($static::class, self::class) && get_called_class() === self::class) {
$static = $static::fromArray($data);
}

$static->actions(
array_map(
fn (array $action): Action | ActionGroup => match (array_key_exists('actions', $action)) {
Expand Down
33 changes: 33 additions & 0 deletions tests/src/Notifications/Fixtures/CustomNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Filament\Tests\Notifications\Fixtures;

class CustomNotification extends \Filament\Notifications\Notification
{
protected string $size = 'md';

public function toArray(): array
{
return [
...parent::toArray(),
'size' => $this->getSize(),
];
}

public static function fromArray(array $data): static
{
return parent::fromArray($data)->size($data['size']);
}

public function size(string $size): static
{
$this->size = $size;

return $this;
}

public function getSize(): string
{
return $this->size;
}
}
39 changes: 39 additions & 0 deletions tests/src/Notifications/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Filament\Notifications\Notification;
use Filament\Support\Enums\ActionSize;
use Filament\Support\Enums\IconPosition;
use Filament\Tests\Notifications\Fixtures\CustomNotification;
use Filament\Tests\TestCase;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -221,3 +222,41 @@ function getLastNotificationAction()
$notification = Notification::make()->actions([$action])->send();
expect(getLastNotificationAction()->getLivewireClickHandler())->toBe("\$dispatchTo('a_component', 'an_event', JSON.parse('[\\u0022data\\u0022]'))");
});

it('can bind custom notification object', function () {
app()->bind(Notification::class, CustomNotification::class);

$notification = Notification::make();

expect($notification)
->toBeInstanceOf(CustomNotification::class);
});

it('can resolve custom notification object from data', function () {
app()->bind(Notification::class, CustomNotification::class);

Notification::make()
->size($size = 'lg')
->body($body = Str::random())
->title($title = Str::random())
->send();

$notifications = session()->get('filament.notifications');

expect($notifications)
->toBeArray()
->toHaveCount(1);

$notification = Arr::last($notifications);

$component = livewire(Notifications::class);

$component
->dispatch('notificationsSent');

$notification = $component->instance()->notifications->first();

expect($notification)
->toBeInstanceOf(CustomNotification::class)
->getSize()->toBe($size);
});