From ff69b22a1525fe53e3af04ff404a08582d058532 Mon Sep 17 00:00:00 2001
From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com>
Date: Wed, 28 Apr 2021 15:48:54 +0200
Subject: [PATCH] feat(search): add protection on distance filter for structure
 in case of search query. Increase search result display

---
 src/app/carto/carto.component.ts | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/app/carto/carto.component.ts b/src/app/carto/carto.component.ts
index 30f8c25b4..52857d854 100644
--- a/src/app/carto/carto.component.ts
+++ b/src/app/carto/carto.component.ts
@@ -63,7 +63,7 @@ export class CartoComponent implements OnInit {
       } else {
         this.structureService.getStructures(filters).subscribe((structures) => {
           if (structures) {
-            this.updateStructuresdistance(structures, this.userLongitude, this.userLatitude);
+            this.updateStructuresdistance(structures, this.userLongitude, this.userLatitude, false);
           } else {
             this.structures = null;
           }
@@ -87,7 +87,19 @@ export class CartoComponent implements OnInit {
     });
   }
 
-  private updateStructuresdistance(structures: Structure[], lon: number, lat: number): void {
+  /**
+   * Update structure distance according to user actual position.
+   * @param structures structures data to update
+   * @param lon user longitude
+   * @param lat user latitde
+   * @param sortByDistance if set to `true`, structures data is sort by distance. Default value is `true`
+   */
+  private updateStructuresdistance(
+    structures: Structure[],
+    lon: number,
+    lat: number,
+    sortByDistance: boolean = true
+  ): void {
     Promise.all(
       structures.map((structure) => {
         if (this.geolocation) {
@@ -96,7 +108,9 @@ export class CartoComponent implements OnInit {
         return this.structureService.updateOpeningStructure(structure);
       })
     ).then((structureList) => {
-      structureList = _.sortBy(structureList, ['distance']);
+      if (sortByDistance) {
+        structureList = _.sortBy(structureList, ['distance']);
+      }
       this.structures = structureList;
     });
   }
-- 
GitLab