mirror of
https://gitlab.com/wheres-the-tp/ui-mobile.git
synced 2026-01-25 05:54:56 -06:00
84 lines
2.3 KiB
JavaScript
84 lines
2.3 KiB
JavaScript
import { ReplaySubject } from 'rxjs';
|
|
import { buildPlaceRecord } from '../records/PlaceRecord';
|
|
import { Map } from 'immutable';
|
|
import { findNearbyPlaces } from '../apis/GooglePlacesApi';
|
|
import { path } from 'ramda';
|
|
import { type GooglePlaceObj } from '../records/PlaceRecord';
|
|
import { setById } from '../helpers/ImmutableHelpers';
|
|
import location$ from './LocationStream';
|
|
import filter$ from './FilterStream';
|
|
import FilterRecord from '../records/FilterRecord';
|
|
// import products$ from './ProductsStream';
|
|
import PlaceRecord from '../records/PlaceRecord';
|
|
import geodist from 'geodist';
|
|
|
|
const getGeoDist = begin => end => geodist(begin, end, { exact: true });
|
|
|
|
const placesSubject = new ReplaySubject();
|
|
|
|
export function emitter(val?: ?PlaceRecord) {
|
|
placesSubject.next(val);
|
|
}
|
|
|
|
filter$.subscribe(() => emitter(null));
|
|
|
|
// products$
|
|
// .mergeMap((products = Map()) => Observable.from(products.toArray()))
|
|
// .mergeMap(([productId, product]) => getPlaceDetails(product))
|
|
// .map(buildPlaceRecord)
|
|
// .subscribe(emitter);
|
|
|
|
location$
|
|
.combineLatest(filter$)
|
|
.debounceTime(200)
|
|
.mergeMap(([location, filter]: [?Position, FilterRecord]) => {
|
|
return findNearbyPlaces({
|
|
location,
|
|
radius: filter.radius,
|
|
search: filter.search,
|
|
});
|
|
})
|
|
.map((val: { location: ?Position, places: ?Array<GooglePlaceObj> }) => {
|
|
if (!val) {
|
|
return;
|
|
}
|
|
|
|
if (val.error || !val.places) {
|
|
console.log(val.error); // eslint-disable-line no-console
|
|
return places;
|
|
}
|
|
|
|
const { places } = val;
|
|
const coords = path(['location', 'coords'], val) || {};
|
|
const getDist = getGeoDist({ lat: coords.latitude, lon: coords.longitude });
|
|
|
|
return (places || [])
|
|
.map((place: GooglePlaceObj) =>
|
|
buildPlaceRecord({
|
|
...place,
|
|
distance: getDist({ lat: place.geometry.location.lat, lon: place.geometry.location.lng }),
|
|
})
|
|
)
|
|
.sort((left, right) => left.distance - right.distance);
|
|
})
|
|
.subscribe(places => places && places.map(emitter));
|
|
|
|
export default placesSubject.scan(
|
|
(typeToPlace, place) => {
|
|
if (!place) {
|
|
return typeToPlace;
|
|
}
|
|
|
|
const { placeType } = place;
|
|
|
|
if (Object.keys(typeToPlace).includes(placeType)) {
|
|
return {
|
|
...typeToPlace,
|
|
[placeType]: [...typeToPlace[placeType], place],
|
|
};
|
|
}
|
|
|
|
return typeToPlace;
|
|
},
|
|
{ HEB: [], WholeFoods: [] }
|
|
);
|