F
Filament16mo ago
josef

Force refresh on page actions

I have an action on the edit page that changes some data on the model, and after that I want to conditionally hide that action and show another one (think publish/unpublish), but even though the form itself seems to refresh, the actions don't. Is there an event I could dispatch to force that?
7 Replies
LeandroFerreira
LeandroFerreira16mo ago
it was supposed to work. Are you doing something like this?
Action::make('publish')
->action(function ($record) {
$record->update(['publish' => true]);
})
->visible(fn ($record): bool => !$record->publish)
Action::make('publish')
->action(function ($record) {
$record->update(['publish' => true]);
})
->visible(fn ($record): bool => !$record->publish)
josef
josefOP16mo ago
exactly, although the action is in a method in the page component, but that shouldnt make a difference
cheesegrits
cheesegrits16mo ago
I have a similar requirement on a relation manager page, so I have this on the relation manager ...
protected $listeners = ['refreshRelations' => '$refresh'];
protected $listeners = ['refreshRelations' => '$refresh'];
... and do this in the action ...
$this->emit('refreshRelations');
$this->emit('refreshRelations');
You can probably do the same with the listener on the EditFoo page. And (I think) use dispatch() instead of emit() in v3?
josef
josefOP16mo ago
Thanks for the suggestions, but there doesnt seem to be an event that re-renders the header actions. I tried cacheHeaderActions() which just adds them to the header since they get added to the existing array of cached actions. Calling render() also diesnt help. What's weird is, if I click the action a second time, it just works and the header actions get refreshed. Could have sworn this just worked in v2. Doing this works. Not pretty, but well ...
$this->cachedHeaderActions = [];
$this->cacheHeaderActions();
$this->cachedHeaderActions = [];
$this->cacheHeaderActions();
cheesegrits
cheesegrits16mo ago
I'll dig up how I was doing this and double check it. I had the exact same problem, that I needed the header action to hide and a different one show.
josef
josefOP16mo ago
Let me know if you find a nicer way. The only other possibility I can think of is to redirect to current url after the action. But for now I'll keep this solution as it works.
Want results from more Discord servers?
Add your server