Skip to content

Commit

Permalink
feature(BatchSelection): add choice to display or not the modal confi…
Browse files Browse the repository at this point in the history
…rmation
  • Loading branch information
Kinhelm committed Nov 5, 2024
1 parent 2bc71ea commit 8c9ee47
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
10 changes: 9 additions & 1 deletion assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,16 +338,24 @@ class App {
.replace('%action_name%', actionName)
.replace('%num_items%', selectedItems.length.toString());

if (actionElement.getAttribute('data-display-batch-modal-confirmation') === 'false') {
batchFormSubmit(actionElement, selectedItems);
}

document.querySelector('#modal-batch-action-button').addEventListener('click', () => {
// prevent double submission of the batch action form
actionElement.setAttribute('disabled', 'disabled');
batchFormSubmit(actionElement, selectedItems);
});

function batchFormSubmit(actionElement, selectedItems) {
const batchFormFields = {
'batchActionName': actionElement.getAttribute('data-action-name'),
'entityFqcn': actionElement.getAttribute('data-entity-fqcn'),
'batchActionUrl': actionElement.getAttribute('data-action-url'),
'batchActionCsrfToken': actionElement.getAttribute('data-action-csrf-token'),
};

selectedItems.forEach((item, i) => {
batchFormFields[`batchActionEntityIds[${i}]`] = item.value;
});
Expand All @@ -365,7 +373,7 @@ class App {

document.body.appendChild(batchForm);
batchForm.submit();
});
}
});
});
}
Expand Down
14 changes: 14 additions & 0 deletions doc/actions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,20 @@ they can link to a CRUD controller method, to a Symfony route or to some URL.
If there's at least one batch action, the backend interface is updated to add some
"checkboxes" that allow selecting more than one row of the index listing.

By default, batch processes open a confirmation modal before proceeding with the action.
This is useful for batch deletions.
However, if you do not want the confirmation modal, use the ``displayBatchConfirmationModal()`` method::

// ...

return $actions
->addBatchAction(Action::new('approve', 'Approve Users')
->linkToCrudAction('approveUsers')
->addCssClass('btn btn-primary')
->setIcon('fa fa-user-check')
->displayBatchConfirmationModal(false)) // <- here
;

When the user clicks on the batch action link/button, a form is submitted using
the ``POST`` method to the action or route configured in the action. The easiest
way to get the submitted data is to type-hint some argument of your batch action
Expand Down
7 changes: 7 additions & 0 deletions src/Config/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ public function createAsBatchAction(): self
return $this;
}

public function displayBatchConfirmationModal(bool $value = true): self
{
$this->dto->setBatchConfirmationModal($value);

return $this;
}

/**
* @param TranslatableInterface|string|false|null $label Use FALSE to hide the label; use NULL to autogenerate it
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Dto/ActionDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class ActionDto
private $url;
private array $translationParameters = [];
private $displayCallable;
private ?bool $batchConfirmationModal = true;

public function getType(): string
{
Expand Down Expand Up @@ -261,6 +262,16 @@ public function setDisplayCallable(callable $displayCallable): void
$this->displayCallable = $displayCallable;
}

public function getBatchConfirmationModal(): ?bool
{
return $this->batchConfirmationModal;
}

public function setBatchConfirmationModal(?bool $batchConfirmationModal): void
{
$this->batchConfirmationModal = $batchConfirmationModal;
}

/**
* @internal
*/
Expand Down
8 changes: 7 additions & 1 deletion src/Factory/ActionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,19 @@ private function processAction(string $pageName, ActionDto $actionDto, ?EntityDt

if ($actionDto->isBatchAction()) {
$actionDto->addHtmlAttributes([
'data-bs-toggle' => 'modal',
'data-bs-target' => '#modal-batch-action',
'data-action-csrf-token' => $this->csrfTokenManager?->getToken('ea-batch-action-'.$actionDto->getName()),
'data-action-batch' => 'true',
'data-entity-fqcn' => $adminContext->getCrud()->getEntityFqcn(),
'data-display-batch-modal-confirmation' => $actionDto->getBatchConfirmationModal() ? 'true' : 'false',
'data-action-url' => $actionDto->getLinkUrl(),
]);

if ($actionDto->getBatchConfirmationModal()) {
$actionDto->addHtmlAttributes([
'data-bs-toggle' => 'modal',
]);
}
}

return $actionDto;
Expand Down

0 comments on commit 8c9ee47

Please sign in to comment.