Skip to content

Commit e24ed06

Browse files
committedOct 28, 2021
Release v1.1.0
- [FEATURE] Add ability to delay toasts with pushOnNextPage(). - [FEATURE] Make session keys configurable. - [FIX] issue with dispatchBrowserEvent if called in same livewire request with a toast notification.
1 parent 6e99fbc commit e24ed06

File tree

7 files changed

+46
-12
lines changed

7 files changed

+46
-12
lines changed
 

‎config/tall-toasts.php

+9
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,13 @@
1010
* How long to wait before displaying the toasts after page loads, in ms
1111
*/
1212
'load_delay' => 400,
13+
14+
/*
15+
* Session keys used.
16+
* No need to edit unless the keys are already being used and conflict.
17+
*/
18+
'session_keys' => [
19+
'toasts' => 'toasts',
20+
'toasts_next_page' => 'toasts-next',
21+
],
1322
];

‎src/Concerns/WireToast.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ public function dehydrate(Response $response): void
1313
{
1414
if (! ToastManager::componentRendered()) {
1515
foreach (ToastManager::pull() ?? [] as $notification) {
16-
$response->effects['dispatches'] ??= [];
17-
18-
$response->effects['dispatches'][] = [
19-
'event' => 'toast',
20-
'data' => $notification,
21-
];
16+
$this->dispatchBrowserEvent('toast', $notification);
2217
}
2318
}
2419
}

‎src/Http/Livewire/ToastComponent.php

+6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@ class ToastComponent extends Component
2222
public function dehydrate(): void
2323
{
2424
ToastManager::setComponentRendered(true);
25+
2526
$this->toasts = array_merge($this->toasts, ToastManager::pull());
2627
}
2728

2829
public function mount(): void
2930
{
31+
if (session()->has(config('tall-toasts.session_keys.toasts_next_page'))) {
32+
$this->toasts = ToastManager::pullNextPage();
33+
}
34+
3035
$this->duration = config('tall-toasts.duration');
36+
3137
$this->loadDelay = config('tall-toasts.load_delay');
3238
}
3339

‎src/Notification.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public static function make(
3535

3636
public function push(): void
3737
{
38-
session()->push('toasts', $this->asArray());
38+
session()->push(config('tall-toasts.session_keys.toasts'), $this->asArray());
39+
}
40+
41+
public function pushOnNextPage(): void
42+
{
43+
session()->push(config('tall-toasts.session_keys.toasts_next_page'), $this->asArray());
3944
}
4045
}

‎src/ToastManager.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected static function getFacadeAccessor(): string
2626

2727
public static function hasPendingToasts(): bool
2828
{
29-
return session()->has('toasts');
29+
return session()->has(config('tall-toasts.session_keys.toasts'));
3030
}
3131

3232
protected static function javaScriptAssets(array $options): string
@@ -62,7 +62,12 @@ protected static function minify(string $subject): ?string
6262

6363
public static function pull(): array
6464
{
65-
return session()->pull('toasts', []);
65+
return session()->pull(config('tall-toasts.session_keys.toasts'), []);
66+
}
67+
68+
public static function pullNextPage(): array
69+
{
70+
return session()->pull(config('tall-toasts.session_keys.toasts_next_page'), []);
6671
}
6772

6873
public static function scripts(array $options = []): string

‎tests/Feature/Http/Livewire/ToastComponentTest.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,20 @@
2626
$message = 'testing info';
2727
$title = 'title info';
2828

29-
$notificationArray = Notification::make($message, $title);
30-
3129
toast()
3230
->info($message, $title)
3331
->push();
3432

33+
toast()
34+
->info($message . ' next', $title)
35+
->pushOnNextPage();
36+
3537
$component = Livewire::test(ToastComponent::class);
3638

37-
assertEquals($component->get('toasts'), [$notificationArray]);
39+
assertEquals($component->get('toasts'), [
40+
Notification::make($message . ' next', $title),
41+
Notification::make($message, $title),
42+
]);
3843
});
3944

4045
it('renders and pulls data in a blade view', function () {

‎tests/Unit/ToastTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,13 @@
3535
])
3636
->and(ToastManager::hasPendingToasts())
3737
->toBeFalse();
38+
39+
toast()
40+
->info('testing next page', 'title')
41+
->pushOnNextPage();
42+
43+
expect(ToastManager::hasPendingToasts())
44+
->toBeFalse()
45+
->and(ToastManager::pullNextPage())
46+
->toEqual([Notification::make('testing next page', 'title')]);
3847
});

0 commit comments

Comments
 (0)
Please sign in to comment.