fix an issue where deleting bitstreamformats would show both an error and a success notification

This commit is contained in:
Art Lowel
2022-10-06 10:29:20 +02:00
parent 8ffc32540a
commit f5deb88c5f
2 changed files with 38 additions and 35 deletions

View File

@@ -20,14 +20,12 @@ import { TestScheduler } from 'rxjs/testing';
import { import {
createNoContentRemoteDataObject$, createNoContentRemoteDataObject$,
createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject,
createSuccessfulRemoteDataObject$ createSuccessfulRemoteDataObject$,
createFailedRemoteDataObject$
} from '../../../shared/remote-data.utils'; } from '../../../shared/remote-data.utils';
import { createPaginatedList } from '../../../shared/testing/utils.test'; 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 { PaginationService } from '../../../core/pagination/pagination.service';
import { PaginationServiceStub } from '../../../shared/testing/pagination-service.stub'; import { PaginationServiceStub } from '../../../shared/testing/pagination-service.stub';
import { FindListOptions } from '../../../core/data/find-list-options.model';
describe('BitstreamFormatsComponent', () => { describe('BitstreamFormatsComponent', () => {
let comp: BitstreamFormatsComponent; let comp: BitstreamFormatsComponent;
@@ -85,10 +83,6 @@ describe('BitstreamFormatsComponent', () => {
]; ];
const mockFormatsRD = createSuccessfulRemoteDataObject(createPaginatedList(mockFormatsList)); 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 = () => { const initAsync = () => {
notificationsServiceStub = new NotificationsServiceStub(); notificationsServiceStub = new NotificationsServiceStub();
@@ -246,7 +240,7 @@ describe('BitstreamFormatsComponent', () => {
)); ));
beforeEach(initBeforeEach); beforeEach(initBeforeEach);
it('should clear bitstream formats ', () => { it('should clear bitstream formats and show a success notification', () => {
comp.deleteFormats(); comp.deleteFormats();
expect(bitstreamFormatService.clearBitStreamFormatRequests).toHaveBeenCalled(); expect(bitstreamFormatService.clearBitStreamFormatRequests).toHaveBeenCalled();
@@ -275,7 +269,7 @@ describe('BitstreamFormatsComponent', () => {
selectBitstreamFormat: {}, selectBitstreamFormat: {},
deselectBitstreamFormat: {}, deselectBitstreamFormat: {},
deselectAllBitstreamFormats: {}, deselectAllBitstreamFormats: {},
delete: observableOf(false), delete: createFailedRemoteDataObject$(),
clearBitStreamFormatRequests: observableOf('cleared') clearBitStreamFormatRequests: observableOf('cleared')
}); });
@@ -295,7 +289,7 @@ describe('BitstreamFormatsComponent', () => {
)); ));
beforeEach(initBeforeEach); beforeEach(initBeforeEach);
it('should clear bitstream formats ', () => { it('should clear bitstream formats and show an error notification', () => {
comp.deleteFormats(); comp.deleteFormats();
expect(bitstreamFormatService.clearBitStreamFormatRequests).toHaveBeenCalled(); expect(bitstreamFormatService.clearBitStreamFormatRequests).toHaveBeenCalled();

View File

@@ -5,7 +5,7 @@ import { PaginatedList } from '../../../core/data/paginated-list.model';
import { PaginationComponentOptions } from '../../../shared/pagination/pagination-component-options.model'; import { PaginationComponentOptions } from '../../../shared/pagination/pagination-component-options.model';
import { BitstreamFormat } from '../../../core/shared/bitstream-format.model'; import { BitstreamFormat } from '../../../core/shared/bitstream-format.model';
import { BitstreamFormatDataService } from '../../../core/data/bitstream-format-data.service'; 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 { hasValue } from '../../../shared/empty.util';
import { NotificationsService } from '../../../shared/notifications/notifications.service'; import { NotificationsService } from '../../../shared/notifications/notifications.service';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
@@ -13,6 +13,7 @@ import { TranslateService } from '@ngx-translate/core';
import { NoContent } from '../../../core/shared/NoContent.model'; import { NoContent } from '../../../core/shared/NoContent.model';
import { PaginationService } from '../../../core/pagination/pagination.service'; import { PaginationService } from '../../../core/pagination/pagination.service';
import { FindListOptions } from '../../../core/data/find-list-options.model'; import { FindListOptions } from '../../../core/data/find-list-options.model';
import { getFirstCompletedRemoteData } from '../../../core/shared/operators';
/** /**
* This component renders a list of bitstream formats * This component renders a list of bitstream formats
@@ -58,31 +59,39 @@ export class BitstreamFormatsComponent implements OnInit, OnDestroy {
* Deletes the currently selected formats from the registry and updates the presented list * Deletes the currently selected formats from the registry and updates the presented list
*/ */
deleteFormats() { deleteFormats() {
this.bitstreamFormatService.clearBitStreamFormatRequests().subscribe(); this.bitstreamFormatService.clearBitStreamFormatRequests();
this.bitstreamFormatService.getSelectedBitstreamFormats().pipe(take(1)).subscribe( this.bitstreamFormatService.getSelectedBitstreamFormats().pipe(
(formats) => { take(1),
const tasks$ = []; // emit all formats in the array one at a time
for (const format of formats) { mergeMap((formats: BitstreamFormat[]) => formats),
if (hasValue(format.id)) { // delete each format
tasks$.push(this.bitstreamFormatService.delete(format.id).pipe(map((response: RemoteData<NoContent>) => response.hasSucceeded))); mergeMap((format: BitstreamFormat) => this.bitstreamFormatService.delete(format.id).pipe(
} // wait for each response to come back
} getFirstCompletedRemoteData(),
zip(...tasks$).subscribe((results: boolean[]) => { // return a boolean to indicate whether a response succeeded
const successResponses = results.filter((result: boolean) => result); map((response: RemoteData<NoContent>) => response.hasSucceeded),
const failedResponses = results.filter((result: boolean) => !result); )),
if (successResponses.length > 0) { // wait for all responses to come in and return them as a single array
this.showNotification(true, successResponses.length); toArray()
} ).subscribe((results: boolean[]) => {
if (failedResponses.length > 0) { // Count the number of succeeded and failed deletions
this.showNotification(false, failedResponses.length); const successResponses = results.filter((result: boolean) => result);
} const failedResponses = results.filter((result: boolean) => !result);
this.deselectAll(); // Show a notification indicating the number of succeeded and failed deletions
if (successResponses.length > 0) {
this.paginationService.resetPage(this.pageConfig.id); 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);
});
} }
/** /**