From fc94dd7395b51ec7d26c68c24d7d9a0e621577f9 Mon Sep 17 00:00:00 2001 From: rass0012 Date: Tue, 10 Dec 2024 15:37:16 +0100 Subject: [PATCH] feat(Restaurant): adapt favourites system to allow multiple favourites --- .../restaurants/src/lib/restaurants.page.html | 4 +-- .../restaurants/src/lib/restaurants.page.ts | 12 ++++----- .../src/lib/restaurants.repository.ts | 27 ++++++++++++++++--- 3 files changed, 31 insertions(+), 12 deletions(-) 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));