3434 * The fact that you are presently reading this means that you have had
3535 * knowledge of the CeCILL-C license and that you accept its terms.
3636 */
37- import { Component , EventEmitter , Input , OnChanges , Output , SimpleChanges } from '@angular/core' ;
37+ import { Component , EventEmitter , Input , OnChanges , OnDestroy , Output , SimpleChanges } from '@angular/core' ;
3838import { FormArray , FormBuilder , FormGroup , Validators } from '@angular/forms' ;
3939import { Observable , of , Subscription } from 'rxjs' ;
4040import { mergeMap , tap } from 'rxjs/operators' ;
@@ -51,7 +51,7 @@ import { TranslateService } from '@ngx-translate/core';
5151 providers : [ ManagementContractToFormGroupConverterService ] ,
5252 standalone : false ,
5353} )
54- export class ManagementContractIdentificationTabComponent implements OnChanges {
54+ export class ManagementContractIdentificationTabComponent implements OnChanges , OnDestroy {
5555 @Input ( ) managementContract : ManagementContract ;
5656 @Output ( ) updated : EventEmitter < boolean > = new EventEmitter < boolean > ( ) ;
5757
@@ -107,6 +107,10 @@ export class ManagementContractIdentificationTabComponent implements OnChanges {
107107 }
108108
109109 resetForm ( managementContract : ManagementContract ) : void {
110+ // Nettoyer les souscriptions existantes pour éviter les faux positifs
111+ this . subscriptions . unsubscribe ( ) ;
112+ this . subscriptions = new Subscription ( ) ;
113+
110114 this . contractForm = this . managementContractToFormGroupConverterService . convert ( managementContract ) ;
111115
112116 const persistentIdentifierPolicies = managementContract . persistentIdentifierPolicyList || [ ] ;
@@ -116,23 +120,25 @@ export class ManagementContractIdentificationTabComponent implements OnChanges {
116120
117121 this . updated . emit ( false ) ;
118122
119- this . contractForm . get ( 'policyTypeOption' ) . valueChanges . subscribe ( ( value ) => {
120- let persistentIdentifierPolicyFormArray : FormArray = this . formBuilder . array ( [ ] ) ;
123+ this . subscriptions . add (
124+ this . contractForm . get ( 'policyTypeOption' ) . valueChanges . subscribe ( ( value ) => {
125+ let persistentIdentifierPolicyFormArray : FormArray = this . formBuilder . array ( [ ] ) ;
121126
122- if ( value === PersistentIdentifierPolicyTypeEnum . ARK ) {
123- persistentIdentifierPolicyFormArray = this . managementContractToFormGroupConverterService
124- . convert ( managementContract )
125- . get ( 'persistentIdentifierPolicies' ) as FormArray ;
126- if ( persistentIdentifierPolicyFormArray . controls . length === 0 ) {
127+ if ( value === PersistentIdentifierPolicyTypeEnum . ARK ) {
127128 persistentIdentifierPolicyFormArray = this . managementContractToFormGroupConverterService
128- . getDefaultManagementContractForm ( )
129+ . convert ( managementContract )
129130 . get ( 'persistentIdentifierPolicies' ) as FormArray ;
131+ if ( persistentIdentifierPolicyFormArray . controls . length === 0 ) {
132+ persistentIdentifierPolicyFormArray = this . managementContractToFormGroupConverterService
133+ . getDefaultManagementContractForm ( )
134+ . get ( 'persistentIdentifierPolicies' ) as FormArray ;
135+ }
136+ persistentIdentifierPolicyFormArray . patchValue ( [ { policyTypeOption : this . policyTypeOptions [ 1 ] . key } ] ) ;
130137 }
131- persistentIdentifierPolicyFormArray . patchValue ( [ { policyTypeOption : this . policyTypeOptions [ 1 ] . key } ] ) ;
132- }
133- this . contractForm . removeControl ( 'persistentIdentifierPolicies' ) ;
134- this . contractForm . setControl ( 'persistentIdentifierPolicies' , persistentIdentifierPolicyFormArray ) ;
135- } ) ;
138+ this . contractForm . removeControl ( 'persistentIdentifierPolicies' ) ;
139+ this . contractForm . setControl ( 'persistentIdentifierPolicies' , persistentIdentifierPolicyFormArray ) ;
140+ } ) ,
141+ ) ;
136142
137143 this . subscriptions . add (
138144 this . contractForm . valueChanges . subscribe ( ( ) => {
@@ -188,4 +194,8 @@ export class ManagementContractIdentificationTabComponent implements OnChanges {
188194
189195 return true ;
190196 }
197+
198+ ngOnDestroy ( ) : void {
199+ this . subscriptions . unsubscribe ( ) ;
200+ }
191201}
0 commit comments