Skip to content

Commit 1d4f2ed

Browse files
authored
Merge pull request #1886 from atmire/fix-1882
Fix dual notification when deleting bitstreamformats
2 parents 8ffc325 + f5deb88 commit 1d4f2ed

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

src/app/admin/admin-registries/bitstream-formats/bitstream-formats.component.spec.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@ import { TestScheduler } from 'rxjs/testing';
2020
import {
2121
createNoContentRemoteDataObject$,
2222
createSuccessfulRemoteDataObject,
23-
createSuccessfulRemoteDataObject$
23+
createSuccessfulRemoteDataObject$,
24+
createFailedRemoteDataObject$
2425
} from '../../../shared/remote-data.utils';
2526
import { createPaginatedList } from '../../../shared/testing/utils.test';
26-
import { PaginationComponentOptions } from '../../../shared/pagination/pagination-component-options.model';
27-
import { SortDirection, SortOptions } from '../../../core/cache/models/sort-options.model';
2827
import { PaginationService } from '../../../core/pagination/pagination.service';
2928
import { PaginationServiceStub } from '../../../shared/testing/pagination-service.stub';
30-
import { FindListOptions } from '../../../core/data/find-list-options.model';
3129

3230
describe('BitstreamFormatsComponent', () => {
3331
let comp: BitstreamFormatsComponent;
@@ -85,10 +83,6 @@ describe('BitstreamFormatsComponent', () => {
8583
];
8684
const mockFormatsRD = createSuccessfulRemoteDataObject(createPaginatedList(mockFormatsList));
8785

88-
const pagination = Object.assign(new PaginationComponentOptions(), { currentPage: 1, pageSize: 20 });
89-
const sort = new SortOptions('score', SortDirection.DESC);
90-
const findlistOptions = Object.assign(new FindListOptions(), { currentPage: 1, elementsPerPage: 20 });
91-
9286
const initAsync = () => {
9387
notificationsServiceStub = new NotificationsServiceStub();
9488

@@ -246,7 +240,7 @@ describe('BitstreamFormatsComponent', () => {
246240
));
247241

248242
beforeEach(initBeforeEach);
249-
it('should clear bitstream formats ', () => {
243+
it('should clear bitstream formats and show a success notification', () => {
250244
comp.deleteFormats();
251245

252246
expect(bitstreamFormatService.clearBitStreamFormatRequests).toHaveBeenCalled();
@@ -275,7 +269,7 @@ describe('BitstreamFormatsComponent', () => {
275269
selectBitstreamFormat: {},
276270
deselectBitstreamFormat: {},
277271
deselectAllBitstreamFormats: {},
278-
delete: observableOf(false),
272+
delete: createFailedRemoteDataObject$(),
279273
clearBitStreamFormatRequests: observableOf('cleared')
280274
});
281275

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

297291
beforeEach(initBeforeEach);
298-
it('should clear bitstream formats ', () => {
292+
it('should clear bitstream formats and show an error notification', () => {
299293
comp.deleteFormats();
300294

301295
expect(bitstreamFormatService.clearBitStreamFormatRequests).toHaveBeenCalled();

src/app/admin/admin-registries/bitstream-formats/bitstream-formats.component.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import { PaginatedList } from '../../../core/data/paginated-list.model';
55
import { PaginationComponentOptions } from '../../../shared/pagination/pagination-component-options.model';
66
import { BitstreamFormat } from '../../../core/shared/bitstream-format.model';
77
import { BitstreamFormatDataService } from '../../../core/data/bitstream-format-data.service';
8-
import { map, switchMap, take } from 'rxjs/operators';
8+
import { map, mergeMap, switchMap, take, toArray } from 'rxjs/operators';
99
import { hasValue } from '../../../shared/empty.util';
1010
import { NotificationsService } from '../../../shared/notifications/notifications.service';
1111
import { Router } from '@angular/router';
1212
import { TranslateService } from '@ngx-translate/core';
1313
import { NoContent } from '../../../core/shared/NoContent.model';
1414
import { PaginationService } from '../../../core/pagination/pagination.service';
1515
import { FindListOptions } from '../../../core/data/find-list-options.model';
16+
import { getFirstCompletedRemoteData } from '../../../core/shared/operators';
1617

1718
/**
1819
* This component renders a list of bitstream formats
@@ -58,31 +59,39 @@ export class BitstreamFormatsComponent implements OnInit, OnDestroy {
5859
* Deletes the currently selected formats from the registry and updates the presented list
5960
*/
6061
deleteFormats() {
61-
this.bitstreamFormatService.clearBitStreamFormatRequests().subscribe();
62-
this.bitstreamFormatService.getSelectedBitstreamFormats().pipe(take(1)).subscribe(
63-
(formats) => {
64-
const tasks$ = [];
65-
for (const format of formats) {
66-
if (hasValue(format.id)) {
67-
tasks$.push(this.bitstreamFormatService.delete(format.id).pipe(map((response: RemoteData<NoContent>) => response.hasSucceeded)));
68-
}
69-
}
70-
zip(...tasks$).subscribe((results: boolean[]) => {
71-
const successResponses = results.filter((result: boolean) => result);
72-
const failedResponses = results.filter((result: boolean) => !result);
73-
if (successResponses.length > 0) {
74-
this.showNotification(true, successResponses.length);
75-
}
76-
if (failedResponses.length > 0) {
77-
this.showNotification(false, failedResponses.length);
78-
}
79-
80-
this.deselectAll();
81-
82-
this.paginationService.resetPage(this.pageConfig.id);
83-
});
62+
this.bitstreamFormatService.clearBitStreamFormatRequests();
63+
this.bitstreamFormatService.getSelectedBitstreamFormats().pipe(
64+
take(1),
65+
// emit all formats in the array one at a time
66+
mergeMap((formats: BitstreamFormat[]) => formats),
67+
// delete each format
68+
mergeMap((format: BitstreamFormat) => this.bitstreamFormatService.delete(format.id).pipe(
69+
// wait for each response to come back
70+
getFirstCompletedRemoteData(),
71+
// return a boolean to indicate whether a response succeeded
72+
map((response: RemoteData<NoContent>) => response.hasSucceeded),
73+
)),
74+
// wait for all responses to come in and return them as a single array
75+
toArray()
76+
).subscribe((results: boolean[]) => {
77+
// Count the number of succeeded and failed deletions
78+
const successResponses = results.filter((result: boolean) => result);
79+
const failedResponses = results.filter((result: boolean) => !result);
80+
81+
// Show a notification indicating the number of succeeded and failed deletions
82+
if (successResponses.length > 0) {
83+
this.showNotification(true, successResponses.length);
8484
}
85-
);
85+
if (failedResponses.length > 0) {
86+
this.showNotification(false, failedResponses.length);
87+
}
88+
89+
// reset the selection
90+
this.deselectAll();
91+
92+
// reload the page
93+
this.paginationService.resetPage(this.pageConfig.id);
94+
});
8695
}
8796

8897
/**

0 commit comments

Comments
 (0)