aretherecookies-mobile/js/enhancers/placeEnhancers.js

84 lines
2.1 KiB
JavaScript
Raw Normal View History

import withProps from 'recompose/withProps';
import mapPropsStream from 'recompose/mapPropsStream';
import compose from 'recompose/compose';
import Places$, { emitter as emitPlace } from '../streams/PlacesStream';
2020-04-17 22:50:23 -05:00
import Products$ from '../streams/ProductsStream';
import { path } from 'ramda';
2020-04-17 22:50:23 -05:00
import { List, getIn } from 'immutable';
import { buildPlaceRecord } from '../records/PlaceRecord';
import { getPlaceDetails } from '../apis/GooglePlacesApi';
import { getSearch } from '../helpers/RouteHelpers';
import { withRouterContext } from './routeEnhancers';
2018-08-12 09:51:44 -05:00
2020-04-17 22:50:23 -05:00
export const fetchPlaceDetails = async ({ distance, placeId }) => {
2018-08-12 09:51:44 -05:00
const place = await getPlaceDetails({
distance,
placeId,
2018-08-12 09:51:44 -05:00
});
emitPlace(buildPlaceRecord(place));
};
export const withPlaces = mapPropsStream(props$ =>
props$.combineLatest(Places$, (props, places) => {
return {
...props,
places,
};
})
);
export const withPlaceIdFromRoute = withProps((props: { match: { params: { id: string } } }) => {
const placeId = path(['match', 'params', 'id'], props);
return { placeId };
});
2018-08-19 11:00:17 -05:00
export const withPlaceId = compose(
withRouterContext,
2017-07-02 17:55:00 -05:00
withProps(props => {
2018-08-19 11:00:17 -05:00
const placeId = props.placeId || getSearch(props).placeId;
return { placeId };
2017-07-02 17:55:00 -05:00
})
);
2018-08-19 11:00:17 -05:00
export const withPlaceActions = withProps(() => {
return {
emitPlace,
fetchPlaceDetails,
2018-08-19 11:00:17 -05:00
};
});
export const withPlace = compose(
withPlaceId,
2018-08-12 11:34:45 -05:00
withPlaces,
2020-04-17 22:50:23 -05:00
withProps(({ placeId, places, placeType }) => {
const place =
placeId && placeType && getIn(places, [placeType], []).find(place => place.id === placeId);
return { place, fetchPlaceDetails };
2018-08-12 11:34:45 -05:00
})
);
2020-04-17 22:50:23 -05:00
export const withPlaceForProduct = compose(
2018-08-19 11:00:17 -05:00
withPlaces,
2020-04-17 22:50:23 -05:00
withProps(({ places, product }) => {
if (!product || !product.placeType) {
2018-08-19 11:00:17 -05:00
return { place: null };
}
2020-04-17 22:50:23 -05:00
const place = getIn(places, [product.placeType, 0]);
return { place };
})
);
2017-07-23 19:58:10 -05:00
2020-04-17 22:50:23 -05:00
export const withProductsForPlace = mapPropsStream(props$ =>
props$.combineLatest(Products$, (props, productsMap) => {
2017-07-23 19:58:10 -05:00
const placeId = props.placeId;
2020-04-17 22:50:23 -05:00
const products = productsMap
? productsMap.toList().filter(product => placeId === product.placeId)
: List();
2017-07-23 19:58:10 -05:00
return {
...props,
2020-04-17 22:50:23 -05:00
products,
2017-07-23 19:58:10 -05:00
};
})
);