pdmfc / nova-action-button

MIT License
37 stars 29 forks source link

Add canRun() #5

Open jerrychee94 opened 4 years ago

jerrychee94 commented 4 years ago

This will not work in fields method.

ActionButton::make('Action')->action((new AddRemark)->canSee(function () {
                return false;
            }), $this->id),

But it works in actions method

jerrychee94 commented 4 years ago

Will come out with PR for this soon.

Quiss commented 4 years ago

@jerrychee94 how soon?

skoyah commented 4 years ago

Hi @jerrychee94 . I used the canSee()method and I was able to hide it. I think your code has a problem. You are trying to use it inside the action() method instead of chaining it after. It should be used as a normal field:

ActionButton::make('Action')
->action(AddRemark::class, $this->id)
->canSee(function () {
    return false;
})

About the canRun() that one seems to be valid. I will try to tackle it as soon as I have the chance. Meanwhile, feel free to contribute with a PR if you want! 😉

jerrychee94 commented 4 years ago

Sorry for the delay. Was caught up with other tasks and I do not have laravel nova on my personal computer :( I will try my best to come out with PR.

skoyah commented 4 years ago

@jerrychee94 What would be a good option for the canRun() method in this case?

Nova by default will run the action and only while executing it will actually determine if you have the right permission.

with the current caveat, we can still leverage the canRun defined in your actions() method as a second guard, while at the same time implement the canRun feature in the package by disabling or hide completely the button, or even by showing the same toasted message as soon as the user clicks on the button.

I'm open to suggestions.

jerrychee94 commented 4 years ago

Hi @skoyah, just like what you said, the permission given to the user. Meaning that this particular user are able to see the action button but not necessarily he/she can run.

Another use case I can think of is conditional actiion buttons that will affected by the value in another field. For example, this action buttion to REJECT an entry will be enabled (or canRun) only if the value of status field is 'APPROVED'.