From 44b857d5e8ac61b5b53065d6a9eae5d5b5a30300 Mon Sep 17 00:00:00 2001 From: Arthur LORENT Date: Fri, 28 Oct 2022 15:38:48 +0200 Subject: [PATCH] 5.2.0 (#111) * Added a new built-in `RedirectRowAction`, that is now used to render the pre-configured `ShowRowAction` * Added an optional second argument `bool $openInNewWindow = false` to the `ShowRowAction` * Added a new pre-configured `AddHeadAction`, that is using the built-in `RedirectHeadAction` * Added a new `Add` translation for it that you'll have to add to [your own translations](/README.md#translations) * Added a new `config('laravel-table.icon.add')` config for it with the `` default value that you'll also have to add to [your published configuration file](/README.md#configuration) --- CHANGELOG.md | 16 ++++- README.md | 16 +++-- config/laravel-table.php | 1 + phpstan.neon.dist | 2 +- src/Abstracts/AbstractRowAction.php | 2 +- src/HeadActions/AddHeadAction.php | 41 +++++++++++++ src/RowActions/RedirectRowAction.php | 60 +++++++++++++++++++ src/RowActions/ShowRowAction.php | 29 +++++---- tests/Unit/Bootstrap5/TableHeadActionTest.php | 9 +-- 9 files changed, 150 insertions(+), 26 deletions(-) create mode 100644 src/HeadActions/AddHeadAction.php create mode 100644 src/RowActions/RedirectRowAction.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f3f215..bad19b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +All notable changes to this package will be documented in this file. + +## [5.2.0](https://github.com/Okipa/laravel-table/compare/5.1.2...5.2.0) + +2022-10-28 + +* Added a new built-in `RedirectRowAction`, that is now used to render the pre-configured `ShowRowAction` +* Added an optional second argument `bool $openInNewWindow = false` to the `ShowRowAction` +* Added a new pre-configured `AddHeadAction`, that is using the built-in `RedirectHeadAction` + * Added a new `Add` translation for it that you'll have to add to [your own translations](/README.md#translations) + * Added a new `config('laravel-table.icon.add')` config for it with the `` default value that you'll also have to add to [your published configuration file](/README.md#configuration) + ## [5.1.2](https://github.com/Okipa/laravel-table/compare/5.1.1...5.1.2) 2022-10-27 @@ -18,8 +30,8 @@ 2022-10-25 * Added ability to chain a `->when(bool $condition)` method to an instantiated head action, in order to enable it conditionally -* Added a new built-in `RedirectHeadAction`, that will be used by the pre-configured `CreateHeadAction` -* Added an optional `bool $openInNewWindow = false` to the `CreateHeadAction` +* Added a new built-in `RedirectHeadAction`, that is now used to render the pre-configured `CreateHeadAction` +* Added an optional second argument `bool $openInNewWindow = false` to the `CreateHeadAction` * Added a new [JavaScript snippet](/README.md#set-up-a-few-lines-of-javascript) to handle head action link opening in tab: you'll have to add it if you want to benefit from this new ability ## [5.0.2](https://github.com/Okipa/laravel-table/compare/5.0.1...5.0.2) diff --git a/README.md b/README.md index 181d00d..4338100 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,7 @@ Status * `Actions` * `Bulk Actions` * `Create` +* `Add` * `Show` * `Edit` * `Destroy` @@ -475,10 +476,10 @@ If no head action is declared, the dedicated slot for it in the table head will This package provides the following built-in head actions: * `RedirectHeadAction`: * Requires `string $url`, `string $label`, `string $icon`, `array $class = ['btn', 'btn-success']` and `bool $openInNewWindow = false` arguments on instantiation - * Redirects to the model create page from a click on a `Create` button + * Redirects to the given URL from a click on the button * `CreateHeadAction`: * Requires `string $createUrl` and `bool $openInNewWindow = false` arguments on instantiation - * Instantiate a pre-configured `RedirectHeadAction` with the given `$createUrl` as URL, `__('Create')` as label and `config('laravel-table.icon.create')` as icon + * Instantiate a pre-configured `RedirectHeadAction` with `$createUrl` as URL, `__('Create')` as label and `config('laravel-table.icon.create')` as icon To use one of them, you'll have to pass an instance of it to the `headAction` method. @@ -491,7 +492,7 @@ namespace App\Tables; use App\Models\User; use Okipa\LaravelTable\Table; -use Okipa\LaravelTable\HeadActions\CreateHeadAction; +use Okipa\LaravelTable\HeadActions\AddHeadAction; use Okipa\LaravelTable\Abstracts\AbstractTableConfiguration; class UsersTable extends AbstractTableConfiguration @@ -501,7 +502,7 @@ class UsersTable extends AbstractTableConfiguration return Table::make() ->model(User::class) // Create head action will not be available when authenticated user is not allowed to create users - ->headAction((new CreateHeadAction(route('user.create')))->when(Auth::user()->cannot('create_users'))); + ->headAction((new AddHeadAction(route('user.create')))->when(Auth::user()->cannot('create_users'))); } } ``` @@ -638,9 +639,12 @@ If no row action is declared on your table, the dedicated `Actions` column at th **Important note:** [you'll have to set up a few lines of javascript](#set-up-a-few-lines-of-javascript) to allow row actions confirmation requests and feedback to be working properly. This package provides the built-in following row actions: +* `RedirectRowAction`: + * Requires `string $url`, `string $title`, `string $icon`, `array $class = ['link-info']`, `string|null $defaultConfirmationQuestion = null`, `string|null $defaultFeedbackMessage = null` and `bool $openInNewWindow = false` arguments on instantiation + * Redirects to the given URL from a click on the link * `ShowRowAction`: - * Requires a `string $showUrl` argument on instantiation - * Redirects to the model edit page on click + * Requires `string $showUrl` and `bool $openInNewWindow = false` arguments on instantiation + * Instantiate a pre-configured `RedirectRowAction` with `$showUrl` as URL, `__('Show')` as label and `config('laravel-table.icon.show')` as icon * `EditRowAction`: * Requires a `string $editUrl` argument on instantiation * Redirects to the model edit page on click diff --git a/config/laravel-table.php b/config/laravel-table.php index c38abd7..05c98f2 100644 --- a/config/laravel-table.php +++ b/config/laravel-table.php @@ -23,6 +23,7 @@ 'info' => '', 'reset' => '', 'drag_drop' => '', + 'add' => '', 'create' => '', 'show' => '', 'edit' => '', diff --git a/phpstan.neon.dist b/phpstan.neon.dist index bc7ca4e..687228d 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -8,7 +8,7 @@ parameters: - src/ - tests/ - # The level 9 is the highest level + # Level 9 is the highest level level: 5 databaseMigrationsPath: diff --git a/src/Abstracts/AbstractRowAction.php b/src/Abstracts/AbstractRowAction.php index 8d7d77c..d0045f8 100644 --- a/src/Abstracts/AbstractRowAction.php +++ b/src/Abstracts/AbstractRowAction.php @@ -17,7 +17,7 @@ abstract class AbstractRowAction public string $identifier; - protected string|null $class; + protected array $class; protected string $icon; diff --git a/src/HeadActions/AddHeadAction.php b/src/HeadActions/AddHeadAction.php new file mode 100644 index 0000000..aa896a6 --- /dev/null +++ b/src/HeadActions/AddHeadAction.php @@ -0,0 +1,41 @@ +redirectHeadAction = new RedirectHeadAction( + url: $createUrl, + label: __('Add'), + icon: config('laravel-table.icon.add'), + openInNewWindow: $openInNewWindow + ); + } + + protected function class(): array + { + return $this->redirectHeadAction->class(); + } + + protected function title(): string + { + return $this->redirectHeadAction->title(); + } + + protected function icon(): string + { + return $this->redirectHeadAction->icon(); + } + + public function action(Component $livewire): void + { + $this->redirectHeadAction->action($livewire); + } +} diff --git a/src/RowActions/RedirectRowAction.php b/src/RowActions/RedirectRowAction.php new file mode 100644 index 0000000..31d75d8 --- /dev/null +++ b/src/RowActions/RedirectRowAction.php @@ -0,0 +1,60 @@ +title); + } + + protected function class(Model $model): array + { + return $this->class; + } + + protected function icon(Model $model): string + { + return $this->icon; + } + + protected function title(Model $model): string + { + return $this->title; + } + + protected function defaultConfirmationQuestion(Model $model): string|null + { + return $this->defaultConfirmationQuestion; + } + + protected function defaultFeedbackMessage(Model $model): string|null + { + return $this->defaultFeedbackMessage; + } + + public function action(Model $model, Component $livewire): void + { + $this->openInNewWindow + ? $livewire->emit('laraveltable:link:open:newtab', $this->url) + : redirect()->to($this->url); + } +} diff --git a/src/RowActions/ShowRowAction.php b/src/RowActions/ShowRowAction.php index 042536e..cb1841b 100644 --- a/src/RowActions/ShowRowAction.php +++ b/src/RowActions/ShowRowAction.php @@ -3,50 +3,55 @@ namespace Okipa\LaravelTable\RowActions; use Illuminate\Database\Eloquent\Model; -use Illuminate\Http\RedirectResponse; use Livewire\Component; -use Livewire\Redirector; use Okipa\LaravelTable\Abstracts\AbstractRowAction; class ShowRowAction extends AbstractRowAction { - public function __construct(public string $showUrl) + protected RedirectRowAction $redirectRowAction; + + public function __construct(public string $showUrl, public bool $openInNewWindow = false) { - // + $this->redirectRowAction = new RedirectRowAction( + url: $showUrl, + title: __('Show'), + icon: config('laravel-table.icon.show'), + openInNewWindow: $openInNewWindow + ); } protected function identifier(): string { - return 'row_action_show'; + return $this->redirectRowAction->identifier(); } protected function class(Model $model): array { - return ['link-info']; + return $this->redirectRowAction->class($model); } protected function icon(Model $model): string { - return config('laravel-table.icon.show'); + return $this->redirectRowAction->icon($model); } protected function title(Model $model): string { - return __('Show'); + return $this->redirectRowAction->title($model); } protected function defaultConfirmationQuestion(Model $model): string|null { - return null; + return $this->redirectRowAction->defaultConfirmationQuestion($model); } protected function defaultFeedbackMessage(Model $model): string|null { - return null; + return $this->redirectRowAction->defaultFeedbackMessage($model); } - public function action(Model $model, Component $livewire): RedirectResponse|Redirector + public function action(Model $model, Component $livewire): void { - return redirect()->to($this->showUrl); + $this->redirectRowAction->action($model, $livewire); } } diff --git a/tests/Unit/Bootstrap5/TableHeadActionTest.php b/tests/Unit/Bootstrap5/TableHeadActionTest.php index 2f77838..486ded8 100644 --- a/tests/Unit/Bootstrap5/TableHeadActionTest.php +++ b/tests/Unit/Bootstrap5/TableHeadActionTest.php @@ -7,6 +7,7 @@ use Livewire\Livewire; use Okipa\LaravelTable\Abstracts\AbstractTableConfiguration; use Okipa\LaravelTable\Column; +use Okipa\LaravelTable\HeadActions\AddHeadAction; use Okipa\LaravelTable\HeadActions\CreateHeadAction; use Okipa\LaravelTable\Table; use Tests\Models\User; @@ -20,13 +21,13 @@ class TableHeadActionTest extends TestCase public function it_can_set_table_head_action(): void { app('router')->get('/user/create', ['as' => 'user.create']); - Config::set('laravel-table.icon.create', 'create-icon'); + Config::set('laravel-table.icon.add', 'add-icon'); $config = new class extends AbstractTableConfiguration { protected function table(): Table { return Table::make()->model(User::class) - ->headAction(new CreateHeadAction(route('user.create'))); + ->headAction(new AddHeadAction(route('user.create'))); } protected function columns(): array @@ -42,8 +43,8 @@ protected function columns(): array '', - 'create-icon Create', + ' title="Add">', + 'add-icon Add', '', ]) ->call('headAction')