1- import { ChangeDetectorRef , Component , Input , OnInit } from '@angular/core' ;
1+ import { ChangeDetectorRef , Component , Input , OnDestroy , OnInit } from '@angular/core' ;
22import { ScriptDataService } from '../core/data/processes/script-data.service' ;
33import { UntypedFormControl , UntypedFormGroup } from '@angular/forms' ;
4- import { getFirstCompletedRemoteData } from '../core/shared/operators' ;
5- import { find , map } from 'rxjs/operators' ;
4+ import { getFirstCompletedRemoteData , getFirstSucceededRemoteDataPayload } from '../core/shared/operators' ;
5+ import { map } from 'rxjs/operators' ;
66import { NotificationsService } from '../shared/notifications/notifications.service' ;
77import { TranslateService } from '@ngx-translate/core' ;
88import { hasValue , isEmpty , isNotEmpty } from '../shared/empty.util' ;
99import { RemoteData } from '../core/data/remote-data' ;
1010import { Router } from '@angular/router' ;
11- import { ProcessDataService } from '../core/data/processes/process-data.service' ;
1211import { Process } from '../process-page/processes/process.model' ;
1312import { ConfigurationDataService } from '../core/data/configuration-data.service' ;
1413import { ConfigurationProperty } from '../core/shared/configuration-property.model' ;
15- import { Observable } from 'rxjs' ;
14+ import { Observable , Subscription } from 'rxjs' ;
1615import { getProcessDetailRoute } from '../process-page/process-page-routing.paths' ;
1716import { HandleService } from '../shared/handle.service' ;
1817
1918export const CURATION_CFG = 'plugin.named.org.dspace.curate.CurationTask' ;
19+
2020/**
2121 * Component responsible for rendering the Curation Task form
2222 */
2323@Component ( {
2424 selector : 'ds-curation-form' ,
2525 templateUrl : './curation-form.component.html'
2626} )
27- export class CurationFormComponent implements OnInit {
27+ export class CurationFormComponent implements OnDestroy , OnInit {
2828
2929 config : Observable < RemoteData < ConfigurationProperty > > ;
3030 tasks : string [ ] ;
@@ -33,10 +33,11 @@ export class CurationFormComponent implements OnInit {
3333 @Input ( )
3434 dsoHandle : string ;
3535
36+ subs : Subscription [ ] = [ ] ;
37+
3638 constructor (
3739 private scriptDataService : ScriptDataService ,
3840 private configurationDataService : ConfigurationDataService ,
39- private processDataService : ProcessDataService ,
4041 private notificationsService : NotificationsService ,
4142 private translateService : TranslateService ,
4243 private handleService : HandleService ,
@@ -45,23 +46,26 @@ export class CurationFormComponent implements OnInit {
4546 ) {
4647 }
4748
49+ ngOnDestroy ( ) : void {
50+ this . subs . forEach ( ( sub : Subscription ) => sub . unsubscribe ( ) ) ;
51+ }
52+
4853 ngOnInit ( ) : void {
4954 this . form = new UntypedFormGroup ( {
5055 task : new UntypedFormControl ( '' ) ,
5156 handle : new UntypedFormControl ( '' )
5257 } ) ;
5358
5459 this . config = this . configurationDataService . findByPropertyName ( CURATION_CFG ) ;
55- this . config . pipe (
56- find ( ( rd : RemoteData < ConfigurationProperty > ) => rd . hasSucceeded ) ,
57- map ( ( rd : RemoteData < ConfigurationProperty > ) => rd . payload )
58- ) . subscribe ( ( configProperties ) => {
60+ this . subs . push ( this . config . pipe (
61+ getFirstSucceededRemoteDataPayload ( ) ,
62+ ) . subscribe ( ( configProperties : ConfigurationProperty ) => {
5963 this . tasks = configProperties . values
6064 . filter ( ( value ) => isNotEmpty ( value ) && value . includes ( '=' ) )
6165 . map ( ( value ) => value . split ( '=' ) [ 1 ] . trim ( ) ) ;
6266 this . form . get ( 'task' ) . patchValue ( this . tasks [ 0 ] ) ;
6367 this . cdr . detectChanges ( ) ;
64- } ) ;
68+ } ) ) ;
6569 }
6670
6771 /**
@@ -77,33 +81,41 @@ export class CurationFormComponent implements OnInit {
7781 */
7882 submit ( ) {
7983 const taskName = this . form . get ( 'task' ) . value ;
80- let handle ;
84+ let handle$ : Observable < string | null > ;
8185 if ( this . hasHandleValue ( ) ) {
82- handle = this . handleService . normalizeHandle ( this . dsoHandle ) ;
83- if ( isEmpty ( handle ) ) {
84- this . notificationsService . error ( this . translateService . get ( 'curation.form.submit.error.head' ) ,
85- this . translateService . get ( 'curation.form.submit.error.invalid-handle' ) ) ;
86- return ;
87- }
86+ handle$ = this . handleService . normalizeHandle ( this . dsoHandle ) . pipe (
87+ map ( ( handle : string | null ) => {
88+ if ( isEmpty ( handle ) ) {
89+ this . notificationsService . error ( this . translateService . get ( 'curation.form.submit.error.head' ) ,
90+ this . translateService . get ( 'curation.form.submit.error.invalid-handle' ) ) ;
91+ }
92+ return handle ;
93+ } ) ,
94+ ) ;
8895 } else {
89- handle = this . handleService . normalizeHandle ( this . form . get ( 'handle' ) . value ) ;
90- if ( isEmpty ( handle ) ) {
91- handle = 'all' ;
92- }
96+ handle$ = this . handleService . normalizeHandle ( this . form . get ( 'handle' ) . value ) . pipe (
97+ map ( ( handle : string | null ) => isEmpty ( handle ) ? 'all' : handle ) ,
98+ ) ;
9399 }
94100
95- this . scriptDataService . invoke ( 'curate' , [
96- { name : '-t' , value : taskName } ,
97- { name : '-i' , value : handle } ,
98- ] , [ ] ) . pipe ( getFirstCompletedRemoteData ( ) ) . subscribe ( ( rd : RemoteData < Process > ) => {
99- if ( rd . hasSucceeded ) {
100- this . notificationsService . success ( this . translateService . get ( 'curation.form.submit.success.head' ) ,
101- this . translateService . get ( 'curation.form.submit.success.content' ) ) ;
102- this . router . navigateByUrl ( getProcessDetailRoute ( rd . payload . processId ) ) ;
103- } else {
104- this . notificationsService . error ( this . translateService . get ( 'curation.form.submit.error.head' ) ,
105- this . translateService . get ( 'curation.form.submit.error.content' ) ) ;
101+ this . subs . push ( handle$ . subscribe ( ( handle : string ) => {
102+ if ( hasValue ( handle ) ) {
103+ this . subs . push ( this . scriptDataService . invoke ( 'curate' , [
104+ { name : '-t' , value : taskName } ,
105+ { name : '-i' , value : handle } ,
106+ ] , [ ] ) . pipe (
107+ getFirstCompletedRemoteData ( ) ,
108+ ) . subscribe ( ( rd : RemoteData < Process > ) => {
109+ if ( rd . hasSucceeded ) {
110+ this . notificationsService . success ( this . translateService . get ( 'curation.form.submit.success.head' ) ,
111+ this . translateService . get ( 'curation.form.submit.success.content' ) ) ;
112+ void this . router . navigateByUrl ( getProcessDetailRoute ( rd . payload . processId ) ) ;
113+ } else {
114+ this . notificationsService . error ( this . translateService . get ( 'curation.form.submit.error.head' ) ,
115+ this . translateService . get ( 'curation.form.submit.error.content' ) ) ;
116+ }
117+ } ) ) ;
106118 }
107- } ) ;
119+ } ) ) ;
108120 }
109121}
0 commit comments