mirror of
https://gitlab.com/wheres-the-tp/ui-mobile.git
synced 2026-01-25 11:54:55 -06:00
33 lines
1.1 KiB
JavaScript
33 lines
1.1 KiB
JavaScript
// @flow
|
|
import { buildPlaceRecord } from '../records/PlaceRecord';
|
|
import { Map } from 'immutable';
|
|
import foodItems$ from './FoodItemsStream';
|
|
import { getPlaceDetails } from '../apis/PlaceDetailsApi';
|
|
import { memoize } from 'ramda';
|
|
import { Observable } from 'rxjs';
|
|
import typeof FoodItemRecord from '../records/FoodItemRecord';
|
|
import { type GooglePlaceObj } from '../records/PlaceRecord';
|
|
import { setById } from '../helpers/ImmutableHelpers';
|
|
|
|
/**
|
|
* return a promise of a place details object
|
|
* if already requested return existing promise
|
|
* swallow exceptions so as not to break the stream
|
|
*/
|
|
const safeGetPlaceDetails = memoize((placeId: string): Promise<?GooglePlaceObj> => {
|
|
return getPlaceDetails(placeId).catch(error => {
|
|
console.error(error); // eslint-disable-line no-console
|
|
return null;
|
|
});
|
|
});
|
|
|
|
const Places$ = foodItems$
|
|
.mergeMap((foodItems: Map<string, FoodItemRecord>): Observable<string> => {
|
|
return Observable.from(foodItems.toArray().map(foodItem => foodItem.placeId));
|
|
})
|
|
.distinct()
|
|
.mergeMap(safeGetPlaceDetails)
|
|
.map(buildPlaceRecord)
|
|
.scan(setById, new Map());
|
|
|
|
export default Places$;
|