mirror of
https://gitlab.com/wheres-the-tp/ui-mobile.git
synced 2026-01-25 09:44:55 -06:00
33 lines
1.3 KiB
JavaScript
33 lines
1.3 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 PlaceRecord, { 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.log(error); // eslint-disable-line no-console
|
|
return null;
|
|
});
|
|
});
|
|
|
|
export default foodItems$
|
|
.map((foodItems: Map<string, FoodItemRecord>): Array<string> => {
|
|
return foodItems.map(foodItem => foodItem.placeId).toArray();
|
|
})
|
|
.mergeMap((placeIds: Array<string>): Observable<Array<Promise<?GooglePlaceObj>>> => {
|
|
return Observable.forkJoin(placeIds.map(safeGetPlaceDetails));
|
|
})
|
|
.map((places: Array<GooglePlaceObj>): Map<string, PlaceRecord> => {
|
|
return places.map(buildPlaceRecord).reduce(setById, new Map());
|
|
});
|