Skip to content

Commit

Permalink
Feat(areas) add param to remove geom from query
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Narcisi authored and jacquesfize committed Apr 3, 2024
1 parent 55be4d8 commit c266e84
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion backend/dependencies/RefGeo
Submodule RefGeo updated 1 files
+17 −7 src/ref_geo/routes.py
16 changes: 12 additions & 4 deletions frontend/src/app/GN2CommonModule/form/areas/areas.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export class AreasComponent extends GenericFormComponent implements OnInit {
* `type_code` indiqués dans cet attribut.
*/
@Input() typeCodes: Array<string> = []; // Areas type_code
/**
* Do not get the geom from the getArea call, for performances
*/
@Input() withoutGeom: Boolean = false;
/**
* Nom du champ à utiliser pour déterminer la valeur à utiliser pour le
* contenu du `FormControl`.
Expand Down Expand Up @@ -90,13 +94,16 @@ export class AreasComponent extends GenericFormComponent implements OnInit {
areas_input$ = new Subject<string>();
areas: Observable<any>;
loading = false;
params = {};

constructor(private dataService: DataFormService) {
super();
}

ngOnInit() {
super.ngOnInit();
this.params['type_code'] = this.typeCodes;
this.params['without_geom'] = this.withoutGeom;
this.valueFieldName = this.valueFieldName === undefined ? 'id_area' : this.valueFieldName;

this.getAreas();
Expand All @@ -105,9 +112,9 @@ export class AreasComponent extends GenericFormComponent implements OnInit {
/**
* Merge initial 100 areas + default values (for update)
*/
initalAreas(): Observable<any> {
initialAreas(): Observable<any> {
return zip(
this.dataService.getAreas(this.typeCodes).pipe(map((data) => this.formatAreas(data))), // Default items
this.dataService.getAreas(this.params).pipe(map((data) => this.formatAreas(data))), // Default items
of(this.defaultItems) // Default items in update mode
).pipe(
map((areasArrays) => {
Expand All @@ -130,14 +137,15 @@ export class AreasComponent extends GenericFormComponent implements OnInit {

getAreas() {
this.areas = concat(
this.initalAreas(),
this.initialAreas(),
this.areas_input$.pipe(
debounceTime(200),
distinctUntilChanged(),
tap(() => (this.loading = true)),
switchMap((term) => {
this.params['area_name'] = term;
return term && term.length >= 2
? this.dataService.getAreas(this.typeCodes, term).pipe(
? this.dataService.getAreas(this.params).pipe(
map((data) => this.formatAreas(data)),
catchError(() => of([])), // Empty list on error
tap(() => (this.loading = false))
Expand Down
18 changes: 9 additions & 9 deletions frontend/src/app/GN2CommonModule/form/data-form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,18 +316,18 @@ export class DataFormService {
});
}

getAreas(area_type_list: Array<string>, area_name?) {
let params: HttpParams = new HttpParams();

area_type_list.forEach((id_type) => {
params = params.append('type_code', id_type.toString());
});
getAreas(params: {}) {
let queryString: HttpParams = new HttpParams();

if (area_name) {
params = params.set('area_name', area_name);
for (let key in params) {
let param = params[key];
if (Array.isArray(param)) {
param = param.join(',');
}
queryString = queryString.set(key, param);
}

return this._http.get<any>(`${this.config.API_ENDPOINT}/geo/areas`, { params: params });
return this._http.get<any>(`${this.config.API_ENDPOINT}/geo/areas`, { params: queryString });
}

getAreasTypes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ <h2 class="h6 mb-1"><small>Champs avancé(s) sélectionné(s) :</small></h2>
[parentFormControl]="area.control"
[label]="area.label"
[typeCodes]="area.type_code_array"
[withoutGeom]="true"
></pnx-areas>
</div>
</div>
Expand Down

0 comments on commit c266e84

Please sign in to comment.