Filament refresh action button as well when triggering an action

1.1k views Asked by At

I'm using filamentphp to hack together a small tool for internal use. I'm trying to add an action to a Person model to toggle is_approved boolean, and it works fine but I can't figure out how to refresh the action button itself on trigger, other than full page reload.

Filamentphp docs describe how to refresh form data on trigger, but not the action button itself.

<?php

namespace App\Filament\Resources\PersonResource\Pages;

use App\Filament\Resources\PersonResource;
use Filament\Actions;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;

class EditPerson extends EditRecord
{
    protected static string $resource = PersonResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\DeleteAction::make(),
            Action::make($this->record->is_approved ? 'Disapprove' : 'Approve')
                ->color($this->record->is_approved ? 'danger' : 'success')
                ->action(function () {
                    $this->record->is_approved = !$this->record->is_approved;
                    $this->record->save();
                    $this->refreshFormData([
                        'is_approved',
                    ]);
                    Notification::make()
                        ->title($this->record->is_approved ? 'Approved successfully' : 'Disapproved successfully')
                        ->success()
                        ->send();
                    $this->redirect('edit');
                })
        ];
    }
}

Commenting out $this->redirect('edit'); will still flip the boolean, but action button will not update.

This is just bare minimum to get it working, albeit in a very crude way. However there is a glaring issue where if you change form input and than decide to Approve/Disapprove before saving you would lose form changes - which is different from the rest of the app and bad UX.

Am approaching the whole thing in a sensible way? I'm not much of a programmer, just hacking things together with variables, functions and loops.

I went trough filamentphp docs to try and find a "native" way of doing something like this, but couldn't figure it out.

1

There are 1 answers

0
Maka On

I've came across same issue. You can use redirect as following:


Action::make('RefreshOrder')
        ->icon('heroicon-m-paper-airplane')
        ->requiresConfirmation()
        ->action(function (Orders $record) {
            // Redirect page to any url you want
            redirect(static::getUrl('view', ['record' => $record->id,]));
        }
})

Assuming that you have getPages defined in your resource:


public static function getPages(): array
    {
        return [
            'index' => Pages\ListOrder::route('/'),
            'create' => Pages\CreateOrder::route('/create'),
            'edit' => Pages\EditOrder::route('/{record}/edit'),
            'view' => Pages\ViewOrder::route('/{record}'),
        ];
    }

I am using view to open up View page. Depend what you need. You can set any URL to redirect('https://....').