From 908281b7ff6ad231b15ece95b361806faa45c67d Mon Sep 17 00:00:00 2001
From: Jeremie BRISON <ext.sopra.jbrison@grandlyon.com>
Date: Tue, 20 Oct 2020 09:28:42 +0200
Subject: [PATCH] fix(search) : fix multiple filter checkbox

---
 api/server.js                                 | 30 +++++++++++++++++++
 .../components/search/search.component.html   |  2 +-
 .../components/search/search.component.ts     | 15 +++++++---
 .../structure-list/models/category.model.ts   |  4 +++
 src/app/structure-list/models/module.model.ts |  1 +
 .../structure-list/services/search.service.ts | 20 +++++++++++--
 .../services/structure-list.service.ts        | 12 ++++++--
 7 files changed, 75 insertions(+), 9 deletions(-)
 create mode 100644 api/server.js

diff --git a/api/server.js b/api/server.js
new file mode 100644
index 000000000..0bcb25ead
--- /dev/null
+++ b/api/server.js
@@ -0,0 +1,30 @@
+const jsonServer = require('json-server');
+const server = jsonServer.create();
+const router = jsonServer.router('db.json');
+const middlewares = jsonServer.defaults();
+
+// Set default middlewares (logger, static, cors and no-cache)
+server.use(middlewares);
+
+// Add custom routes before JSON Server router
+server.get('/structures/count', (req, res) => {
+  let structureCountTab = [];
+  structureCountTab.push({ id: 260, count: 3 });
+  structureCountTab.push({ id: 260, count: 3 });
+  structureCountTab.push({ id: 259, count: 3 });
+  structureCountTab.push({ id: 261, count: 3 });
+  structureCountTab.push({ id: 249, count: 3 });
+  structureCountTab.push({ id: 222, count: 2 });
+  structureCountTab.push({ id: 212, count: 3 });
+  structureCountTab.push({ id: 186, count: 2 });
+  structureCountTab.push({ id: 183, count: 2 });
+  return res.status(200).jsonp({
+    structureCountTab,
+  });
+});
+
+// Use default router
+server.use(router);
+server.listen(3000, () => {
+  console.log('JSON Server is running');
+});
diff --git a/src/app/structure-list/components/search/search.component.html b/src/app/structure-list/components/search/search.component.html
index 8236e0fcd..df0c51915 100644
--- a/src/app/structure-list/components/search/search.component.html
+++ b/src/app/structure-list/components/search/search.component.html
@@ -65,7 +65,7 @@
                 <div class="label">{{ module.text }}</div>
               </div>
             </li>
-            <span class="nbResult">34</span>
+            <span class="nbResult">{{ module.count ? module.count : '0' }}</span>
           </div>
         </ul>
       </div>
diff --git a/src/app/structure-list/components/search/search.component.ts b/src/app/structure-list/components/search/search.component.ts
index 30c27b5f7..259d6e208 100644
--- a/src/app/structure-list/components/search/search.component.ts
+++ b/src/app/structure-list/components/search/search.component.ts
@@ -77,6 +77,8 @@ export class SearchComponent implements OnInit {
   // Management of the checkbox event (Check / Uncheck)
   public onCheckboxChange(event, categ: string): void {
     const checkValue: number = parseInt(event.target.value);
+    console.log(checkValue);
+
     if (event.target.checked) {
       this.checkedModules.push(new Module(checkValue, categ));
     } else {
@@ -131,10 +133,15 @@ export class SearchComponent implements OnInit {
     if (option === this.modalType[0]) {
       this.mockService(this.categories, 'Accompagnement des démarches', { name: 'CAF', id: '' }, 7);
     } else if (option === this.modalType[1]) {
-      this.searchService.getCategories().subscribe((d) => {
-        d.forEach((element) => {
-          this.categories.push(element);
-        });
+      this.searchService.getCategories().subscribe((categories: Category[]) => {
+        this.searchService
+          .getFakeCounterModule()
+          .subscribe((res: { structureCountTab: { id: number; count: number }[] }) => {
+            categories.forEach((category) => {
+              category = this.searchService.setCountModules(category, res.structureCountTab);
+              this.categories.push(category);
+            });
+          });
       });
     } else if (option === this.modalType[2]) {
       this.mockService(this.categories, 'Équipements', 'Accès à des revues ou livres infoirmatiques numériques', 8);
diff --git a/src/app/structure-list/models/category.model.ts b/src/app/structure-list/models/category.model.ts
index f6c60e54e..cb5a96eee 100644
--- a/src/app/structure-list/models/category.model.ts
+++ b/src/app/structure-list/models/category.model.ts
@@ -3,4 +3,8 @@ import { Module } from './module.model';
 export class Category {
   name: string;
   modules: Module[];
+
+  constructor(obj?: any) {
+    Object.assign(this, obj);
+  }
 }
diff --git a/src/app/structure-list/models/module.model.ts b/src/app/structure-list/models/module.model.ts
index 83d025356..340c04092 100644
--- a/src/app/structure-list/models/module.model.ts
+++ b/src/app/structure-list/models/module.model.ts
@@ -1,6 +1,7 @@
 export class Module {
   id: number;
   text: string;
+  count: number;
 
   constructor(id: number, text: string) {
     this.id = id;
diff --git a/src/app/structure-list/services/search.service.ts b/src/app/structure-list/services/search.service.ts
index d48c602e1..4ae64b197 100644
--- a/src/app/structure-list/services/search.service.ts
+++ b/src/app/structure-list/services/search.service.ts
@@ -1,6 +1,9 @@
 import { HttpClient } from '@angular/common/http';
 import { Injectable } from '@angular/core';
 import { Observable } from 'rxjs';
+import { map } from 'rxjs/operators';
+import { Category } from '../models/category.model';
+import { Module } from '../models/module.model';
 
 @Injectable({
   providedIn: 'root',
@@ -8,7 +11,20 @@ import { Observable } from 'rxjs';
 export class SearchService {
   constructor(private http: HttpClient) {}
 
-  public getCategories(): Observable<any> {
-    return this.http.get('/api/Categories');
+  public getCategories(): Observable<Category[]> {
+    return this.http.get('/api/Categories').pipe(map((data: any[]) => data.map((item) => new Category(item))));
+  }
+  public getFakeCounterModule(): Observable<any> {
+    return this.http.get('http://localhost:3000/structures/count');
+  }
+  public setCountModules(category: Category, structureCountTab: { id: number; count: number }[]): Category {
+    category.modules.forEach((m: Module) => {
+      for (let i = 0; i < structureCountTab.length; i++) {
+        if (structureCountTab[i].id === m.id) {
+          m.count = structureCountTab[i].count;
+        }
+      }
+    });
+    return category;
   }
 }
diff --git a/src/app/structure-list/services/structure-list.service.ts b/src/app/structure-list/services/structure-list.service.ts
index 0a1d1e34a..2b9f77512 100644
--- a/src/app/structure-list/services/structure-list.service.ts
+++ b/src/app/structure-list/services/structure-list.service.ts
@@ -32,9 +32,17 @@ export class StructureService {
           api = api + '&';
         }
         if (filter.isStrict) {
-          api = api + filter.name + '=' + filter.value;
+          if (api.includes(filter.name)) {
+            api = api + '=' + filter.value;
+          } else {
+            api = api + filter.name + '=' + filter.value;
+          }
         } else {
-          api = api + filter.name + '_like=' + filter.value;
+          if (api.includes(filter.name)) {
+            api = api + filter.value;
+          } else {
+            api = api + filter.name + '_like=' + filter.value;
+          }
         }
       });
     }
-- 
GitLab