Skip to content

Commit

Permalink
feat(order-grid): add bulk order actions
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Sep 18, 2023
1 parent 1c13eef commit 53b2a63
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 98 deletions.
2 changes: 2 additions & 0 deletions myparcelnl.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\PrestaShop\Hooks\HasPdkCheckoutDeliveryOptionsHooks;
use MyParcelNL\PrestaShop\Hooks\HasPdkCheckoutHooks;
use MyParcelNL\PrestaShop\Hooks\HasPdkOrderGridHooks;
use MyParcelNL\PrestaShop\Hooks\HasPdkOrderHooks;
use MyParcelNL\PrestaShop\Hooks\HasPdkProductHooks;
use MyParcelNL\PrestaShop\Hooks\HasPdkRenderHooks;
Expand All @@ -28,6 +29,7 @@ class MyParcelNL extends CarrierModule
{
use HasPdkCheckoutDeliveryOptionsHooks;
use HasPdkCheckoutHooks;
use HasPdkOrderGridHooks;
use HasPdkOrderHooks;
use HasPdkProductHooks;
use HasPdkRenderHooks;
Expand Down
4 changes: 2 additions & 2 deletions src/Grid/Column/MyParcelOrderColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public function __construct()
* Corresponds to the filename of the template that will be used for rendering the column.
*
* @return string
* @see /views/PrestaShop/Admin/Common/Grid/Columns/Content/order_box.html.twig
* @see /views/PrestaShop/Admin/Common/Grid/Columns/Content/order_grid_item.html.twig
*/
public function getType(): string
{
return 'order_box';
return 'order_grid_item';
}

/**
Expand Down
97 changes: 97 additions & 0 deletions src/Hooks/HasPdkOrderGridHooks.php
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());
}
}
19 changes: 19 additions & 0 deletions src/Hooks/HasPdkOrderHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use CustomerMessage;
use CustomerThread;
use MyParcelNL\Pdk\App\Api\Backend\PdkBackendActions;
use MyParcelNL\Pdk\App\Order\Contract\PdkOrderRepositoryInterface;
use MyParcelNL\Pdk\Facade\Actions;
use MyParcelNL\Pdk\Facade\Frontend;
use MyParcelNL\Pdk\Facade\Pdk;

trait HasPdkOrderHooks
{
Expand All @@ -26,4 +29,20 @@ public function hookActionObjectCustomerMessageAddAfter(array $params): void
]);
}
}

/**
* Renders the order box on a single order page.
*
* @param array $params
*
* @return string
*/
public function hookDisplayAdminOrderMain(array $params): string
{
/** @var PdkOrderRepositoryInterface $repository */
$repository = Pdk::get(PdkOrderRepositoryInterface::class);
$order = $repository->get($params['id_order']);

return Frontend::renderOrderBox($order);
}
}
62 changes: 0 additions & 62 deletions src/Hooks/HasPdkRenderHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,10 @@

namespace MyParcelNL\PrestaShop\Hooks;

use MyParcelNL\Pdk\App\Order\Contract\PdkOrderRepositoryInterface;
use MyParcelNL\Pdk\Facade\Frontend;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\PrestaShop\Grid\Column\MyParcelOrderColumn;
use PrestaShop\PrestaShop\Core\Grid\Record\RecordCollection;

trait HasPdkRenderHooks
{
/**
* Add the "MyParcel" column to the order grid to render the order boxes in.
*
* @param array $params
*
* @return void
*/
public function hookActionOrderGridDefinitionModifier(array $params): void
{
/** @var \PrestaShop\PrestaShop\Core\Grid\Definition\GridDefinitionInterface $definition */
$definition = $params['definition'];

$definition
->getColumns()
->addBefore(Pdk::get('orderColumnBefore'), new MyParcelOrderColumn());
}

/**
* Renders MyParcel buttons in order grid.
*
* @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())
);
}

/**
* Renders the notification area.
*
Expand All @@ -76,20 +30,4 @@ public function hookDisplayAdminEndContent(): string
{
return Frontend::renderInitScript();
}

/**
* Renders the shipment card on a single order page.
*
* @param array $params
*
* @return string
*/
public function hookDisplayAdminOrderMain(array $params): string
{
/** @var PdkOrderRepositoryInterface $repository */
$repository = Pdk::get(PdkOrderRepositoryInterface::class);
$order = $repository->get($params['id_order']);

return Frontend::renderOrderBox($order);
}
}

This file was deleted.

30 changes: 30 additions & 0 deletions views/js/backend/admin/src/functions/listenForBulkActions.ts
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)});
});
});
};
5 changes: 5 additions & 0 deletions views/js/backend/admin/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
bootstrap4Config,
} from '@myparcel-pdk/admin-preset-bootstrap4';
import {LogLevel, createPdkAdmin, type ElementInstance} from '@myparcel-pdk/admin';
import {listenForBulkActions} from './functions/listenForBulkActions';
import {
PsDropdownButton,
PsFormGroup,
Expand Down Expand Up @@ -130,5 +131,9 @@ window.onload = () => {

return `myparcelnl-${form.name}-${name}`;
},

onInitialized() {
listenForBulkActions();
},
});
};

0 comments on commit 53b2a63

Please sign in to comment.