filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
18.08k stars 2.83k forks source link

Action button with modal execute multiple duplicated calls #8763

Closed wychoong closed 9 months ago

wychoong commented 1 year ago

Package

filament/filament

Package Version

3.0.62

Laravel Version

10.25.1

Livewire Version

3.0.5

PHP Version

8.1.23

Problem description

when passing action arguments to modal content view it become empty

a behaviour that is observed is Action->modalContent is being called 4 times and only the first call contain the arguments

[2023-09-28 09:56:16] local.INFO: add {"product":1} 
[2023-09-28 09:56:16] local.INFO: add  
[2023-09-28 09:56:16] local.INFO: add  
[2023-09-28 09:56:16] local.INFO: add  

Expected behavior

only call one time and the arguments are passed to the view

Steps to reproduce

clone the repo and go to "Action Test" page

Reproduction repository

https://github.com/wychoong/filament-test/tree/passing-arguments-to-action-modal-view

Relevant log output

No response

wychoong commented 1 year ago

after some source dive, this is due to how filament handle the action mounting.

there are 2 issues in it:

  1. to determine whether an action contain modal content, action will call getModalContent once https://github.com/filamentphp/filament/blob/f69bc07143f9752f41d96b31de8dbe3b68979ebb/packages/actions/src/Concerns/InteractsWithActions.php#L187-L200 thus in the log it shows multiple calls, 1 for checking and 1 for actual content rendering. with caching in getModalContent will be a quick and easy way, or add a new hasModalContent. this will bring the calls down to 2, and the second call is mounting action

  2. mounting action in <x-filament-actions::modals /> the action is fetch as below

    $action = $this->getMountedAction();

    however here is where the issue why $arguments is empty, it doesn't mount $livewire->mountedActionsArguments

a workaround will be

->modalContent(function ($arguments, $action) {
    $arguments = Arr::last($this->mountedActionsArguments);

    return view('components.modal-content', [
        'params' => 'params',
        'arguments' => $arguments,
    ]);
})

this works but its not ideal, and should be fixed

atmonshi commented 10 months ago

had the same issue and this worked for me

$arguments = Arr::last($this->mountedActionsArguments);
danharrin commented 10 months ago

I believe this was fixed a couple of weeks ago

wychoong commented 10 months ago

I believe this was fixed a couple of weeks ago

hey @danharrin thanks for the fix, can confirmed its fixed. as mentioned in this issue there is also another issue where Action->modalContent is being called multiple times, which can be tested with the issue repo with this minor addition

->modalContent(function ($arguments, $action) {
      Log::info($action->getname(), [$arguments, $action->getArguments()]);
})

can you reopen this and rename the title? or should a new issue with same repo to be opened?

danharrin commented 10 months ago

please rename it :)

wychoong commented 10 months ago

please rename it :)

thanks, I have updated the issue repo as well

danharrin commented 9 months ago

Should be fixed by #10143, please confirm

wychoong commented 9 months ago

Should be fixed by #10143, please confirm

Can confirm its fixed. Thanks!