Refactored Media Bulk MoveTo repository notification context

This commit is contained in:
leekelleher
2024-07-17 11:48:10 +01:00
parent f367c56562
commit c6f3f24995

View File

@@ -3,9 +3,19 @@ import { UmbRepositoryBase } from '@umbraco-cms/backoffice/repository';
import { UMB_NOTIFICATION_CONTEXT } from '@umbraco-cms/backoffice/notification';
import type { UmbBulkMoveToRepository, UmbBulkMoveToRequestArgs } from '@umbraco-cms/backoffice/entity-bulk-action';
import type { UmbRepositoryErrorResponse } from '@umbraco-cms/backoffice/repository';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
export class UmbBulkMoveToMediaRepository extends UmbRepositoryBase implements UmbBulkMoveToRepository {
#moveSource = new UmbMoveMediaServerDataSource(this);
#notificationContext?: typeof UMB_NOTIFICATION_CONTEXT.TYPE;
constructor(host: UmbControllerHost) {
super(host);
this.consumeContext(UMB_NOTIFICATION_CONTEXT, (notificationContext) => {
this.#notificationContext = notificationContext;
});
}
async requestBulkMoveTo(args: UmbBulkMoveToRequestArgs): Promise<UmbRepositoryErrorResponse> {
let count = 0;
@@ -15,18 +25,16 @@ export class UmbBulkMoveToMediaRepository extends UmbRepositoryBase implements U
const { error } = await this.#moveSource.moveTo({ unique, destination });
if (error) {
const notificationContext = await this.getContext(UMB_NOTIFICATION_CONTEXT);
const notification = { data: { message: error.message } };
notificationContext.peek('danger', notification);
this.#notificationContext?.peek('danger', notification);
} else {
count++;
}
}
if (count > 0) {
const notificationContext = await this.getContext(UMB_NOTIFICATION_CONTEXT);
const notification = { data: { message: `Moved ${count} media items` } };
notificationContext.peek('positive', notification);
const notification = { data: { message: `Moved ${count} media ${count === 1 ? 'item' : 'items'}` } };
this.#notificationContext?.peek('positive', notification);
}
return {};