@@ -5,14 +5,15 @@ import { PaginatedList } from '../../../core/data/paginated-list.model';
55import { PaginationComponentOptions } from '../../../shared/pagination/pagination-component-options.model' ;
66import { BitstreamFormat } from '../../../core/shared/bitstream-format.model' ;
77import { 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' ;
99import { hasValue } from '../../../shared/empty.util' ;
1010import { NotificationsService } from '../../../shared/notifications/notifications.service' ;
1111import { Router } from '@angular/router' ;
1212import { TranslateService } from '@ngx-translate/core' ;
1313import { NoContent } from '../../../core/shared/NoContent.model' ;
1414import { PaginationService } from '../../../core/pagination/pagination.service' ;
1515import { 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