Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
<div class="border"></div>

<ion-img [src]="restaurant.thumbnailUrl"
(ionError)="restaurant.thumbnailUrl = 'assets/restaurants/fallback.svg'"></ion-img>
<ion-icon (click)="unsetFavoriteRestaurant()" *ngIf="restaurant.favorite"
(ionError)="restaurant.thumbnailUrl = 'assets/icons/restaurants/fallback.svg'"></ion-img>
<ion-icon (click)="unsetFavoriteRestaurant(restaurant.id)" *ngIf="restaurant.favorite"
[attr.aria-label]="'RESTAURANTS.REMOVE_FROM_FAVORITES' | translate: { name: restaurant.title }"
class="app-icon-4 favorite-icon" name="heart">
</ion-icon>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -96,8 +96,8 @@ export class RestaurantsPage implements OnInit {
setFavoriteRestaurant(restaurantId);
}

unsetFavoriteRestaurant() {
setFavoriteRestaurant(null);
unsetFavoriteRestaurant(restaurantId: number) {
unsetFavoriteRestaurant(restaurantId);
}

navigateToRestaurantMenus(restaurantId: number) {
Expand Down Expand Up @@ -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 => {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -67,7 +67,7 @@ const store = createStore(
{ name: STORE_NAME },
withEntities<Restaurant>(),
withProps<RestaurantsProps>({
favoriteRestaurantId: null
favoritesRestaurantsIds: []
})
);

Expand All @@ -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));