Skip to content

Commit

Permalink
add delete combinations for all shops btn
Browse files Browse the repository at this point in the history
  • Loading branch information
zuk3975 authored and jolelievre committed Dec 16, 2022
1 parent e0bad75 commit 0d8b95c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,46 +61,42 @@ export default class BulkDeleteHandler {
}

private async init(): Promise<void> {
const bulkDeleteBtn = document.querySelector<HTMLButtonElement>(CombinationMap.bulkDeleteBtn);

if (!(bulkDeleteBtn instanceof HTMLButtonElement)) {
console.error(`${CombinationMap.bulkDeleteBtn} must be a HTMLButtonElement`);

return;
}

bulkDeleteBtn.addEventListener('click', async () => {
const selectedCombinationIds = await this.bulkChoicesSelector.getSelectedIds();

try {
const selectedCombinationsCount = selectedCombinationIds.length;
const confirmLabel = bulkDeleteBtn.dataset.modalConfirmLabel
?.replace(/%combinations_number%/, String(selectedCombinationsCount));

const modal = new ConfirmModal(
{
id: 'modal-confirm-delete-combinations',
confirmTitle: bulkDeleteBtn.innerHTML,
confirmMessage: bulkDeleteBtn.dataset.modalMessage,
confirmButtonLabel: confirmLabel,
closeButtonLabel: bulkDeleteBtn.dataset.modalCancelLabel,
closable: true,
},
async () => {
await this.bulkDelete(selectedCombinationIds);
},
);
modal.show();
} catch (error: any) {
const errorMessage = error.response?.JSON ?? error;
$.growl.error({message: errorMessage});
}
});
const bulkDeleteButtons = document.querySelectorAll<HTMLButtonElement>(CombinationMap.bulkDeleteBtn);

bulkDeleteButtons.forEach((bulkDeleteBtn: HTMLButtonElement) => {
bulkDeleteBtn.addEventListener('click', async () => {
const selectedCombinationIds = await this.bulkChoicesSelector.getSelectedIds();

try {
const selectedCombinationsCount = selectedCombinationIds.length;
const confirmLabel = bulkDeleteBtn.dataset.modalConfirmLabel
?.replace(/%combinations_number%/, String(selectedCombinationsCount));

const modal = new ConfirmModal(
{
id: 'modal-confirm-delete-combinations',
confirmTitle: bulkDeleteBtn.innerHTML,
confirmMessage: bulkDeleteBtn.dataset.modalMessage,
confirmButtonLabel: confirmLabel,
closeButtonLabel: bulkDeleteBtn.dataset.modalCancelLabel,
closable: true,
},
async () => {
await this.bulkDelete(selectedCombinationIds, bulkDeleteBtn);
},
);
modal.show();
} catch (error: any) {
const errorMessage = error.response?.JSON ?? error;
$.growl.error({message: errorMessage});
}
});
})
}

private async bulkDelete(combinationIds: number[]): Promise<void> {
const $bulkDeleteBtn = $(CombinationMap.bulkDeleteBtn);
const bulkChunkSize = Number($bulkDeleteBtn.data('bulkChunkSize'));
private async bulkDelete(combinationIds: number[], bulkDeleteBtn: HTMLButtonElement): Promise<void> {
// const $bulkDeleteBtn = $(bulkDeleteBtn);
const bulkChunkSize = Number(bulkDeleteBtn.dataset.bulkChunkSize);
const abortController = new AbortController();

const progressModal = new ProgressModal({
Expand All @@ -110,15 +106,15 @@ export default class BulkDeleteHandler {
abortController.abort();
},
closeCallback: () => this.eventEmitter.emit(CombinationEvents.bulkDeleteFinished),
progressionTitle: $bulkDeleteBtn.data('progressTitle'),
progressionMessage: $bulkDeleteBtn.data('progressMessage'),
closeLabel: $bulkDeleteBtn.data('closeLabel'),
abortProcessingLabel: $bulkDeleteBtn.data('stopProcessing'),
errorsMessage: $bulkDeleteBtn.data('errorsMessage'),
backToProcessingLabel: $bulkDeleteBtn.data('backToProcessing'),
downloadErrorLogLabel: $bulkDeleteBtn.data('downloadErrorLog'),
viewErrorLogLabel: $bulkDeleteBtn.data('viewErrorLog'),
viewErrorTitle: $bulkDeleteBtn.data('viewErrorTitle'),
progressionTitle: bulkDeleteBtn.dataset.progressTitle,
progressionMessage: bulkDeleteBtn.dataset.progressMessage,
closeLabel: bulkDeleteBtn.dataset.closeLabel,
abortProcessingLabel: bulkDeleteBtn.dataset.stopProcessing,
errorsMessage: bulkDeleteBtn.dataset.errorsMessage,
backToProcessingLabel: bulkDeleteBtn.dataset.backToProcessing,
downloadErrorLogLabel: bulkDeleteBtn.dataset.downloadErrorLog,
viewErrorLogLabel: bulkDeleteBtn.dataset.viewErrorLog,
viewErrorTitle: bulkDeleteBtn.dataset.viewErrorTitle,
total: combinationIds.length,
});
progressModal.show();
Expand All @@ -137,6 +133,7 @@ export default class BulkDeleteHandler {
const response: Response = await this.combinationsService.bulkDeleteCombinations(
this.productId,
chunkIds,
bulkDeleteBtn.id === CombinationMap.bulkDeleteBtnAllShopsId,
abortController.signal,
);

Expand Down
3 changes: 2 additions & 1 deletion admin-dev/themes/new-theme/js/pages/product/product-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ export default {
searchInput: '#product-combinations-generate .attributes-search',
generateCombinationsButton: '.generate-combinations-button',
bulkCombinationFormBtn: '#combination-bulk-form-btn',
bulkDeleteBtn: '#combination-bulk-delete-btn',
bulkDeleteBtn: '.bulk-delete-btn',
bulkDeleteBtnAllShopsId: 'combination-bulk-delete-btn-all-shops',
bulkActionBtn: '.bulk-action-btn',
bulkActionsDropdownBtn: '#combination-bulk-actions-btn',
bulkAllPreviewInput: '#bulk-all-preview',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ export default class CombinationsService {
});
}

bulkDeleteCombinations(productId: number, combinationIds: number[], abortSignal: AbortSignal): Promise<Response> {
bulkDeleteCombinations(productId: number, combinationIds: number[], allShops: boolean, abortSignal: AbortSignal): Promise<Response> {
const formData = new FormData();
formData.append('combinationIds', JSON.stringify(combinationIds));
formData.append('allShops', allShops ? '1' : '0');

return fetch(
this.router.generate('admin_products_combinations_bulk_delete', {productId}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ public function paginatedListAction(int $productId): Response
'combinationLimitChoices' => self::COMBINATIONS_PAGINATION_OPTIONS,
'combinationsLimit' => ProductCombinationFilters::LIST_LIMIT,
'combinationsForm' => $combinationsForm->createView(),
'isMultistoreActive' => $this->get('prestashop.adapter.multistore_feature')->isActive(),
'contextShopName' => $this->getContext()->shop->name,
]);
}

Expand Down Expand Up @@ -411,7 +413,7 @@ public function bulkDeleteAction(int $productId, Request $request): JsonResponse
$this->getCommandBus()->handle(new BulkDeleteCombinationCommand(
$productId,
json_decode($combinationIds),
ShopConstraint::shop($this->getContextShopId())
$request->request->get('allShops') ? ShopConstraint::allShops() : ShopConstraint::shop($this->getContextShopId())
));
} catch (Exception $e) {
if ($e instanceof BulkCombinationException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

{{ include('@PrestaShop/Admin/Sell/Catalog/Product/Combination/paginated_list_header.html.twig', {
'productId': productId,
'isMultistoreActive': isMultistoreActive,
'contextShopName': contextShopName,
}) }}

<div class="justify-content-center" id="combinations-list">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@
>
{{ 'Edit %combinations_number% combinations'|trans({'%s': ''}, 'Admin.Catalog.Feature') }}
</button>
{% if isMultistoreActive %}
{% set deleteLabel = 'Delete %combinations_number% combinations for shop "%shop%"'|trans({'%shop%': contextShopName}, 'Admin.Catalog.Feature') %}
{% else %}
{% set deleteLabel = 'Delete %combinations_number% combinations'|trans({}, 'Admin.Catalog.Feature') %}
{% endif %}
<button
id="combination-bulk-delete-btn" class="dropdown-item bulk-action-btn" type="button"
data-btn-label="{{ 'Delete %combinations_number% combinations'|trans({}, 'Admin.Catalog.Feature') }}"
id="combination-bulk-delete-btn" class="dropdown-item bulk-action-btn bulk-delete-btn" type="button"
data-btn-label="{{ deleteLabel }}"
data-form-url="{{ url('admin_products_combinations_bulk_delete', { 'productId': productId }) }}"
data-modal-message="{{ 'Are you sure you want to delete selected combinations?'|trans({}, 'Admin.Catalog.Notification') }}"
data-modal-confirm-label="{{ 'Delete %combinations_number% combinations'|trans({}, 'Admin.Catalog.Feature') }}"
Expand All @@ -63,8 +68,30 @@
data-view-error-log="{{ 'View %error_count% error logs'|trans({}, 'Admin.Catalog.Feature') }}"
data-view-error-title="{{ 'Error log'|trans({}, 'Admin.Catalog.Feature') }}"
>
{{ 'Delete %combinations_number% combinations'|trans({'%s': ''}, 'Admin.Catalog.Feature') }}
{{ deleteLabel }}
</button>
{% if isMultistoreActive %}
<button
id="combination-bulk-delete-btn-all-shops" class="dropdown-item bulk-action-btn bulk-delete-btn" type="button"
data-btn-label="{{ 'Delete %combinations_number% combinations for all shops'|trans({}, 'Admin.Catalog.Feature') }}"
data-form-url="{{ url('admin_products_combinations_bulk_delete', { 'productId': productId }) }}"
data-modal-message="{{ 'Are you sure you want to delete selected combinations?'|trans({}, 'Admin.Catalog.Notification') }}"
data-modal-confirm-label="{{ 'Delete %combinations_number% combinations'|trans({}, 'Admin.Catalog.Feature') }}"
data-modal-cancel-label="{{ 'Cancel'|trans({}, 'Admin.Actions') }}"
data-bulk-chunk-size="10"
data-progress-title="{{ 'Deleting %total% combinations'|trans({}, 'Admin.Catalog.Feature') }}"
data-progress-message="{{ 'Deleting %done%/%total% combinations'|trans({}, 'Admin.Catalog.Feature') }}"
data-close-label="{{ 'Close'|trans({}, 'Admin.Catalog.Feature') }}"
data-stop-processing="{{ 'Stop processing'|trans({}, 'Admin.Catalog.Feature') }}"
data-errors-message="{{ '%error_count% errors occurred. You can download the logs for future reference.'|trans({}, 'Admin.Catalog.Feature') }}"
data-back-to-processing="{{ 'Back to processing'|trans({}, 'Admin.Catalog.Feature') }}"
data-download-error-log="{{ 'Download error log'|trans({}, 'Admin.Catalog.Feature') }}"
data-view-error-log="{{ 'View %error_count% error logs'|trans({}, 'Admin.Catalog.Feature') }}"
data-view-error-title="{{ 'Error log'|trans({}, 'Admin.Catalog.Feature') }}"
>
{{ 'Delete %combinations_number% combinations for all shops'|trans({'%s': ''}, 'Admin.Catalog.Feature') }}
</button>
{% endif %}
</div>
</div>

Expand Down

0 comments on commit 0d8b95c

Please sign in to comment.