Skip to content

Commit 9450b1b

Browse files
author
David Emory
committed
feat(form): Make 'use current location' option functional
1 parent a8fd10c commit 9450b1b

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

lib/actions/location.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { createAction } from 'redux-actions'
2+
3+
export const receivedPositionError = createAction('POSITION_ERROR')
4+
export const receivedPositionResponse = createAction('POSITION_RESPONSE')
5+
6+
export function getCurrentPosition () {
7+
return async function (dispatch, getState) {
8+
if (navigator.geolocation) {
9+
navigator.geolocation.getCurrentPosition(position => {
10+
if (position) {
11+
console.log('current loc', position)
12+
dispatch(receivedPositionResponse({ position }))
13+
} else {
14+
dispatch(receivedPositionError())
15+
}
16+
})
17+
} else {
18+
console.log('current position not supported')
19+
dispatch(receivedPositionError())
20+
}
21+
}
22+
}

lib/components/form/location-field.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { setLocation, clearLocation } from '../../actions/map'
99
class LocationField extends Component {
1010
static propTypes = {
1111
config: PropTypes.object,
12+
currentPosition: PropTypes.object,
1213
location: PropTypes.object,
1314
label: PropTypes.string,
1415
type: PropTypes.string, // replace with locationType?
@@ -74,15 +75,25 @@ class LocationField extends Component {
7475
}
7576

7677
render () {
77-
const currentLocationOption = createOption('location-arrow', 'Use Current Location', 'test1')
78+
const { currentPosition } = this.props
79+
80+
const currentLocationOption = currentPosition !== null
81+
? createOption('location-arrow', 'Use Current Location', () => {
82+
this.props.setLocation(this.props.type, {
83+
lat: currentPosition.coords.latitude,
84+
lon: currentPosition.coords.longitude,
85+
name: '(Current Location)'
86+
})
87+
})
88+
: null
7889

7990
let geocodedFeatures = this.state.geocodedFeatures
8091
if (geocodedFeatures.length > 5) geocodedFeatures = geocodedFeatures.splice(0, 5)
8192

82-
let nearbyStops = [] /*{
93+
const nearbyStops = [] /* {
8394
name: 'Stop X',
8495
routes: ['10','11']
85-
}]*/
96+
}] */
8697

8798
const formControlClassname = this.props.type + '-form-control'
8899
return (
@@ -104,7 +115,7 @@ class LocationField extends Component {
104115
noCaret
105116
>
106117
{/* current location option */}
107-
{createOption('location-arrow', 'Use Current Location')}
118+
{currentLocationOption}
108119

109120
{/* geocode search result option(s) */}
110121
{geocodedFeatures.length > 0 && <MenuItem header>Search Results</MenuItem>}
@@ -194,7 +205,8 @@ function createTransitStopOption (name, routes, onSelect) {
194205
const mapStateToProps = (state, ownProps) => {
195206
return {
196207
config: state.otp.config,
197-
location: state.otp.currentQuery[ownProps.type]
208+
location: state.otp.currentQuery[ownProps.type],
209+
currentPosition: state.otp.location.currentPosition
198210
}
199211
}
200212

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import NarrativeItineraries from './components/narrative/narrative-itineraries'
1616
import NarrativeItinerary from './components/narrative/narrative-itinerary'
1717

1818
import { setAutoPlan } from './actions/config'
19+
import { getCurrentPosition } from './actions/location'
1920

2021
import createOtpReducer from './reducers/create-otp-reducer'
2122

@@ -53,6 +54,7 @@ export {
5354
NarrativeItinerary,
5455

5556
// actions
57+
getCurrentPosition,
5658
setAutoPlan,
5759

5860
// redux utilities

lib/reducers/create-otp-reducer.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ function createOtpReducer (config, initialQuery) {
3030
const initialState = {
3131
config: Object.assign(defaultConfig, config),
3232
currentQuery,
33+
location: {
34+
currentPosition: null,
35+
sessionSearches: []
36+
},
3337
searches: [],
3438
activeSearch: 0
3539
}
@@ -108,6 +112,9 @@ function createOtpReducer (config, initialQuery) {
108112
case 'SET_AUTOPLAN':
109113
return update(state, { config: { autoPlan: { $set: action.payload.autoPlan } } })
110114

115+
case 'POSITION_RESPONSE':
116+
return update(state, { location: { currentPosition: { $set: action.payload.position } } })
117+
111118
default:
112119
return state
113120
}

0 commit comments

Comments
 (0)