Dispatch Event on AfterStateUpdated()

Hello, Does anyone know how to dispatch event for example to update-counter within main resource? Like we have $this->dispatch in Livewire components and in List,Edit filament classes, I need kind-a same thing but within main resource..
protected static function getStatusColumn (): Tables\Columns\SelectColumn
{
return Tables\Columns\SelectColumn::make('status')
->label(__("inputs.status"))
->options(ContactFormStatuses::class)
->sortable()
->afterStateUpdated(function($record) {
Notification::make()
->title(__('notifications.status_updated'))
->success()
->send();
$this->dispatch('update-counter');
})
->visible(fn() => self::$user->can('view_status' . self::RESOURCE_PERMISSION_SUFFIX));
}
protected static function getStatusColumn (): Tables\Columns\SelectColumn
{
return Tables\Columns\SelectColumn::make('status')
->label(__("inputs.status"))
->options(ContactFormStatuses::class)
->sortable()
->afterStateUpdated(function($record) {
Notification::make()
->title(__('notifications.status_updated'))
->success()
->send();
$this->dispatch('update-counter');
})
->visible(fn() => self::$user->can('view_status' . self::RESOURCE_PERMISSION_SUFFIX));
}
6 Replies
biebthesecond
biebthesecond4w ago
I'm assuming, since filament is basically fancy livewire components you probably could use $this->dispatch()?
Señor Nikola
Señor NikolaOP4w ago
Sadly no, it is within static method so $this-> wont work Just wondered if anyone had an solution?
biebthesecond
biebthesecond4w ago
You could try to pass the livewire instance as a parameter? Then you could use that to call ->dispatch() Maybe not the prettiest solution, but maybe it works
Señor Nikola
Señor NikolaOP4w ago
Thanks mate alot! You gave me perfect direction, i passed livewire insatnce as paramter and than i created pub function in List resource whic has access to $this->dispatch() that did the trick ContactFormResource
->afterStateUpdated(function($livewire) {
Notification::make()
->title(__('notifications.status_updated'))
->success()
->send();
$livewire->updateCounter();
})
->afterStateUpdated(function($livewire) {
Notification::make()
->title(__('notifications.status_updated'))
->success()
->send();
$livewire->updateCounter();
})
in ListContactForm
public function updateCounter (): void
{
$this->dispatch('update-counter');
}
public function updateCounter (): void
{
$this->dispatch('update-counter');
}
biebthesecond
biebthesecond4w ago
Cool it works! Maybe you could even do $livewire->dispatch('update-counter') directly in the afterStateUpdated, saves some code. Unless you use that method at other spots. Happy coding! Don't forget to set this thread as solved :D
Señor Nikola
Señor NikolaOP4w ago
Tried with $livewire->dispatch('update-counter') but it looks for dispatch() functon on List class hence why i did that extra code 🙂

Did you find this page helpful?