FilamentF
Filament7mo ago
5 replies
frame

Closing tab after saving changes from EditResource

I am opening an EditResource page in a new tab from the front end. Is it possible to also close the tab once the user has saved changes? I tried dispatching an event from
getSaveFormAction
action's
after
method but I can't get the event to fire. Just "livewire:init" shows up in console. 🤔

protected function getSaveFormAction(): Action
{
    return Action::make('save')
        ->submit('save')
        ->after(function () {
            $this->dispatch('close-page');
        });
}

->renderHook(PanelsRenderHook::BODY_END, fn (): HtmlString => new HtmlString(<<<'HTML'
<script>
window.addEventListener("livewire:init", function () {
    console.log("livewire:init");
    Livewire.on("close-page", function () {
        console.log("livewire close page");
        window.close();
    });
});
</script>
HTML))


Cancel was easier:
protected function getCancelFormAction(): Action
{
    return Action::make('cancel')
        ->color('gray')
        ->extraAttributes([
            'x-on:click' => 'window.close()',
        ]);
}
Solution
Ok I sort-of managed to do this using

protected function handleRecordUpdate(Model $record, array $data): Model
{
    $record = parent::handleRecordUpdate($record, $data);
    $this->dispatch('close-page');
    return $record;
}


For some reason, the event was not dispatched at all either using your example or
protected function afterActionCalled(): void
{
    $this->dispatch('close-page');
}


BUT you must be opening the tab with JS (I'm assuming that what you mean by "I am opening ... a new tab from the front end"

Anyway, hope it helps
Was this page helpful?