Skip to content

Commit ed4f489

Browse files
feat(metadata): add possibility to fill af form from query params
1 parent a04326e commit ed4f489

File tree

3 files changed

+79
-11
lines changed

3 files changed

+79
-11
lines changed

frontend/src/app/metadataModule/actors/actors.component.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
22
import { UntypedFormArray, UntypedFormGroup, Validators } from '@angular/forms';
3-
import { BehaviorSubject } from 'rxjs';
3+
import { BehaviorSubject, combineLatest, Subject } from 'rxjs';
4+
import { startWith } from 'rxjs/operators';
45
import { MatDialog } from '@angular/material/dialog';
56

67
import { ConfirmationDialog } from '@geonature_common/others/modal-confirmation/confirmation.dialog';
@@ -102,6 +103,14 @@ export class ActorComponent implements OnInit {
102103
}
103104

104105
this.setToggleButtonValue();
106+
107+
combineLatest([
108+
this.actorForm.get('id_organism').valueChanges.pipe(startWith(null)),
109+
this.actorForm.get('id_role').valueChanges.pipe(startWith(null)),
110+
]).subscribe((_) => {
111+
const btn = this.computeButtonValue();
112+
this._toggleButtonValue.next(btn);
113+
});
105114
}
106115

107116
toggleActorOrganismChoiceChange(event) {
@@ -132,14 +141,20 @@ export class ActorComponent implements OnInit {
132141
}
133142
}
134143

135-
setToggleButtonValue() {
144+
computeButtonValue() {
136145
var btn =
137146
this.actorForm.get('id_organism').value && this.actorForm.get('id_role').value
138147
? 'all'
139148
: this.actorForm.get('id_role').value
140149
? 'person'
141150
: 'organism';
142151

152+
return btn;
153+
}
154+
155+
setToggleButtonValue() {
156+
var btn = this.computeButtonValue();
157+
143158
this.toggleActorOrganismChoiceChange({ value: btn });
144159
}
145160

frontend/src/app/metadataModule/af/af-form.component.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { AfterViewInit, Component, OnInit } from '@angular/core';
22
import { UntypedFormArray, UntypedFormGroup, Validators } from '@angular/forms';
33
import { Router, ActivatedRoute } from '@angular/router';
44
import { NgbDateParserFormatter } from '@ng-bootstrap/ng-bootstrap';
@@ -20,7 +20,7 @@ import { TranslateService } from '@librairies/@ngx-translate/core';
2020
styleUrls: ['../form.component.scss'],
2121
providers: [AcquisitionFrameworkFormService],
2222
})
23-
export class AfFormComponent implements OnInit {
23+
export class AfFormComponent implements OnInit, AfterViewInit {
2424
public form: UntypedFormGroup;
2525
//observable pour la liste déroulantes HTML des AF parents
2626
public acquisitionFrameworkParents: Observable<any>;
@@ -63,6 +63,18 @@ export class AfFormComponent implements OnInit {
6363
this.entityLabel = this.translation_service.instant('AcquisitionFramework');
6464
}
6565

66+
ngAfterViewInit() {
67+
this.setFromQueryParams();
68+
}
69+
70+
setFromQueryParams() {
71+
this._route.queryParams.subscribe((params) => {
72+
this.afFormS.queryParamsSource.next(params);
73+
});
74+
75+
// TODO?: Clean route from unsupported query params ?
76+
}
77+
6678
handleDates(af, isParseElseFormat = false) {
6779
const handlingFunction = isParseElseFormat ? this.dateParser.parse : this.dateParser.format;
6880

frontend/src/app/metadataModule/services/af-form.service.ts

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
import { Injectable } from '@angular/core';
22
import { UntypedFormGroup, UntypedFormArray, UntypedFormBuilder, Validators } from '@angular/forms';
3-
import { BehaviorSubject, forkJoin, Observable, of } from 'rxjs';
3+
import { BehaviorSubject, combineLatest, forkJoin, Observable, of } from 'rxjs';
44
import { tap, filter, switchMap, map } from 'rxjs/operators';
55

66
import { ActorFormService } from './actor-form.service';
77
import { FormService } from '@geonature_common/form/form.service';
88
import { DataFormService } from '@geonature_common/form/data-form.service';
99
import { ModuleService } from '@geonature/services/module.service';
1010

11+
import { NgbDateParserFormatter } from '@ng-bootstrap/ng-bootstrap';
12+
1113
@Injectable()
1214
export 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

Comments
 (0)