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

Add the refresh_action #180

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions docs/src/docs/components/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -672,3 +672,11 @@ Inside the template, we can render the modal however we want:
```

For more details, see the [`ModalActionType`](../../reference/types/action/modal.md) reference page.

## Refresh actions

You have the option to add an action that refreshes the content of the DataTable.

Thanks to `Hotwire Turbo`, only the content of the DataTable is refreshed.

This action cannot be added as a `RowAction` or `BatchAction`.
2 changes: 1 addition & 1 deletion docs/src/docs/features/asynchronicity.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ which wraps the whole table in the `<turbo-frame>` tag:
```twig
{# @KreyuDataTable/themes/base.html.twig #}
{% block kreyu_data_table %}
<turbo-frame id="kreyu_data_table_{{ name }}">
<turbo-frame id="{{ data_table_name(data_table) }}">
{# ... #}
</turbo-frame>
{% endblock %}
Expand Down
41 changes: 41 additions & 0 deletions src/Action/ActionRefreshUrlGenerator.php
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this class (and its interface) at all? I think refreshing the data table should always use the current URL, which (I believe) we can retrieve directly in Twig?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our project, we sometimes return the same DataTable in multiple views. To avoid instantiating this DataTable in several places (so as to keep the same options), we created an endpoint that returns only the content of this DataTable.

I wanted to replicate this behavior.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\Action;

use Kreyu\Bundle\DataTableBundle\Exception\LogicException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class ActionRefreshUrlGenerator implements ActionRefreshUrlGeneratorInterface
{
public function __construct(
private RequestStack $requestStack,
private UrlGeneratorInterface $urlGenerator,
) {
}

public function generate(): string
{
$request = $this->getRequest();

$route = $request->attributes->get('_route');
$routeParams = $request->attributes->get('_route_params', []);
$queryParams = $request->query->all();

$parameters = [...$routeParams, ...$queryParams];

return $this->urlGenerator->generate($route, $parameters);
}

private function getRequest(): Request
{
if (null === $request = $this->requestStack->getCurrentRequest()) {
throw new LogicException('Unable to retrieve current request.');
}

return $request;
}
}
9 changes: 9 additions & 0 deletions src/Action/ActionRefreshUrlGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\Action;

interface ActionRefreshUrlGeneratorInterface
{
public function generate(): string;
}
29 changes: 29 additions & 0 deletions src/Action/Type/RefreshActionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Kreyu\Bundle\DataTableBundle\Action\Type;

use Kreyu\Bundle\DataTableBundle\Action\ActionContext;
use Kreyu\Bundle\DataTableBundle\Action\ActionInterface;
use Kreyu\Bundle\DataTableBundle\Action\ActionRefreshUrlGeneratorInterface;
use Kreyu\Bundle\DataTableBundle\Action\ActionView;

class RefreshActionType extends AbstractActionType
{
public function __construct(
private ActionRefreshUrlGeneratorInterface $columnRefreshUrlGenerator,
) {
}

public function buildView(ActionView $view, ActionInterface $action, array $options): void
{
if (ActionContext::Global !== $action->getConfig()->getContext()) {
throw new \LogicException(sprintf('A %s action can only be added as a global action.', $this::class));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call 👍🏻 It could be used as row or batch action, but that would be unintuitive.

}

$view->vars = array_replace($view->vars, [
'href' => $this->columnRefreshUrlGenerator->generate(),
]);
}
}
21 changes: 20 additions & 1 deletion src/Resources/config/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@

use Kreyu\Bundle\DataTableBundle\Action\ActionFactory;
use Kreyu\Bundle\DataTableBundle\Action\ActionFactoryInterface;
use Kreyu\Bundle\DataTableBundle\Action\ActionRefreshUrlGenerator;
use Kreyu\Bundle\DataTableBundle\Action\ActionRegistry;
use Kreyu\Bundle\DataTableBundle\Action\ActionRegistryInterface;
use Kreyu\Bundle\DataTableBundle\Action\ActionRefreshUrlGeneratorInterface;
use Kreyu\Bundle\DataTableBundle\Action\Type\ActionType;
use Kreyu\Bundle\DataTableBundle\Action\Type\ButtonActionType;
use Kreyu\Bundle\DataTableBundle\Action\Type\Dropdown\DropdownActionType;
use Kreyu\Bundle\DataTableBundle\Action\Type\Dropdown\LinkDropdownItemActionType;
use Kreyu\Bundle\DataTableBundle\Action\Type\FormActionType;
use Kreyu\Bundle\DataTableBundle\Action\Type\LinkActionType;
use Kreyu\Bundle\DataTableBundle\Action\Type\ModalActionType;
use Kreyu\Bundle\DataTableBundle\Action\Type\RefreshActionType;
use Kreyu\Bundle\DataTableBundle\Action\Type\ResolvedActionTypeFactory;
use Kreyu\Bundle\DataTableBundle\Action\Type\ResolvedActionTypeFactoryInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator;

Expand Down Expand Up @@ -55,6 +57,23 @@
->tag('kreyu_data_table.action.type')
;

$services
->set('kreyu_data_table.action.type.refresh', RefreshActionType::class)
->tag('kreyu_data_table.action.type')
->args([service(ActionRefreshUrlGeneratorInterface::class)])
;


$services
->set('kreyu_data_table.action.action_refresh_url_generator', ActionRefreshUrlGenerator::class)
->args([
service('request_stack'),
service(UrlGeneratorInterface::class),
])
->alias(ActionRefreshUrlGeneratorInterface::class, 'kreyu_data_table.action.action_refresh_url_generator')
;


$services
->set('kreyu_data_table.action.type.button', ButtonActionType::class)
->tag('kreyu_data_table.action.type')
Expand Down
12 changes: 10 additions & 2 deletions src/Resources/views/themes/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{% set stimulus_controllers = stimulus_controllers|merge(['kreyu--data-table-bundle--batch']) %}
{% endif %}

<turbo-frame id="kreyu_data_table_{{ name }}" target="_top">
<turbo-frame id="{{ data_table_name(data_table) }}" target="_top">
<div
data-controller="{{ stimulus_controllers|join(' ') }}"
data-kreyu--data-table-bundle--state-url-query-parameters-value="{{ url_query_parameters|default({})|json_encode }}"
Expand All @@ -33,7 +33,7 @@
{% set stimulus_controllers = stimulus_controllers|merge(['kreyu--data-table-bundle--batch']) %}
{% endif %}

<turbo-frame id="kreyu_data_table_{{ name }}" target="_top">
<turbo-frame id="{{ data_table_name(data_table) }}" target="_top">
<div
data-controller="{{ stimulus_controllers|join(' ') }}"
data-kreyu--data-table-bundle--state-url-query-parameters-value="{{ url_query_parameters|default({})|json_encode }}"
Expand Down Expand Up @@ -621,6 +621,14 @@
</a>
{% endblock %}

{% block action_refresh_control %}
{% set attr = { href }|filter(v => v != null)|merge(attr|default({'data-turbo-frame': data_table_name(data_table) })) %}

<a {% with { attr } %}{{- block('attributes') -}}{% endwith %}>
{% with { attr: {} } %}{{- block('action_control', theme, _context) -}}{% endwith %}
</a>
{% endblock %}

{% block action_form_control %}
{% set attr = { action, method: html_friendly_method }|merge(attr|default({})) %}

Expand Down
8 changes: 8 additions & 0 deletions src/Resources/views/themes/bootstrap_5.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,14 @@
{{ parent() }}
{% endblock %}

{% block action_refresh_control %}
{% set attr = {
class: 'btn btn-primary',
}|merge(attr) %}

{{ parent() }}
{% endblock %}

{% block action_form_control %}
{% set attr = { class: 'd-inline-block' }|merge(attr) %}

Expand Down
6 changes: 6 additions & 0 deletions src/Twig/DataTableExtension.php
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this function is needed, since that identifier is used primarily for the turbo frame. If I call the data_table_name(data_table) I would expect to retrieve its name (data_table.vars.name), without any prefix though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, but I don't like the idea of having an identification key that is hard-coded in a view and used in several different places.

Do you have any suggestions on how to change this approach? Perhaps a more explicit function name, such as data_table_turbo_identifier?

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __construct(
public function getFunctions(): array
{
$definitions = [
'data_table_name' => $this->getDataTableName(...),
'data_table' => $this->renderDataTable(...),
'data_table_table' => $this->renderDataTableTable(...),
'data_table_action_bar' => $this->renderDataTableActionBar(...),
Expand Down Expand Up @@ -88,6 +89,11 @@ public function setDataTableThemes(DataTableView $view, array $themes, bool $onl
}
}

public function getDataTableName(Environment $environment, DataTableView $view): string
{
return 'kreyu_data_table_'.$view->vars['name'];
}

/**
* @param array<string, mixed> $variables
*
Expand Down