F
Filament11mo ago
Hugo

How to dispatch an event from a Notification Action

I've been trying to dispatch an event from a Notification Action but it's not working. using ->dispatch or ->dispatchSelf seems to do nothing. I've searched around and tried to use ->action() and dispatch using $livewire->dispatch() but action seems to not work on notification Actions. How can I rly dispatch an event from a Notification action?
Notification::make()
->success()
->title('Portador criado com sucesso!')
->actions([ \Filament\Notifications\Actions\Action::make('view')
->label('Ver Portador')
// ->url('/admin/business/clients/' .$client->id.'/edit?activeRelationManager=2',true)
->dispatchSelf('test-created')
])
->send();
Notification::make()
->success()
->title('Portador criado com sucesso!')
->actions([ \Filament\Notifications\Actions\Action::make('view')
->label('Ver Portador')
// ->url('/admin/business/clients/' .$client->id.'/edit?activeRelationManager=2',true)
->dispatchSelf('test-created')
])
->send();
Further down in my Resource, there's my method that is attempting to listen to the dispatch event but it never gets there.
#[On('test-event')]
public function test()
{
dd('here');
}
#[On('test-event')]
public function test()
{
dd('here');
}
Any Thoughts?
12 Replies
Tin
Tin11mo ago
I think you should use dispatch() instead of dispatchSelf and be careful of the naming. You are dispatching test-created and listening for test-event
Hugo
HugoOP11mo ago
I had to change some things around and forgot to change the name but it doesn't work still, not with dispatch() and not with dispatchSelf()
awcodes
awcodes11mo ago
You have defined an ->action() method. So it won’t do anything on click.
Hugo
HugoOP11mo ago
Hello, I don't have an ->action() method on my Action.
toeknee
toeknee11mo ago
he means you havent
awcodes
awcodes11mo ago
That’s my point. Actions aren’t livewire components so they aren’t going to just do a dispatch by themselves.
Hugo
HugoOP11mo ago
Oh sorry for my misunderstanding then, so what do you suggest? Have an action method as well as a dispatch one? I've tried to dispatch it from the ->action() method and my perception was that it doesn't even go into ->action() method, even a simple dd inside the ->action() method doesn't work.
awcodes
awcodes11mo ago
Something like this, maybe.
Action::make('view')
->label('Ver Portador')
->action(fn ($livewire) => $livewire->dispatch('test-event')
Action::make('view')
->label('Ver Portador')
->action(fn ($livewire) => $livewire->dispatch('test-event')
Hugo
HugoOP11mo ago
I've already tried it, it's not working, even tried to just ->action(fn ($livewire) => dd($livewire) just to see if it was even getting inside the method but it's not Can it be any behavior with it being a Notification Action?
awcodes
awcodes11mo ago
possibly, it shouldn't matter though. sorry i don't have an app set up with notification's that i can test against at the moment. hmm. according to the docs ->dispatch() should work.
Action::make('undo')
->color('gray')
->dispatch('undoEditingPost', [$post->id]),
Action::make('undo')
->color('gray')
->dispatch('undoEditingPost', [$post->id]),
Manal
Manal6mo ago
Having the same issue, were you able to work around this @Hugo ?
Want results from more Discord servers?
Add your server