Skip to content

Commit

Permalink
Merge pull request DSpace#1886 from atmire/fix-1882
Browse files Browse the repository at this point in the history
Fix dual notification when deleting bitstreamformats
  • Loading branch information
tdonohue authored Oct 6, 2022
2 parents 8ffc325 + f5deb88 commit 1d4f2ed
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ import { TestScheduler } from 'rxjs/testing';
import {
createNoContentRemoteDataObject$,
createSuccessfulRemoteDataObject,
createSuccessfulRemoteDataObject$
createSuccessfulRemoteDataObject$,
createFailedRemoteDataObject$
} from '../../../shared/remote-data.utils';
import { createPaginatedList } from '../../../shared/testing/utils.test';
import { PaginationComponentOptions } from '../../../shared/pagination/pagination-component-options.model';
import { SortDirection, SortOptions } from '../../../core/cache/models/sort-options.model';
import { PaginationService } from '../../../core/pagination/pagination.service';
import { PaginationServiceStub } from '../../../shared/testing/pagination-service.stub';
import { FindListOptions } from '../../../core/data/find-list-options.model';

describe('BitstreamFormatsComponent', () => {
let comp: BitstreamFormatsComponent;
Expand Down Expand Up @@ -85,10 +83,6 @@ describe('BitstreamFormatsComponent', () => {
];
const mockFormatsRD = createSuccessfulRemoteDataObject(createPaginatedList(mockFormatsList));

const pagination = Object.assign(new PaginationComponentOptions(), { currentPage: 1, pageSize: 20 });
const sort = new SortOptions('score', SortDirection.DESC);
const findlistOptions = Object.assign(new FindListOptions(), { currentPage: 1, elementsPerPage: 20 });

const initAsync = () => {
notificationsServiceStub = new NotificationsServiceStub();

Expand Down Expand Up @@ -246,7 +240,7 @@ describe('BitstreamFormatsComponent', () => {
));

beforeEach(initBeforeEach);
it('should clear bitstream formats ', () => {
it('should clear bitstream formats and show a success notification', () => {
comp.deleteFormats();

expect(bitstreamFormatService.clearBitStreamFormatRequests).toHaveBeenCalled();
Expand Down Expand Up @@ -275,7 +269,7 @@ describe('BitstreamFormatsComponent', () => {
selectBitstreamFormat: {},
deselectBitstreamFormat: {},
deselectAllBitstreamFormats: {},
delete: observableOf(false),
delete: createFailedRemoteDataObject$(),
clearBitStreamFormatRequests: observableOf('cleared')
});

Expand All @@ -295,7 +289,7 @@ describe('BitstreamFormatsComponent', () => {
));

beforeEach(initBeforeEach);
it('should clear bitstream formats ', () => {
it('should clear bitstream formats and show an error notification', () => {
comp.deleteFormats();

expect(bitstreamFormatService.clearBitStreamFormatRequests).toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { PaginatedList } from '../../../core/data/paginated-list.model';
import { PaginationComponentOptions } from '../../../shared/pagination/pagination-component-options.model';
import { BitstreamFormat } from '../../../core/shared/bitstream-format.model';
import { BitstreamFormatDataService } from '../../../core/data/bitstream-format-data.service';
import { map, switchMap, take } from 'rxjs/operators';
import { map, mergeMap, switchMap, take, toArray } from 'rxjs/operators';
import { hasValue } from '../../../shared/empty.util';
import { NotificationsService } from '../../../shared/notifications/notifications.service';
import { Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { NoContent } from '../../../core/shared/NoContent.model';
import { PaginationService } from '../../../core/pagination/pagination.service';
import { FindListOptions } from '../../../core/data/find-list-options.model';
import { getFirstCompletedRemoteData } from '../../../core/shared/operators';

/**
* This component renders a list of bitstream formats
Expand Down Expand Up @@ -58,31 +59,39 @@ export class BitstreamFormatsComponent implements OnInit, OnDestroy {
* Deletes the currently selected formats from the registry and updates the presented list
*/
deleteFormats() {
this.bitstreamFormatService.clearBitStreamFormatRequests().subscribe();
this.bitstreamFormatService.getSelectedBitstreamFormats().pipe(take(1)).subscribe(
(formats) => {
const tasks$ = [];
for (const format of formats) {
if (hasValue(format.id)) {
tasks$.push(this.bitstreamFormatService.delete(format.id).pipe(map((response: RemoteData<NoContent>) => response.hasSucceeded)));
}
}
zip(...tasks$).subscribe((results: boolean[]) => {
const successResponses = results.filter((result: boolean) => result);
const failedResponses = results.filter((result: boolean) => !result);
if (successResponses.length > 0) {
this.showNotification(true, successResponses.length);
}
if (failedResponses.length > 0) {
this.showNotification(false, failedResponses.length);
}

this.deselectAll();

this.paginationService.resetPage(this.pageConfig.id);
});
this.bitstreamFormatService.clearBitStreamFormatRequests();
this.bitstreamFormatService.getSelectedBitstreamFormats().pipe(
take(1),
// emit all formats in the array one at a time
mergeMap((formats: BitstreamFormat[]) => formats),
// delete each format
mergeMap((format: BitstreamFormat) => this.bitstreamFormatService.delete(format.id).pipe(
// wait for each response to come back
getFirstCompletedRemoteData(),
// return a boolean to indicate whether a response succeeded
map((response: RemoteData<NoContent>) => response.hasSucceeded),
)),
// wait for all responses to come in and return them as a single array
toArray()
).subscribe((results: boolean[]) => {
// Count the number of succeeded and failed deletions
const successResponses = results.filter((result: boolean) => result);
const failedResponses = results.filter((result: boolean) => !result);

// Show a notification indicating the number of succeeded and failed deletions
if (successResponses.length > 0) {
this.showNotification(true, successResponses.length);
}
);
if (failedResponses.length > 0) {
this.showNotification(false, failedResponses.length);
}

// reset the selection
this.deselectAll();

// reload the page
this.paginationService.resetPage(this.pageConfig.id);
});
}

/**
Expand Down

0 comments on commit 1d4f2ed

Please sign in to comment.