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' ;
77import { FormService } from '@geonature_common/form/form.service' ;
88import { DataFormService } from '@geonature_common/form/data-form.service' ;
99import { ModuleService } from '@geonature/services/module.service' ;
1010
11+ import { NgbDateParserFormatter } from '@ng-bootstrap/ng-bootstrap' ;
12+
1113@Injectable ( )
1214export class AcquisitionFrameworkFormService {
1315 public form : UntypedFormGroup ;
1416 public acquisition_framework : BehaviorSubject < any > = new BehaviorSubject ( null ) ;
1517 // Custom additional fields
1618 public additionalFieldsForm : Array < any > = [ ] ;
19+ public queryParamsSource : BehaviorSubject < Object > = new BehaviorSubject ( { } ) ;
1720
1821 constructor (
1922 private fb : UntypedFormBuilder ,
2023 private actorFormS : ActorFormService ,
2124 private formS : FormService ,
2225 private dataFormService : DataFormService ,
23- private _moduleService : ModuleService
26+ private _moduleService : ModuleService ,
27+ private dateParser : NgbDateParserFormatter
2428 ) {
2529 this . initForm ( ) ;
2630 this . setObservables ( ) ;
@@ -104,9 +108,8 @@ export class AcquisitionFrameworkFormService {
104108 **/
105109 private setObservables ( ) {
106110 //Observable de this.dataset pour adapter le formulaire selon la donnée
107- this . acquisition_framework
108- . asObservable ( )
109- . pipe (
111+ combineLatest ( [
112+ this . acquisition_framework . asObservable ( ) . pipe (
110113 tap ( ( ) => this . reset ( ) ) ,
111114 tap ( ( ) => {
112115 this . additionalFieldsForm = [ ] ;
@@ -154,8 +157,12 @@ export class AcquisitionFrameworkFormService {
154157 } ) ,
155158 // Map to return acquisition framework data only
156159 map ( ( [ acquisition_framework , additional_data ] ) => acquisition_framework )
157- )
158- . subscribe ( ( value : any ) => this . form . patchValue ( value ) ) ;
160+ ) ,
161+ this . queryParamsSource ,
162+ ] ) . subscribe ( ( [ value , params ] ) => {
163+ setTimeout ( ( ) => this . form . patchValue ( value ) , 0 ) ;
164+ this . setFromParams ( params ) ;
165+ } ) ;
159166
160167 //gère lactivation/désactivation de la zone de saisie du framework Parent
161168 this . form . get ( 'is_parent' ) . valueChanges . subscribe ( ( value : boolean ) => {
@@ -167,6 +174,40 @@ export class AcquisitionFrameworkFormService {
167174 } ) ;
168175 }
169176
177+ setFromParams ( params : any ) {
178+ // Supported query params
179+ const supportedQueryParams = {
180+ basicFields : [
181+ 'acquisition_framework_name' ,
182+ 'acquisition_framework_description' ,
183+ 'acquisition_framework_end_date' ,
184+ ] ,
185+ actorFields : [ 'id_role' , 'id_organism' ] ,
186+ } ;
187+
188+ Object . keys ( params ) . forEach ( ( key ) => {
189+ // Basic fields
190+ const keyAsBasicField = supportedQueryParams . basicFields . find ( ( field ) => field == key ) ;
191+ if ( keyAsBasicField ) {
192+ let value = params [ key ] ;
193+ if ( keyAsBasicField == 'acquisition_framework_end_date' ) {
194+ value = this . dateParser . parse ( value ) ;
195+ }
196+ this . form . get ( keyAsBasicField ) . setValue ( value ) ;
197+ }
198+ // Contact principal
199+ const keyAsActorField = supportedQueryParams . actorFields . find ( ( field ) => field == key ) ;
200+ if ( keyAsActorField ) {
201+ this . form . get ( 'cor_af_actor' ) . patchValue ( [ { [ keyAsActorField ] : + params [ key ] } ] ) ;
202+ }
203+ // Additional fields
204+ // /!\ Tested only for Number field type
205+ setTimeout ( ( ) => {
206+ this . form . get ( 'additional_data' ) . patchValue ( { [ key ] : params [ key ] } ) ;
207+ } , 0 ) ;
208+ } ) ;
209+ }
210+
170211 get actors ( ) : UntypedFormArray {
171212 return this . form . get ( 'cor_af_actor' ) as UntypedFormArray ;
172213 }
0 commit comments