clear places between searches

This commit is contained in:
Bart Akeley 2018-07-15 11:28:38 -05:00
parent 9ba55edeb3
commit 3a0d86ff28
4 changed files with 52 additions and 36 deletions

View file

@ -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,
};
};

View file

@ -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 (
<SubText>{`${place.name || 'Loading...'} - ${parseFloat(distance).toFixed(1)} mi`}</SubText>
<SubText>{`${(place && place.name) || 'Loading...'} - ${parseFloat(distance).toFixed(
1
)} mi`}</SubText>
);
}
);

View file

@ -11,27 +11,23 @@ import typeof PlaceRecord from '../records/PlaceRecord';
type Props = {
foodItemsByPlace: Map<string, Map<string, FoodItemRecord>>,
places: Map<string, PlaceRecord>,
places: ?Map<string, PlaceRecord>,
onRefresh: () => Promise<any>,
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 (
<ScrollView
refreshControl={<RefreshControl refreshing={isRefreshing} onRefresh={onPulldown} />}>
{places
.map((place: PlaceRecord, placeId: string) => {
const foodItems = foodItemsByPlace.get(placeId, new List());
return <PlaceTile key={placeId} place={place} foodItems={foodItems} />;
})
.toList()}
<ScrollView refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onPulldown} />}>
{places &&
places
.map((place: PlaceRecord, placeId: string) => {
const foodItems = foodItemsByPlace.get(placeId, new List());
return <PlaceTile key={placeId} place={place} foodItems={foodItems} />;
})
.toList()}
</ScrollView>
);
};

View file

@ -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<GooglePlaceObj> }) => {
});
})
.map((val: { location: ?Position, places: ?Array<GooglePlaceObj> }) => {
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);
});