Skip to content

Commit debda63

Browse files
author
David Emory
committed
feat(form): Support auto-population of from field with current location
1 parent 870f3c4 commit debda63

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

lib/actions/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
import { createAction } from 'redux-actions'
22

33
export const setAutoPlan = createAction('SET_AUTOPLAN')
4+
5+
// TODO: this should eventually be handled via mapState
6+
export const setMapCenter = createAction('SET_MAP_CENTER')

lib/actions/map.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ export function setLocation (payload) {
3131
}
3232
}
3333

34+
/* payload is simply { type: 'from'|'to' }; location filled in automatically */
35+
36+
export function setLocationToCurrent (payload) {
37+
return function (dispatch, getState) {
38+
const currentPosition = getState().otp.location.currentPosition
39+
if (!currentPosition) return
40+
payload.location = {
41+
lat: currentPosition.coords.latitude,
42+
lon: currentPosition.coords.longitude,
43+
name: '(Current Location)'
44+
}
45+
dispatch(settingLocation(payload))
46+
dispatch(formChanged())
47+
}
48+
}
49+
3450
export function switchLocations () {
3551
return function (dispatch, getState) {
3652
const {from, to} = getState().otp.currentQuery

lib/components/form/location-field.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { FormGroup, FormControl, InputGroup, DropdownButton, MenuItem } from 're
55
import { connect } from 'react-redux'
66
import { autocomplete } from 'isomorphic-mapzen-search'
77

8-
import { setLocation, clearLocation } from '../../actions/map'
8+
import { setLocation, setLocationToCurrent, clearLocation } from '../../actions/map'
99
import { addLocationSearch } from '../../actions/location'
1010
import { distanceStringImperial } from '../../util/distance'
1111

@@ -27,7 +27,8 @@ class LocationField extends Component {
2727
// dispatch
2828
addLocationSearch: PropTypes.func,
2929
clearLocation: PropTypes.func,
30-
setLocation: PropTypes.func
30+
setLocation: PropTypes.func,
31+
setLocationToCurrent: PropTypes.func
3132
}
3233

3334
constructor (props) {
@@ -78,11 +79,7 @@ class LocationField extends Component {
7879

7980
const currentLocationOption = currentPosition !== null
8081
? createOption('location-arrow', 'Use Current Location', () => {
81-
this.props.setLocation(this.props.type, {
82-
lat: currentPosition.coords.latitude,
83-
lon: currentPosition.coords.longitude,
84-
name: '(Current Location)'
85-
})
82+
this.props.setLocationToCurrent(this.props.type)
8683
})
8784
: null
8885

@@ -275,6 +272,7 @@ const mapDispatchToProps = (dispatch, ownProps) => {
275272
return {
276273
addLocationSearch: (location) => { dispatch(addLocationSearch({ location })) },
277274
setLocation: (type, location) => { dispatch(setLocation({ type, location })) },
275+
setLocationToCurrent: (type) => { dispatch(setLocationToCurrent({ type })) },
278276
clearLocation: (type) => { dispatch(clearLocation({ type })) }
279277
}
280278
}

lib/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import ItineraryCarousel from './components/narrative/itinerary-carousel'
2222
import NarrativeItineraries from './components/narrative/narrative-itineraries'
2323
import NarrativeItinerary from './components/narrative/narrative-itinerary'
2424

25-
import { setAutoPlan } from './actions/config'
25+
import { setAutoPlan, setMapCenter } from './actions/config'
2626
import { getCurrentPosition } from './actions/location'
27+
import { setLocationToCurrent } from './actions/map'
2728
import { findNearbyStops } from './actions/api'
2829
import { setShowExtendedSettings } from './actions/ui'
2930

@@ -73,6 +74,8 @@ export {
7374
findNearbyStops,
7475
getCurrentPosition,
7576
setAutoPlan,
77+
setLocationToCurrent,
78+
setMapCenter,
7679
setShowExtendedSettings,
7780

7881
// redux utilities

lib/reducers/create-otp-reducer.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ function createOtpReducer (config, initialQuery) {
150150

151151
case 'SET_AUTOPLAN':
152152
return update(state, { config: { autoPlan: { $set: action.payload.autoPlan } } })
153+
case 'SET_MAP_CENTER':
154+
return update(state, { config: { map: {
155+
initLat: { $set: action.payload.lat },
156+
initLon: { $set: action.payload.lon }
157+
} } })
153158

154159
case 'POSITION_RESPONSE':
155160
return update(state, { location: { currentPosition: { $set: action.payload.position } } })

0 commit comments

Comments
 (0)