|
|
@@ -1,15 +1,26 @@
|
|
|
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
|
|
|
-import { BulkAction, DataService, ModalService } from '@vendure/admin-ui/core';
|
|
|
-import { delay } from 'rxjs/operators';
|
|
|
+import {
|
|
|
+ BulkAction,
|
|
|
+ DataService,
|
|
|
+ DeletionResult,
|
|
|
+ ModalService,
|
|
|
+ NotificationService,
|
|
|
+ SearchProducts,
|
|
|
+} from '@vendure/admin-ui/core';
|
|
|
+import { EMPTY } from 'rxjs';
|
|
|
+import { switchMap } from 'rxjs/operators';
|
|
|
|
|
|
-export const deleteProductsBulkAction: BulkAction = {
|
|
|
+import { ProductListComponent } from './product-list.component';
|
|
|
+
|
|
|
+export const deleteProductsBulkAction: BulkAction<SearchProducts.Items, ProductListComponent> = {
|
|
|
location: 'product-list',
|
|
|
label: _('common.delete'),
|
|
|
icon: 'trash',
|
|
|
iconClass: 'is-danger',
|
|
|
- onClick: ({ injector, selection }) => {
|
|
|
+ onClick: ({ injector, selection, hostComponent, clearSelection }) => {
|
|
|
const modalService = injector.get(ModalService);
|
|
|
const dataService = injector.get(DataService);
|
|
|
+ const notificationService = injector.get(NotificationService);
|
|
|
modalService
|
|
|
.dialog({
|
|
|
title: _('catalog.confirm-bulk-delete-products'),
|
|
|
@@ -21,13 +32,32 @@ export const deleteProductsBulkAction: BulkAction = {
|
|
|
{ type: 'danger', label: _('common.delete'), returnValue: true },
|
|
|
],
|
|
|
})
|
|
|
- .pipe
|
|
|
- // switchMap(response =>
|
|
|
- // response ? dataService.product.deleteProduct(productId) : EMPTY,
|
|
|
- // ),
|
|
|
- // Short delay to allow the product to be removed from the search index before
|
|
|
- // refreshing.
|
|
|
- ()
|
|
|
- .subscribe();
|
|
|
+ .pipe(
|
|
|
+ switchMap(response =>
|
|
|
+ response ? dataService.product.deleteProducts(selection.map(p => p.productId)) : EMPTY,
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ .subscribe(result => {
|
|
|
+ let deleted = 0;
|
|
|
+ const errors: string[] = [];
|
|
|
+ for (const item of result.deleteProducts) {
|
|
|
+ if (item.result === DeletionResult.DELETED) {
|
|
|
+ deleted++;
|
|
|
+ } else if (item.message) {
|
|
|
+ errors.push(item.message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (0 < deleted) {
|
|
|
+ notificationService.success(_('common.notify-bulk-delete-success'), {
|
|
|
+ count: deleted,
|
|
|
+ entity: 'Products',
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (0 < errors.length) {
|
|
|
+ notificationService.error(errors.join('\n'));
|
|
|
+ }
|
|
|
+ hostComponent.refresh();
|
|
|
+ clearSelection();
|
|
|
+ });
|
|
|
},
|
|
|
};
|