diff --git a/lib/components/form/connect-location-field.js b/lib/components/form/connect-location-field.js index b7de0be84..7e21049d7 100644 --- a/lib/components/form/connect-location-field.js +++ b/lib/components/form/connect-location-field.js @@ -1,29 +1,22 @@ import { connect } from 'react-redux' -import * as mapActions from '../../actions/map' import * as locationActions from '../../actions/location' import * as apiActions from '../../actions/api' import { getActiveSearch, getShowUserSettings } from '../../util/state' /** - * This higher-order component connects the given (styled) LocationField to the redux store. - * It encapsulates the props mapping that must be done explicitly otherwise, - * even when styling a LocationField component that is already connected to the redux store. - * @param LocationFieldComponent The LocationFieldComponent to connect. - * @param options Optional object with the following optional boolean props that determine whether the corresponding - * redux state/action is passed to the component (all default to true): - * - clearLocation - * - location - * - onLocationSelected + * This higher-order component connects the target (styled) LocationField to the + * redux store. + * @param StyledLocationField The input LocationField component to connect. + * @param options Optional object with the following optional props: + * - actions: a list of actions to include in mapDispatchToProps + * - includeLocation: whether to derive the location prop from + * the active query * @returns The connected component. */ -export default function connectLocationField (LocationFieldComponent, options = {}) { - const { - clearLocation: clearLocationProp = true, - location: locationProp = true, - onLocationSelected: onLocationSelectedProp = true - } = options - +export default function connectLocationField (StyledLocationField, options = {}) { + // By default, set actions to empty list and do not include location. + const {actions = [], includeLocation = false} = options const mapStateToProps = (state, ownProps) => { const { config, currentQuery, location, transitIndex, user } = state.otp const { currentPosition, nearbyStops, sessionSearches } = location @@ -33,15 +26,13 @@ export default function connectLocationField (LocationFieldComponent, options = const stateToProps = { currentPosition, geocoderConfig: config.geocoder, + location: includeLocation ? query[ownProps.locationType] : undefined, nearbyStops, sessionSearches, showUserSettings: getShowUserSettings(state.otp), stopsIndex: transitIndex.stops, userLocationsAndRecentPlaces: [...user.locations, ...user.recentPlaces] } - if (locationProp) { - stateToProps.location = query[ownProps.locationType] - } return stateToProps } @@ -49,14 +40,9 @@ export default function connectLocationField (LocationFieldComponent, options = const mapDispatchToProps = { addLocationSearch: locationActions.addLocationSearch, findNearbyStops: apiActions.findNearbyStops, - getCurrentPosition: locationActions.getCurrentPosition - } - if (clearLocationProp) { - mapDispatchToProps.clearLocation = mapActions.clearLocation - } - if (onLocationSelectedProp) { - mapDispatchToProps.onLocationSelected = mapActions.onLocationSelected + getCurrentPosition: locationActions.getCurrentPosition, + ...actions } - return connect(mapStateToProps, mapDispatchToProps)(LocationFieldComponent) + return connect(mapStateToProps, mapDispatchToProps)(StyledLocationField) } diff --git a/lib/components/form/connected-location-field.js b/lib/components/form/connected-location-field.js index e7001683d..6be2c99d6 100644 --- a/lib/components/form/connected-location-field.js +++ b/lib/components/form/connected-location-field.js @@ -9,6 +9,7 @@ import { } from '@opentripplanner/location-field/lib/styled' import styled from 'styled-components' +import * as mapActions from '../../actions/map' import connectLocationField from './connect-location-field' const StyledLocationField = styled(LocationField)` @@ -51,4 +52,10 @@ const StyledLocationField = styled(LocationField)` } ` -export default connectLocationField(StyledLocationField) +export default connectLocationField(StyledLocationField, { + actions: { + clearLocation: mapActions.clearLocation, + onLocationSelected: mapActions.onLocationSelected + }, + includeLocation: true +}) diff --git a/lib/components/form/intermediate-place-field.js b/lib/components/form/intermediate-place-field.js index aaa334b8b..82a8ba3f8 100644 --- a/lib/components/form/intermediate-place-field.js +++ b/lib/components/form/intermediate-place-field.js @@ -74,7 +74,4 @@ class IntermediatePlaceField extends Component { } } -export default connectLocationField(IntermediatePlaceField, { - location: false, - onLocationSelected: false -}) +export default connectLocationField(IntermediatePlaceField) diff --git a/lib/components/user/favorite-location.js b/lib/components/user/favorite-location.js index 00ada56fc..eb3f3dd5a 100644 --- a/lib/components/user/favorite-location.js +++ b/lib/components/user/favorite-location.js @@ -39,11 +39,7 @@ const StyledLocationField = styled(LocationField)` width: 100%; } ` -const FavoriteLocationField = connectLocationField(StyledLocationField, { - clearLocation: false, - location: false, - onLocationSelected: false -}) +const FavoriteLocationField = connectLocationField(StyledLocationField) // Styles for other controls const StyledDropdown = styled(DropdownButton)`