Skip to content

Commit

Permalink
add can method in button action
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev committed Aug 31, 2021
1 parent fd6a971 commit 01af306
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 8 deletions.
5 changes: 5 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,15 +516,20 @@ These methods are available in the `Button` class.
|**openModal**| *String* $component, *Array* $params| |```->openModal('product', ['product' => 'id'])```|
|**method**| *String* $method|Method for action (GET/POST/PUT/DELETE))|```->method('delete')```|
|**route**| *String* $route, *Array* $param|Route for action|```->route('product.edit', ['product' => 'id'])```|
|**can**| *bool* $can|Permission for action|```->can($canClickButton)```|

Example of usage in action:

```php
public function actions(): array
{
$canDelete = false;

return [
Button::add('destroy')
->caption(__('Delete'))
->caption(__('Delete'))
->can($canDelete)
->class('bg-red-500 text-white')
->route('product.destroy', ['product' => 'id'])
->method('delete'),
Expand Down
2 changes: 1 addition & 1 deletion resources/stubs/table.fillable.stub
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class {{ componentName }} extends PowerGridComponent
'_default_message' => __('Data has been updated successfully!'),
//'custom_field' => __('Custom Field updated successfully!'),
],
"error" => [
'error' => [
'_default_message' => __('Error updating the data.'),
//'custom_field' => __('Error updating custom field.'),
]
Expand Down
2 changes: 1 addition & 1 deletion resources/stubs/table.model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class {{ componentName }} extends PowerGridComponent
'_default_message' => __('Data has been updated successfully!'),
//'custom_field' => __('Custom Field updated successfully!'),
],
"error" => [
'error' => [
'_default_message' => __('Error updating the data.'),
//'custom_field' => __('Error updating custom field.'),
]
Expand Down
4 changes: 2 additions & 2 deletions resources/stubs/table.stub
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ class {{ componentName }} extends PowerGridComponent
public function updateMessages(string $status, string $field = '_default_message'): string
{
$updateMessages = [
'success' => [
'uccess' => [
'_default_message' => __('Data has been updated successfully!'),
//'custom_field' => __('Custom Field updated successfully!'),
],
"error" => [
'error' => [
'_default_message' => __('Error updating the data.'),
//'custom_field' => __('Error updating custom field.'),
]
Expand Down
5 changes: 3 additions & 2 deletions resources/views/components/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
@else
<th>
@foreach($data as $row)
<tr class="{{ $theme->table->trBodyClass }}" style="{{ $theme->table->trBodyClass }}"
@ds($row)
<tr class="{{ $theme->table->trBodyClass }}"
style="{{ $theme->table->trBodyStyle }}"
wire:key="{{ $row->id }}">
<x-livewire-powergrid::checkbox-row
:theme="$theme->checkbox"
Expand All @@ -71,4 +73,3 @@
</x-slot>
</x-livewire-powergrid::table-base>


14 changes: 14 additions & 0 deletions src/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Button

public string $event = '';

public bool $can = true;

/**
* Button constructor.
* @param string $action
Expand Down Expand Up @@ -118,4 +120,16 @@ public function emit(string $event, array $param): Button

return $this;
}

/**
* emit
* @param bool $can can
* @return $this
*/
public function can(bool $can): Button
{
$this->can = $can;

return $this;
}
}
9 changes: 7 additions & 2 deletions src/Traits/ActionButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PowerComponents\LivewirePowerGrid\Traits;

use PowerComponents\LivewirePowerGrid\Button;

trait ActionButton
{
public array $actionRoutes = [];
Expand All @@ -15,10 +17,13 @@ trait ActionButton

public function initActions()
{
$this->actions = $this->actions();
$this->actions = collect($this->actions())
->where('can', true)
->toArray();

/** @var Button $action */
foreach ($this->actions as $action) {
if (isset($action->route)) {
if (isset($action->route) && $action->can) {
$this->actionRoutes[$action->action] = $action->route;
}
}
Expand Down

0 comments on commit 01af306

Please sign in to comment.