-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin-ui): Add product variant bulk actions (assign/delete chann…
…el, delete) (#2238) * fix(admin-ui): Fix product variant bulk action for deletion * feat(admin-ui): Add bulk action for removing prod-variants from channel * feat(admin-ui): Add bulk action for assigning product variants to channels This also refactors the fetching of variants in the dialog component.
- Loading branch information
1 parent
60c9e86
commit b25ddcd
Showing
8 changed files
with
171 additions
and
87 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
68 changes: 0 additions & 68 deletions
68
...admin-ui/src/lib/catalog/src/components/product-variant-list/product-list-bulk-actions.ts
This file was deleted.
Oops, something went wrong.
120 changes: 120 additions & 0 deletions
120
.../src/lib/catalog/src/components/product-variant-list/product-variant-list-bulk-actions.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,120 @@ | ||
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker'; | ||
import { | ||
BulkAction, | ||
DataService, | ||
DeletionResult, | ||
GetProductVariantListQuery, | ||
ItemOf, | ||
ModalService, | ||
NotificationService, | ||
Permission, | ||
createBulkRemoveFromChannelAction, | ||
isMultiChannel, | ||
} from '@vendure/admin-ui/core'; | ||
import { unique } from '@vendure/common/lib/unique'; | ||
import { EMPTY } from 'rxjs'; | ||
import { map, switchMap } from 'rxjs/operators'; | ||
|
||
import { ProductVariant } from 'package/core'; | ||
import { AssignProductsToChannelDialogComponent } from '../assign-products-to-channel-dialog/assign-products-to-channel-dialog.component'; | ||
import { ProductVariantListComponent } from './product-variant-list.component'; | ||
|
||
export const assignProductVariantsToChannelBulkAction: BulkAction< | ||
ItemOf<GetProductVariantListQuery, 'productVariants'>, | ||
ProductVariantListComponent | ||
> = { | ||
location: 'product-variant-list', | ||
label: _('catalog.assign-to-channel'), | ||
icon: 'layers', | ||
requiresPermission: userPermissions => | ||
userPermissions.includes(Permission.UpdateCatalog) || | ||
userPermissions.includes(Permission.UpdateProduct), | ||
isVisible: ({ injector }) => isMultiChannel(injector.get(DataService)), | ||
onClick: ({ injector, selection, clearSelection }) => { | ||
const modalService = injector.get(ModalService); | ||
modalService | ||
.fromComponent(AssignProductsToChannelDialogComponent, { | ||
size: 'lg', | ||
locals: { | ||
productVariantIds: unique(selection.map(p => p.id)), | ||
currentChannelIds: [], | ||
}, | ||
}) | ||
.subscribe(result => { | ||
if (result) { | ||
clearSelection(); | ||
} | ||
}); | ||
}, | ||
}; | ||
|
||
export const removeProductVariantsFromChannelBulkAction = createBulkRemoveFromChannelAction< | ||
ItemOf<GetProductVariantListQuery, 'productVariants'> | ||
>({ | ||
location: 'product-variant-list', | ||
requiresPermission: userPermissions => | ||
userPermissions.includes(Permission.UpdateCatalog) || | ||
userPermissions.includes(Permission.UpdateProduct), | ||
getItemName: item => item.name, | ||
bulkRemoveFromChannel: (dataService, ids, channelId) => | ||
dataService.product | ||
.removeVariantsFromChannel({ | ||
channelId: channelId, | ||
productVariantIds: ids, | ||
}) | ||
.pipe(map(res => res.removeProductVariantsFromChannel)), | ||
}); | ||
|
||
export const deleteProductVariantsBulkAction: BulkAction<ProductVariant, ProductVariantListComponent> = { | ||
location: 'product-variant-list', | ||
label: _('common.delete'), | ||
icon: 'trash', | ||
iconClass: 'is-danger', | ||
requiresPermission: userPermissions => | ||
userPermissions.includes(Permission.DeleteProduct) || | ||
userPermissions.includes(Permission.DeleteCatalog), | ||
onClick: ({ injector, selection, hostComponent, clearSelection }) => { | ||
const modalService = injector.get(ModalService); | ||
const dataService = injector.get(DataService); | ||
const notificationService = injector.get(NotificationService); | ||
modalService | ||
.dialog({ | ||
title: _('common.confirm-bulk-delete'), | ||
translationVars: { | ||
count: selection.length, | ||
}, | ||
buttons: [ | ||
{ type: 'secondary', label: _('common.cancel') }, | ||
{ type: 'danger', label: _('common.delete'), returnValue: true }, | ||
], | ||
}) | ||
.pipe( | ||
switchMap(response => | ||
response | ||
? dataService.product.deleteProductVariants(unique(selection.map(p => p.id))) | ||
: EMPTY, | ||
), | ||
) | ||
.subscribe(result => { | ||
let deleted = 0; | ||
const errors: string[] = []; | ||
for (const item of result.deleteProductVariants) { | ||
if (item.result === DeletionResult.DELETED) { | ||
deleted++; | ||
} else if (item.message) { | ||
errors.push(item.message); | ||
} | ||
} | ||
if (0 < deleted) { | ||
notificationService.success(_('catalog.notify-bulk-delete-products-success'), { | ||
count: deleted, | ||
}); | ||
} | ||
if (0 < errors.length) { | ||
notificationService.error(errors.join('\n')); | ||
} | ||
hostComponent.refresh(); | ||
clearSelection(); | ||
}); | ||
}, | ||
}; |
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
Oops, something went wrong.