FilamentF
Filament2y ago
Toni

Experiencing odd behaviour with custom Actions

Hi,
I have a small application where the user can design email templates and publish them. Once published, the email templates can be used in other parts of the application.
Therefore I wrote to actions: publish and unpublish Email.
This is the code for the publish action:
class PublishMailAction extends Action
{
    public static function getDefaultName(): ?string
    {
        return 'publishEmail';
    }

    protected function setUp(): void
    {
        parent::setUp();
        $this->icon('fal-check-to-slot');
        $this->label(__('mail.labels.publish'));
        $this->action(static function ($record) {
            if (MailServiceProvider::publishMailTemplate($record)) {
                MailNotifications::mailTemplatePublishedSuccess()->send();
                return true;
            }
            MailNotifications::mailTemplatePublishedFailed()->send();
            return false;
        });
    }
}

The unpublish action is similar, except MailServiceProvider::publishMailTemplate.

The table function from the resource:
->actions([
  ActionGroup::make([
    ActionGroup::make([
      Tables\Actions\ViewAction::make(),
      Tables\Actions\EditAction::make(),
    ])->dropdown(false),
    ActionGroup::make([
      SendTestMailAction::make(),
      PublishMailAction::make()
        ->visible(fn ($record) => !$record->is_published),
      UnpublishMailAction::make()
        ->visible(fn ($record) => $record->is_published),
    ])->dropdown(false),
  ]),
])

The Problem is, that it seems that as soon as I publish an email template, the state is preserved.
I published the last email template, and when I click on another entry, the unpublish action is visible and all custom actions refer still to the first published item.

What am I doing wrong?

Thank you!

Edit: I have forgotten the second screenshot:
image.png
Solution
Second screenshot:
image.png
Was this page helpful?