From 3a0d86ff28c9b68d07fb54677a060de4e4b07399 Mon Sep 17 00:00:00 2001 From: Bart Akeley Date: Sun, 15 Jul 2018 11:28:38 -0500 Subject: [PATCH] clear places between searches --- js/apis/GooglePlacesApi.js | 30 +++++++++++++++++++++--------- js/components/FoodItemTile.js | 6 ++++-- js/pages/PlacesList.js | 26 +++++++++++--------------- js/streams/PlacesStream.js | 26 ++++++++++++++++---------- 4 files changed, 52 insertions(+), 36 deletions(-) diff --git a/js/apis/GooglePlacesApi.js b/js/apis/GooglePlacesApi.js index fe1fa59..08b93a8 100644 --- a/js/apis/GooglePlacesApi.js +++ b/js/apis/GooglePlacesApi.js @@ -81,16 +81,28 @@ export const findNearbyPlaces = async ({ const rad = `radius=${milesToMeters(radius)}`; const reqUrl = `${placesFromTextUrl}&${keyword}&${loc}&${rad}&type=restaurant`; - const response: GoogleFindPlaceResponse = await (await fetch(reqUrl)).json(); + let error; + let places; + try { + const response: GoogleFindPlaceResponse = await (await fetch(reqUrl)).json(); - // if (response.next_page_token) { - // const page = await getPageResults({ pageToken: response.next_page_token }); - // return concat(response.results, page.results); - // } + // if (response.next_page_token) { + // const page = await getPageResults({ pageToken: response.next_page_token }); + // return concat(response.results, page.results); + // } - if (response.status !== 'OK') { - throw new Error('google find places request failed'); + if (response.status !== 'OK') { + throw new Error('google find places request failed'); + } + + places = response.results; + } catch (err) { + console.log(error); // eslint-disable-line no-console + error = err; } - - return { location, places: response.results }; + return { + error, + location, + places, + }; }; diff --git a/js/components/FoodItemTile.js b/js/components/FoodItemTile.js index 13dc7c6..909b597 100644 --- a/js/components/FoodItemTile.js +++ b/js/components/FoodItemTile.js @@ -11,9 +11,11 @@ import { TileBox, StrongText, SubText, Thumbnail, QuantityLine } from './ItemTil import { withPlace } from '../enhancers/placeEnhancers'; const PlaceNameAndDistance = withPlace( - ({ place = {}, distance = 999.9 }: { place: PlaceRecord, distance: number }) => { + ({ place, distance = 999.9 }: { place: ?PlaceRecord, distance: number }) => { return ( - {`${place.name || 'Loading...'} - ${parseFloat(distance).toFixed(1)} mi`} + {`${(place && place.name) || 'Loading...'} - ${parseFloat(distance).toFixed( + 1 + )} mi`} ); } ); diff --git a/js/pages/PlacesList.js b/js/pages/PlacesList.js index d8f955c..c363342 100644 --- a/js/pages/PlacesList.js +++ b/js/pages/PlacesList.js @@ -11,27 +11,23 @@ import typeof PlaceRecord from '../records/PlaceRecord'; type Props = { foodItemsByPlace: Map>, - places: Map, + places: ?Map, onRefresh: () => Promise, onPulldown: () => {}, isRefreshing: boolean, }; -const PlacesList = ({ - foodItemsByPlace = Map(), - places = Map(), - isRefreshing, - onPulldown, -}: Props) => { +const PlacesList = ({ foodItemsByPlace = Map(), places, isRefreshing, onPulldown }: Props) => { + const refreshing = isRefreshing || !places; return ( - }> - {places - .map((place: PlaceRecord, placeId: string) => { - const foodItems = foodItemsByPlace.get(placeId, new List()); - return ; - }) - .toList()} + }> + {places && + places + .map((place: PlaceRecord, placeId: string) => { + const foodItems = foodItemsByPlace.get(placeId, new List()); + return ; + }) + .toList()} ); }; diff --git a/js/streams/PlacesStream.js b/js/streams/PlacesStream.js index 501388b..f3a2cd8 100644 --- a/js/streams/PlacesStream.js +++ b/js/streams/PlacesStream.js @@ -3,7 +3,7 @@ import { ReplaySubject, Observable } from 'rxjs'; import { buildPlaceRecord } from '../records/PlaceRecord'; import { Map } from 'immutable'; import { findNearbyPlaces, getPlaceDetails } from '../apis/GooglePlacesApi'; -import { memoize } from 'ramda'; +import { memoize, path } from 'ramda'; import { type GooglePlaceObj } from '../records/PlaceRecord'; import { setById } from '../helpers/ImmutableHelpers'; import location$ from './LocationStream'; @@ -36,7 +36,7 @@ export function emitter(val?: ?PlaceRecord) { placesSubject.next(val); } -emitter(null); +filter$.subscribe(() => emitter(null)); foodItems$ .mergeMap((foodItems = Map()) => Observable.from(foodItems.toArray())) @@ -46,19 +46,20 @@ foodItems$ location$ .combineLatest(filter$) - .mergeMap(([location, filter]: [?Position, FilterRecord]) => - safeFindNearbyPlaces({ + .mergeMap(([location, filter]: [?Position, FilterRecord]) => { + return safeFindNearbyPlaces({ location, radius: filter.radius, search: filter.search, - }) - ) - .map((val: { location: Position, places: Array }) => { + }); + }) + .map((val: { location: ?Position, places: ?Array }) => { if (!val) { return; } - const { location, places } = val; - const getDist = getGeoDist({ lat: location.coords.latitude, lon: location.coords.longitude }); + const { places } = val; + const coords = path(['location', 'coords'], val) || {}; + const getDist = getGeoDist({ lat: coords.latitude, lon: coords.longitude }); return (places || []).map((place: GooglePlaceObj) => buildPlaceRecord({ @@ -69,4 +70,9 @@ location$ }) .subscribe(places => places && places.map(emitter)); -export default placesSubject.distinct().scan(setById, new Map()); +export default placesSubject.scan((places, place) => { + if (!place) { + return null; + } + return setById(places || new Map(), place); +});