diff --git a/dev/user-frontend-ionic/projects/restaurants/src/lib/restaurants.page.html b/dev/user-frontend-ionic/projects/restaurants/src/lib/restaurants.page.html index 0e989572..618a4022 100644 --- a/dev/user-frontend-ionic/projects/restaurants/src/lib/restaurants.page.html +++ b/dev/user-frontend-ionic/projects/restaurants/src/lib/restaurants.page.html @@ -50,8 +50,8 @@
- + diff --git a/dev/user-frontend-ionic/projects/restaurants/src/lib/restaurants.page.ts b/dev/user-frontend-ionic/projects/restaurants/src/lib/restaurants.page.ts index eeb7915b..16ebd354 100644 --- a/dev/user-frontend-ionic/projects/restaurants/src/lib/restaurants.page.ts +++ b/dev/user-frontend-ionic/projects/restaurants/src/lib/restaurants.page.ts @@ -44,7 +44,7 @@ import { NetworkService } from '@multi/shared'; import { getDistance } from 'geolib'; import { combineLatest, from, Observable, of } from 'rxjs'; import { catchError, map, take, tap } from 'rxjs/operators'; -import { favoriteRestaurantId$, RestaurantOpening, restaurants$, setFavoriteRestaurant } from './restaurants.repository'; +import { favoritesRestaurantsIds$, RestaurantOpening, restaurants$, setFavoriteRestaurant, unsetFavoriteRestaurant } from './restaurants.repository'; import { RestaurantsService } from './restaurants.service'; export interface PositionDto { @@ -96,8 +96,8 @@ export class RestaurantsPage implements OnInit { setFavoriteRestaurant(restaurantId); } - unsetFavoriteRestaurant() { - setFavoriteRestaurant(null); + unsetFavoriteRestaurant(restaurantId: number) { + unsetFavoriteRestaurant(restaurantId); } navigateToRestaurantMenus(restaurantId: number) { @@ -131,9 +131,9 @@ export class RestaurantsPage implements OnInit { this.restaurants$ = combineLatest([ restaurants$, this.getMyCurrentPosition(), - favoriteRestaurantId$ + favoritesRestaurantsIds$ ]).pipe( - map(([restaurants, myPosition, favoriteRestaurantId]) => { + map(([restaurants, myPosition, favoritesRestaurantsIds]) => { const restaurantsSortedByDistance = restaurants // Restaurant from store to RestaurantDto to display .map(restaurant => { @@ -149,7 +149,7 @@ export class RestaurantsPage implements OnInit { shortDesc: restaurant.shortDesc, opening: restaurant.opening, distance, - favorite: restaurant.id === favoriteRestaurantId + favorite: favoritesRestaurantsIds.includes(restaurant.id) }; }) // sort by distance diff --git a/dev/user-frontend-ionic/projects/restaurants/src/lib/restaurants.repository.ts b/dev/user-frontend-ionic/projects/restaurants/src/lib/restaurants.repository.ts index e45f29a6..b6268503 100644 --- a/dev/user-frontend-ionic/projects/restaurants/src/lib/restaurants.repository.ts +++ b/dev/user-frontend-ionic/projects/restaurants/src/lib/restaurants.repository.ts @@ -43,7 +43,7 @@ import { persistState } from '@ngneat/elf-persist-state'; import { localForageStore } from '@multi/shared'; interface RestaurantsProps { - favoriteRestaurantId: number | null; + favoritesRestaurantsIds: number[] | null; } export interface Restaurant { @@ -67,7 +67,7 @@ const store = createStore( { name: STORE_NAME }, withEntities(), withProps({ - favoriteRestaurantId: null + favoritesRestaurantsIds: [] }) ); @@ -84,14 +84,33 @@ export const setRestaurants = (restaurants: Restaurant[]) => { export const clearRestaurant = () => store.reset(); +export const getFavoritesRestaurantsIds = () => { + let favoritesRestaurantsIdsArray: number[] = []; + + favoritesRestaurantsIds$.subscribe({ + next(value) { + favoritesRestaurantsIdsArray = value; + } + }); + return favoritesRestaurantsIdsArray; +}; + export const setFavoriteRestaurant = (restaurantId: number) => { store.update( setProps({ - favoriteRestaurantId: restaurantId, + favoritesRestaurantsIds: [...getFavoritesRestaurantsIds(),restaurantId], + }) + ); +}; + +export const unsetFavoriteRestaurant = (restaurantId: number) => { + store.update( + setProps({ + favoritesRestaurantsIds: getFavoritesRestaurantsIds().filter(item => item !== restaurantId), }) ); }; -export const favoriteRestaurantId$ = store.pipe(select((state) => state.favoriteRestaurantId)); +export const favoritesRestaurantsIds$ = store.pipe(select((state) => state.favoritesRestaurantsIds)); export const getRestaurantById = (restaurantId: number) => store.pipe(selectEntity(restaurantId));