bug restauCateg = ok

This commit is contained in:
Thomas Cardon 2022-03-09 14:52:09 +01:00
parent 8b70164a9f
commit f7d6b0f691

View file

@ -1,5 +1,5 @@
import { Component, OnInit } from '@angular/core'; 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 { ApiBackService } from 'src/app/services/api-back.service';
import { TokenService } from 'src/app/services/token.service'; import { TokenService } from 'src/app/services/token.service';
import { Preference } from '../models/preference'; import { Preference } from '../models/preference';
@ -12,33 +12,35 @@ import { Restaurant } from '../models/restaurant';
}) })
export class RestoPageComponent implements OnInit { export class RestoPageComponent implements OnInit {
public listRestaurants : Restaurant[]; public listRestaurants: Restaurant[];
public listPref : any; public listPref: any;
public restaurantPref : any; public restaurantPref: any;
public beforeRoute ?: string ; public beforeRoute?: string;
private userId : number | null; private userId: number | null;
constructor(private apiBackService : ApiBackService, constructor(private apiBackService: ApiBackService,
private tokenService : TokenService) { private tokenService: TokenService) {
this.listRestaurants = []; this.listRestaurants = [];
this.restaurantPref = []; this.restaurantPref = [];
this.beforeRoute = this.apiBackService.routeParam; this.beforeRoute = this.apiBackService.routeParam;
this.userId=0; this.userId = 0;
} }
ngOnInit(): void { ngOnInit(): void {
this.restauLiked(); this.restauLiked();
this.listRestaurants = [];
this.userId = this.tokenService.getCurrentUserId(); this.userId = this.tokenService.getCurrentUserId();
} }
// onEventLike(liked : any, idRestau : number | undefined){ // onEventLike(liked : any, idRestau : number | undefined){
// //this.apiBackService.restoLiked$.next(true); // //this.apiBackService.restoLiked$.next(true);
// console.log(this.userId); // console.log(this.userId);
// console.log(idRestau); // console.log(idRestau);
// if(liked){ // if(liked){
// this.apiBackService.addPreference(this.userId,idRestau).subscribe(); // this.apiBackService.addPreference(this.userId,idRestau).subscribe();
// }else{ // }else{
@ -46,46 +48,47 @@ export class RestoPageComponent implements OnInit {
// } // }
// } // }
saveRestauList(event : any){ saveRestauList(event: any) {
console.log(event); console.log(event);
this.listRestaurants = event; this.listRestaurants = event;
} }
restauLiked(){ restauLiked() {
forkJoin({ forkJoin({
restaurants: this.apiBackService.getRestaurants(), restaurants: this.apiBackService.getRestaurants(),
user: this.apiBackService.getPersonneById(this.tokenService.getCurrentUserId()), user: this.apiBackService.getPersonneById(this.tokenService.getCurrentUserId()),
restauByCat : this.apiBackService.restoByCat restauByCat: this.apiBackService.restoByCat
}).subscribe(({restaurants, user, restauByCat}) => { }).subscribe(({ restaurants, user, restauByCat }) => {
if(this.beforeRoute === "filtres"){ if (this.beforeRoute === "filtres") {
this.listRestaurants = this.apiBackService.restoFilter; this.listRestaurants = this.apiBackService.restoFilter;
}else if(this.beforeRoute === "categories" ){ } else if (this.beforeRoute === "categories" && restauByCat.length > 0) {
this.listRestaurants = restauByCat this.listRestaurants = restauByCat
}else{ this.apiBackService.restoByCat = of([]);
this.listRestaurants = restaurants; } 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;
});
} }
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;
});
}
); );