Skip to content

PoC view talks filter by multiple speakers on a bulk action of the grid #261

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

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
2 changes: 1 addition & 1 deletion app/Entity/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getPath(): ?string
return $this->path;
}

public function setPath(string $path): void
public function setPath(?string $path): void
{
$this->path = $path;
}
Expand Down
6 changes: 6 additions & 0 deletions app/Entity/Speaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use App\Form\SpeakerType;
use App\Grid\SpeakerGrid;
use App\Repository\SpeakerRepository;
use App\State\Responder\RedirectToSpeakerTalksResponder;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
Expand All @@ -38,6 +39,11 @@
new Create(),
new Update(),
new Index(grid: SpeakerGrid::class),
new Index(
shortName: 'redirect_to_speaker_talks',
responder: RedirectToSpeakerTalksResponder::class,
repositoryMethod: 'findById',
),
new Delete(),
new BulkDelete(),
],
Expand Down
2 changes: 2 additions & 0 deletions app/Grid/SpeakerGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
->addActionGroup(
BulkActionGroup::create(
DeleteAction::create(),
Action::create('redirect_to_speaker_talks', 'redirect_to_speaker_talks')
->setLabel('View talks'),
),
)
;
Expand Down
1 change: 1 addition & 0 deletions app/Grid/TalkGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
->addFilter(
Filter::create(name: 'speaker', type: SpeakerFilter::class)
->setLabel('app.ui.speaker')
->setFormOptions(['multiple' => true])
->setOptions(['fields' => ['speakers.id']]),
)
->addFilter(
Expand Down
48 changes: 48 additions & 0 deletions app/State/Responder/RedirectToSpeakerTalksResponder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\State\Responder;

use App\Entity\Speaker;
use Sylius\Resource\Context\Context;
use Sylius\Resource\Metadata\Operation;
use Sylius\Resource\State\ResponderInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Webmozart\Assert\Assert;

final class RedirectToSpeakerTalksResponder implements ResponderInterface
{
public function __construct(
private readonly RouterInterface $router,
) {
}

/**
* @param Speaker[] $data
*/
public function respond(mixed $data, Operation $operation, Context $context): RedirectResponse
{
Assert::allIsInstanceOf($data, Speaker::class);

$ids = array_map(fn (Speaker $speaker) => $speaker->getId(), $data);

$path = $this->router->generate('app_admin_talk_index', [
'criteria' => [
'speaker' => $ids,
],
]);

return new RedirectResponse($path);
}
}
2 changes: 2 additions & 0 deletions config/packages/sylius_grid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ sylius_grid:
templates:
filter:
'App\Grid\Filter\SpeakerFilter': '@SyliusBootstrapAdminUi/shared/grid/filter/entity.html.twig'
bulk_action:
'redirect_to_speaker_talks': 'shared/grid/bulk_action/redirect_to_speaker_talks.html.twig'
2 changes: 1 addition & 1 deletion src/BootstrapAdminUi/assets/entrypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import './styles/main.scss';

import './scripts/bulk-delete';
import './scripts/bulk-action';
import './scripts/check-all';
import './scripts/menu-search';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

/* global document */

function syliusBulkDelete(form) {
const groupName = form.getAttribute('data-bulk-delete');
function syliusBulkAction(form) {
const groupName = form.getAttribute('data-bulk-action');
const groupItems = Array.from(document.querySelectorAll(`input[data-check-all-group="${groupName}"]`));

form.addEventListener('submit', (e) => {
Expand All @@ -31,5 +31,5 @@ function syliusBulkDelete(form) {
}

(function () {
document.querySelectorAll('[data-bulk-delete]').forEach(syliusBulkDelete);
document.querySelectorAll('[data-bulk-action]').forEach(syliusBulkAction);
}());
2 changes: 1 addition & 1 deletion src/BootstrapAdminUi/public/app.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% set path = options.link.url|default(path(options.link.route|default(grid.requestConfiguration.getRouteName('bulk_delete')), options.link.parameters|default({}))) %}

{% component 'sylius_bootstrap_admin_ui:delete_modal' with {id: 'bulk_delete', path: path, type: 'bulk-delete', form_attr: 'data-bulk-delete=index'} %}
{% component 'sylius_bootstrap_admin_ui:delete_modal' with {id: 'bulk_delete', path: path, type: 'bulk-delete', form_attr: 'data-bulk-action=index'} %}
{% import '@SyliusBootstrapAdminUi/shared/helper/button.html.twig' as button %}

{% block trigger %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% import '@SyliusBootstrapAdminUi/shared/helper/button.html.twig' as button %}

{% set label = action.label|trans %}
{% set path = path('app_admin_speaker_redirect_to_speaker_talks') %}
{% set form_attr = 'data-bulk-action=index' %}

<form action="{{ path }}" method="GET" {{ form_attr }}>
{{ button.default({
text: label|trans,
type: 'submit',
class: 'btn-ghost-primary',
disabled: true,
attr: 'data-check-all-action=index',
icon: 'tabler:list-letters'
}) }}
</form>