11import { Injectable } from '@angular/core' ;
22import { UntypedFormGroup , UntypedFormArray , UntypedFormBuilder , Validators } from '@angular/forms' ;
3- import { BehaviorSubject , forkJoin , Observable , of } from 'rxjs' ;
3+ import { BehaviorSubject , combineLatest , forkJoin , Observable , of } from 'rxjs' ;
44import { tap , filter , switchMap , map } from 'rxjs/operators' ;
55
66import { ActorFormService } from './actor-form.service' ;
@@ -15,7 +15,6 @@ export class AcquisitionFrameworkFormService {
1515 // Custom additional fields
1616 public additionalFieldsForm : Array < any > = [ ] ;
1717 public queryParamsSource : BehaviorSubject < Object > = new BehaviorSubject ( { } ) ;
18- private queryParams$ : Observable < Object > = this . queryParamsSource . asObservable ( ) ;
1918
2019 constructor (
2120 private fb : UntypedFormBuilder ,
@@ -106,9 +105,8 @@ export class AcquisitionFrameworkFormService {
106105 **/
107106 private setObservables ( ) {
108107 //Observable de this.dataset pour adapter le formulaire selon la donnée
109- this . acquisition_framework
110- . asObservable ( )
111- . pipe (
108+ combineLatest ( [
109+ this . acquisition_framework . asObservable ( ) . pipe (
112110 tap ( ( ) => this . reset ( ) ) ,
113111 tap ( ( ) => {
114112 this . additionalFieldsForm = [ ] ;
@@ -156,8 +154,12 @@ export class AcquisitionFrameworkFormService {
156154 } ) ,
157155 // Map to return acquisition framework data only
158156 map ( ( [ acquisition_framework , additional_data ] ) => acquisition_framework )
159- )
160- . subscribe ( ( value : any ) => this . form . patchValue ( value ) ) ;
157+ ) ,
158+ this . queryParamsSource ,
159+ ] ) . subscribe ( ( [ value , params ] ) => {
160+ setTimeout ( ( ) => this . form . patchValue ( value ) , 0 ) ;
161+ this . setFromParams ( params ) ;
162+ } ) ;
161163
162164 //gère lactivation/désactivation de la zone de saisie du framework Parent
163165 this . form . get ( 'is_parent' ) . valueChanges . subscribe ( ( value : boolean ) => {
@@ -167,11 +169,6 @@ export class AcquisitionFrameworkFormService {
167169 this . form . get ( 'acquisition_framework_parent_id' ) . enable ( ) ;
168170 }
169171 } ) ;
170-
171- // Update form from query params on every update of the latter
172- this . queryParams$ . subscribe ( ( params ) => {
173- this . setFromParams ( params ) ;
174- } ) ;
175172 }
176173
177174 setFromParams ( params : any ) {
@@ -211,17 +208,19 @@ export class AcquisitionFrameworkFormService {
211208 ( field ) => field . paramName == key
212209 ) ;
213210 if ( keyAsAdditionalField ) {
214- this . form . get ( 'additional_data' ) . get ( keyAsAdditionalField . fieldName ) . setValue ( params [ key ] ) ;
211+ setTimeout ( ( ) => {
212+ this . form
213+ . get ( 'additional_data' )
214+ . patchValue ( { [ keyAsAdditionalField . fieldName ] : params [ key ] } ) ;
215+ } , 0 ) ;
215216 }
216217 if ( key == 'id_role' ) {
217- this . form . get ( 'cor_af_actor' ) . patchValue ( [ { id_role : params [ key ] } ] ) ;
218+ this . form . get ( 'cor_af_actor' ) . patchValue ( [ { id_role : + params [ key ] } ] ) ;
218219 }
219220 if ( key == 'id_organism' ) {
220- this . form . get ( 'cor_af_actor' ) . patchValue ( [ { id_organism : params [ key ] } ] ) ;
221+ this . form . get ( 'cor_af_actor' ) . patchValue ( [ { id_organism : + params [ key ] } ] ) ;
221222 }
222223 } ) ;
223-
224- console . log ( this . form . value ) ;
225224 }
226225
227226 get actors ( ) : UntypedFormArray {
0 commit comments