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 {
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;
@@ -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();
@@ -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();
@@ -275,7 +269,7 @@ describe('BitstreamFormatsComponent', () => {
selectBitstreamFormat: {},
deselectBitstreamFormat: {},
deselectAllBitstreamFormats: {},
delete: observableOf(false),
delete: createFailedRemoteDataObject$(),
clearBitStreamFormatRequests: observableOf('cleared')
});
@@ -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();

View File

@@ -5,7 +5,7 @@ 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';
@@ -13,6 +13,7 @@ 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
@@ -58,18 +59,26 @@ 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[]) => {
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);
}
@@ -77,13 +86,13 @@ export class BitstreamFormatsComponent implements OnInit, OnDestroy {
this.showNotification(false, failedResponses.length);
}
// reset the selection
this.deselectAll();
// reload the page
this.paginationService.resetPage(this.pageConfig.id);
});
}
);
}
/**
* Deselects all selecetd bitstream formats