diff --git a/src/app/pages/resto-page/resto-page.component.ts b/src/app/pages/resto-page/resto-page.component.ts index 4dfc1cd..26240b0 100644 --- a/src/app/pages/resto-page/resto-page.component.ts +++ b/src/app/pages/resto-page/resto-page.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit } from '@angular/core'; -import { forkJoin } from 'rxjs'; +import { forkJoin, of } from 'rxjs'; import { ApiBackService } from 'src/app/services/api-back.service'; import { TokenService } from 'src/app/services/token.service'; import { Preference } from '../models/preference'; @@ -12,33 +12,35 @@ import { Restaurant } from '../models/restaurant'; }) export class RestoPageComponent implements OnInit { - public listRestaurants : Restaurant[]; - public listPref : any; - public restaurantPref : any; - public beforeRoute ?: string ; - private userId : number | null; + public listRestaurants: Restaurant[]; + public listPref: any; + public restaurantPref: any; + public beforeRoute?: string; + private userId: number | null; - constructor(private apiBackService : ApiBackService, - private tokenService : TokenService) { + constructor(private apiBackService: ApiBackService, + private tokenService: TokenService) { this.listRestaurants = []; this.restaurantPref = []; this.beforeRoute = this.apiBackService.routeParam; - this.userId=0; + this.userId = 0; } ngOnInit(): void { this.restauLiked(); + this.listRestaurants = []; + this.userId = this.tokenService.getCurrentUserId(); - + } // onEventLike(liked : any, idRestau : number | undefined){ // //this.apiBackService.restoLiked$.next(true); // console.log(this.userId); // console.log(idRestau); - + // if(liked){ // this.apiBackService.addPreference(this.userId,idRestau).subscribe(); // }else{ @@ -46,46 +48,47 @@ export class RestoPageComponent implements OnInit { // } // } - saveRestauList(event : any){ + saveRestauList(event: any) { console.log(event); this.listRestaurants = event; } - - restauLiked(){ + + restauLiked() { forkJoin({ restaurants: this.apiBackService.getRestaurants(), user: this.apiBackService.getPersonneById(this.tokenService.getCurrentUserId()), - restauByCat : this.apiBackService.restoByCat - }).subscribe(({restaurants, user, restauByCat}) => { + restauByCat: this.apiBackService.restoByCat + }).subscribe(({ restaurants, user, restauByCat }) => { - if(this.beforeRoute === "filtres"){ + if (this.beforeRoute === "filtres") { this.listRestaurants = this.apiBackService.restoFilter; - }else if(this.beforeRoute === "categories" ){ - this.listRestaurants = restauByCat - }else{ - this.listRestaurants = restaurants; - } - - this.listPref = user.preference; - - for (let i = 0; i < this.listPref.length; i++) { - this.restaurantPref.push(this.listPref[i]['preferencePK']['restau']); - } - - const listRestauIdLike = this.restaurantPref.map((x: any) => x.id); - - this.listRestaurants.forEach((restau: any) => { - - - if(listRestauIdLike.includes(restau.id)) - //console.log(restau); - restau.restauLike = true; - }); - + } else if (this.beforeRoute === "categories" && restauByCat.length > 0) { + this.listRestaurants = restauByCat + this.apiBackService.restoByCat = of([]); + } else { + this.listRestaurants = restaurants; } + + this.listPref = user.preference; + + for (let i = 0; i < this.listPref.length; i++) { + this.restaurantPref.push(this.listPref[i]['preferencePK']['restau']); + } + + const listRestauIdLike = this.restaurantPref.map((x: any) => x.id); + + this.listRestaurants.forEach((restau: any) => { + + + if (listRestauIdLike.includes(restau.id)) + //console.log(restau); + restau.restauLike = true; + }); + + } );