diff --git a/api/server.js b/api/server.js new file mode 100644 index 0000000000000000000000000000000000000000..0bcb25eade496308cc87591861976279b2528813 --- /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 8236e0fcd146738189beb4f1be4f9af9bd43d3d6..df0c51915943ddbf45d64ad991d6c66aa0d15e67 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 30c27b5f7ddf724456c9497cb838e85c77b9ac11..259d6e208dd05fa6bab94103dcb6446db975a0a5 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 f6c60e54e45427e03ce9c5c8fc9886baf823118d..cb5a96eeedce438d3d4e07f714ad42531d82908d 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 83d025356912464a4b01d71fa36ae68e6def6d96..340c04092a730edcaa4a4a1f38d6778ee94064c1 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 d48c602e1fa51501389f404a50e357d30c57b65b..4ae64b197ba7dd08bb267e97564a141a0045e742 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 0a1d1e34a698e616b8644890fa8cce6df5218db4..2b9f775127c3964edbbcb7940161ae9826b11bb3 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; + } } }); }