From f5deb88c5fe32bd59dbfb9696542f232ee7bb969 Mon Sep 17 00:00:00 2001 From: Art Lowel Date: Thu, 6 Oct 2022 10:29:20 +0200 Subject: [PATCH] fix an issue where deleting bitstreamformats would show both an error and a success notification --- .../bitstream-formats.component.spec.ts | 16 ++--- .../bitstream-formats.component.ts | 59 +++++++++++-------- 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/src/app/admin/admin-registries/bitstream-formats/bitstream-formats.component.spec.ts b/src/app/admin/admin-registries/bitstream-formats/bitstream-formats.component.spec.ts index 7d3a726eec7..222804ca28f 100644 --- a/src/app/admin/admin-registries/bitstream-formats/bitstream-formats.component.spec.ts +++ b/src/app/admin/admin-registries/bitstream-formats/bitstream-formats.component.spec.ts @@ -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(); diff --git a/src/app/admin/admin-registries/bitstream-formats/bitstream-formats.component.ts b/src/app/admin/admin-registries/bitstream-formats/bitstream-formats.component.ts index 89d8ac29f3d..3e2f3ce8fcd 100644 --- a/src/app/admin/admin-registries/bitstream-formats/bitstream-formats.component.ts +++ b/src/app/admin/admin-registries/bitstream-formats/bitstream-formats.component.ts @@ -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,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) => 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) => 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); + }); } /**