F
Filament6mo 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
Tin6mo 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
Hugo6mo 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
awcodes6mo ago
You have defined an ->action() method. So it won’t do anything on click.
Hugo
Hugo6mo ago
Hello, I don't have an ->action() method on my Action.
toeknee
toeknee6mo ago
he means you havent
awcodes
awcodes6mo ago
That’s my point. Actions aren’t livewire components so they aren’t going to just do a dispatch by themselves.
Hugo
Hugo6mo 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
awcodes6mo 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
Hugo6mo 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
awcodes6mo 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
Manal7d ago
Having the same issue, were you able to work around this @Hugo ?