From 01af306663d8cc7acf1101464e4ddf342c6d15a1 Mon Sep 17 00:00:00 2001 From: Luan Freitas Date: Tue, 31 Aug 2021 16:02:47 -0300 Subject: [PATCH] add can method in button action --- docs/README.md | 5 +++++ resources/stubs/table.fillable.stub | 2 +- resources/stubs/table.model.stub | 2 +- resources/stubs/table.stub | 4 ++-- resources/views/components/table.blade.php | 5 +++-- src/Button.php | 14 ++++++++++++++ src/Traits/ActionButton.php | 9 +++++++-- 7 files changed, 33 insertions(+), 8 deletions(-) diff --git a/docs/README.md b/docs/README.md index 4069e7af..db64e30e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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'), diff --git a/resources/stubs/table.fillable.stub b/resources/stubs/table.fillable.stub index 97a6236c..2bf366c4 100644 --- a/resources/stubs/table.fillable.stub +++ b/resources/stubs/table.fillable.stub @@ -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.'), ] diff --git a/resources/stubs/table.model.stub b/resources/stubs/table.model.stub index 4751d35c..3c79c607 100644 --- a/resources/stubs/table.model.stub +++ b/resources/stubs/table.model.stub @@ -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.'), ] diff --git a/resources/stubs/table.stub b/resources/stubs/table.stub index e13f01a3..e7379e86 100644 --- a/resources/stubs/table.stub +++ b/resources/stubs/table.stub @@ -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.'), ] diff --git a/resources/views/components/table.blade.php b/resources/views/components/table.blade.php index a7f0f2fa..d6df1c4c 100644 --- a/resources/views/components/table.blade.php +++ b/resources/views/components/table.blade.php @@ -47,7 +47,9 @@ @else @foreach($data as $row) - - diff --git a/src/Button.php b/src/Button.php index 80dfd29d..7dc2289e 100644 --- a/src/Button.php +++ b/src/Button.php @@ -20,6 +20,8 @@ class Button public string $event = ''; + public bool $can = true; + /** * Button constructor. * @param string $action @@ -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; + } } diff --git a/src/Traits/ActionButton.php b/src/Traits/ActionButton.php index f6332e7f..5ab528ff 100644 --- a/src/Traits/ActionButton.php +++ b/src/Traits/ActionButton.php @@ -2,6 +2,8 @@ namespace PowerComponents\LivewirePowerGrid\Traits; +use PowerComponents\LivewirePowerGrid\Button; + trait ActionButton { public array $actionRoutes = []; @@ -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; } }