Trigger Page Reload from Admin Resource?

I've got some logic that happens when a model is saved and a custom component to render the result of that (in this case, an image variant.) But it does not refresh on form save and I can't seem to find a way to make that happen within the Filament/Livewire universe. However, I'd be perfectly happy to just have the page reload after save. I realize this is probably a dumb question, but is there a way to reload the edit page after save?
9 Replies
Saade
Saade2y ago
// ServiceProvider.php

Filament::registerRenderHook(
'body.end',
fn () => '<script>window.addEventListener("reload", () => window.location.reload())</script>',
);
// ServiceProvider.php

Filament::registerRenderHook(
'body.end',
fn () => '<script>window.addEventListener("reload", () => window.location.reload())</script>',
);
// YourResource/Pages/EditPage.php

protected function afterSave(): void
{
$this->dispatchBrowserEvent('reload');
}
// YourResource/Pages/EditPage.php

protected function afterSave(): void
{
$this->dispatchBrowserEvent('reload');
}
wyChoong
wyChoong2y ago
I’m interested in this as well. Using redirect currently causes the $this->previousUrl to be updated with current page url and causes the back/cancel button can’t go back to previous page properly
greatfortress
greatfortressOP2y ago
@saadeguilherme brilliant! Thank you. This definitely seems like it should solve the problem.
DrByte
DrByte13mo ago
Update for v3:
FilamentView::registerRenderHook(
'panels::body.end',
static fn () => '<script>window.addEventListener("reload", () => window.location.reload())</script>',
);
FilamentView::registerRenderHook(
'panels::body.end',
static fn () => '<script>window.addEventListener("reload", () => window.location.reload())</script>',
);
and
$this->dispatch('reload');
$this->dispatch('reload');
Saade
Saade13mo ago
In v3 you can just $this->js("window.location.reload"); you don’t need a render hook
DrByte
DrByte13mo ago
Oh nice. Thanks! Hmmm ... I just tried that, but the page is not reloading, and the console logs:
Alpine Expression Error: 'reload' called on an object that does not implement interface Location.

Expression: "window.location.reload"
Alpine Expression Error: 'reload' called on an object that does not implement interface Location.

Expression: "window.location.reload"
LeandroFerreira
LeandroFerreira13mo ago
$this->js('window.location.reload()'); ?
wyChoong
wyChoong13mo ago
$this->dispatch(‘$refresh’)->to(YourComponent::class) Then register listener on your target component protected $listeners = [‘$refresh’]
DrByte
DrByte13mo ago
Oh, duh. I should have thought of that. Yes, adding the () makes it work. Thanks!
Want results from more Discord servers?
Add your server