Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.2.0 #111

Merged
merged 4 commits into from
Oct 28, 2022
Merged

5.2.0 #111

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
5.2.0
  • Loading branch information
Arthur LORENT committed Oct 28, 2022
commit 5c8d57c9cb22dbed0c4ed755e5ae69f4922c99ad
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# 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 `RedirectShowAction`, that will be used by the pre-configured `ShowRowAction`
*

## [5.1.2](https://github.com/Okipa/laravel-table/compare/5.1.1...5.1.2)

2022-10-27
Expand Down
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ Status
* `Actions`
* `Bulk Actions`
* `Create`
* `Add`
* `Show`
* `Edit`
* `Destroy`
Expand Down Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -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')));
}
}
```
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/laravel-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
'info' => '<i class="fa-solid fa-circle-info"></i>',
'reset' => '<i class="fa-solid fa-rotate-left"></i>',
'drag_drop' => '<i class="fa-solid fa-grip-vertical"></i>',
'add' => '<i class="fa-solid fa-circle-plus fa-fw"></i>',
'create' => '<i class="fa-solid fa-circle-plus fa-fw"></i>',
'show' => '<i class="fa-solid fa-eye fa-fw"></i>',
'edit' => '<i class="fa-solid fa-pencil fa-fw"></i>',
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ parameters:
- src/
- tests/

# The level 9 is the highest level
# Level 9 is the highest level
level: 5

databaseMigrationsPath:
Expand Down
41 changes: 41 additions & 0 deletions src/HeadActions/AddHeadAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Okipa\LaravelTable\HeadActions;

use Livewire\Component;
use Okipa\LaravelTable\Abstracts\AbstractHeadAction;

class AddHeadAction extends AbstractHeadAction
{
protected RedirectHeadAction $redirectHeadAction;

public function __construct(public string $createUrl, bool $openInNewWindow = false)
{
$this->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);
}
}
63 changes: 63 additions & 0 deletions src/RowActions/RedirectRowAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Okipa\LaravelTable\RowActions;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Str;
use Livewire\Component;
use Livewire\Redirector;
use Okipa\LaravelTable\Abstracts\AbstractRowAction;

class RedirectRowAction extends AbstractRowAction
{
public function __construct(
public string $url,
public string $title,
public string $icon,
public array $class = ['link-info'],
public string|null $defaultConfirmationQuestion = null,
public string|null $defaultFeedbackMessage = null,
public bool $openInNewWindow = false,
)
{
//
}

protected function identifier(): string
{
return 'row_action_' . Str::snake($this->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);
}
}
27 changes: 17 additions & 10 deletions src/RowActions/ShowRowAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,50 @@

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);
}
}
6 changes: 3 additions & 3 deletions tests/Unit/Bootstrap5/TableHeadActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Livewire\Livewire;
use Okipa\LaravelTable\Abstracts\AbstractTableConfiguration;
use Okipa\LaravelTable\Column;
use Okipa\LaravelTable\HeadActions\CreateHeadAction;
use Okipa\LaravelTable\HeadActions\AddHeadAction;
use Okipa\LaravelTable\Table;
use Tests\Models\User;
use Tests\TestCase;
Expand All @@ -26,7 +26,7 @@ public function it_can_set_table_head_action(): void
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
Expand Down Expand Up @@ -60,7 +60,7 @@ public function it_can_allow_head_action_conditionally(): void
protected function table(): Table
{
return Table::make()->model(User::class)
->headAction((new CreateHeadAction(route('user.create'), true))->when(false));
->headAction((new AddHeadAction(route('user.create'), true))->when(false));
}

protected function columns(): array
Expand Down