-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(order-grid): add bulk order actions
- Loading branch information
1 parent
1c13eef
commit 53b2a63
Showing
9 changed files
with
155 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelNL\PrestaShop\Hooks; | ||
|
||
use MyParcelNL\Pdk\App\Order\Contract\PdkOrderRepositoryInterface; | ||
use MyParcelNL\Pdk\Facade\Frontend; | ||
use MyParcelNL\Pdk\Facade\Language; | ||
use MyParcelNL\Pdk\Facade\Pdk; | ||
use MyParcelNL\PrestaShop\Grid\Column\MyParcelOrderColumn; | ||
use PrestaShop\PrestaShop\Core\Grid\Action\Bulk\Type\ButtonBulkAction; | ||
use PrestaShop\PrestaShop\Core\Grid\Definition\GridDefinitionInterface; | ||
use PrestaShop\PrestaShop\Core\Grid\Record\RecordCollection; | ||
|
||
/** | ||
* Modifies the order grid | ||
*/ | ||
trait HasPdkOrderGridHooks | ||
{ | ||
/** | ||
* Extends the order grid actions and columns. | ||
* | ||
* @param array $params | ||
* | ||
* @return void | ||
*/ | ||
public function hookActionOrderGridDefinitionModifier(array $params): void | ||
{ | ||
/** @var \PrestaShop\PrestaShop\Core\Grid\Definition\GridDefinitionInterface $definition */ | ||
$definition = $params['definition']; | ||
|
||
$this->addColumn($definition); | ||
$this->addBulkActions($definition); | ||
} | ||
|
||
/** | ||
* Renders the pdk order list item in our created MyParcel column. | ||
* | ||
* @param array $params | ||
* | ||
* @return void | ||
* @throws \PrestaShopDatabaseException | ||
* @throws \PrestaShopException | ||
* @throws \Exception | ||
*/ | ||
public function hookActionOrderGridPresenterModifier(array &$params): void | ||
{ | ||
$params['presented_grid']['data']['records'] = new RecordCollection( | ||
array_map(static function (array $row) { | ||
/** @var PdkOrderRepositoryInterface $repository */ | ||
$repository = Pdk::get(PdkOrderRepositoryInterface::class); | ||
$order = $repository->get($row['id_order']); | ||
|
||
$row['myparcel'] = Frontend::renderOrderListItem($order); | ||
|
||
return $row; | ||
}, $params['presented_grid']['data']['records']->all()) | ||
); | ||
} | ||
|
||
/** | ||
* @param \PrestaShop\PrestaShop\Core\Grid\Definition\GridDefinitionInterface $definition | ||
* | ||
* @return void | ||
*/ | ||
private function addBulkActions(GridDefinitionInterface $definition): void | ||
{ | ||
$appInfo = Pdk::getAppInfo(); | ||
$bulkActions = $definition->getBulkActions(); | ||
|
||
foreach (Pdk::get('bulkActions') as $bulkAction) { | ||
$id = "$appInfo->name-$bulkAction"; | ||
$action = new ButtonBulkAction($id); | ||
|
||
$translation = sprintf('%s: %s', $appInfo->title, Language::translate($bulkAction)); | ||
|
||
$action | ||
->setName($translation) | ||
->setOptions(['class' => $id]); | ||
|
||
$bulkActions->add($action); | ||
} | ||
} | ||
|
||
/** | ||
* @param \PrestaShop\PrestaShop\Core\Grid\Definition\GridDefinitionInterface $definition | ||
* | ||
* @return void | ||
*/ | ||
private function addColumn(GridDefinitionInterface $definition): void | ||
{ | ||
$definition | ||
->getColumns() | ||
->addBefore(Pdk::get('orderColumnBefore'), new MyParcelOrderColumn()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
views/PrestaShop/Admin/Common/Grid/Actions/Bulk/icon_button.html.twig
This file was deleted.
Oops, something went wrong.
File renamed without changes.
30 changes: 30 additions & 0 deletions
30
views/js/backend/admin/src/functions/listenForBulkActions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {globalLogger} from '@myparcel-pdk/admin-core'; | ||
import {AdminAction, useActionStore} from '@myparcel-pdk/admin'; | ||
|
||
const BULK_ACTION_PREFIX = 'myparcelnl'; | ||
|
||
const BULK_ACTION_MAP = Object.freeze({ | ||
action_edit: AdminAction.OrdersEdit, | ||
action_export: AdminAction.OrdersExport, | ||
action_export_print: AdminAction.OrdersExportPrint, | ||
action_print: AdminAction.OrdersPrint, | ||
}); | ||
|
||
export const listenForBulkActions = (): void => { | ||
Object.entries(BULK_ACTION_MAP).forEach(([key, action]) => { | ||
const button = document.querySelector<HTMLElement>(`.${BULK_ACTION_PREFIX}-${key}`); | ||
|
||
if (!button) { | ||
globalLogger.error(`Could not find bulk action button for ${key}`); | ||
return; | ||
} | ||
|
||
button.addEventListener('click', (event) => { | ||
const actionStore = useActionStore(); | ||
|
||
const orderCheckboxes = document.querySelectorAll<HTMLInputElement>('.js-bulk-action-checkbox:checked'); | ||
|
||
void actionStore.dispatch(action, {orderIds: [...orderCheckboxes].map((el) => el.value)}); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters