Skip to content

Commit 967a995

Browse files
committed
feat(actions): add ability to switch locations
1 parent 91fd5ee commit 967a995

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

example.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import thunk from 'redux-thunk'
77
import createLogger from 'redux-logger'
88

99
// import Bootstrap Grid components for layout
10-
import { Navbar, Grid, Row, Col } from 'react-bootstrap'
10+
import { Navbar, Grid, Row, Col, Button } from 'react-bootstrap'
1111

1212
// import OTP-RR components
1313
import {
@@ -25,6 +25,8 @@ import {
2525
ErrorMessage
2626
} from './lib'
2727

28+
import SwitchButton from './lib/components/form/switchButton'
29+
2830
// load the OTP configuration
2931
import otpConfig from './config.yml'
3032

@@ -63,6 +65,7 @@ class OtpRRExample extends Component {
6365
<Col xs={12} md={4} className='sidebar'>
6466
<LocationField type='from' label='Enter start location or click on map...' />
6567
<LocationField type='to' label='Enter destination or click on map...' />
68+
<SwitchButton />
6669
<ModeSelector />
6770
<DateTimeSelector />
6871
<ErrorMessage />

lib/actions/map.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ import { formChanged } from './form'
1313
* }
1414
*/
1515

16-
export const settingLocation = createAction('SET_LOCATION')
1716
export const clearingLocation = createAction('CLEAR_LOCATION')
17+
export const settingLocation = createAction('SET_LOCATION')
18+
export const switchingLocations = createAction('SWITCH_LOCATIONS')
19+
20+
export function clearLocation (payload) {
21+
return function (dispatch, getState) {
22+
dispatch(clearingLocation(payload))
23+
dispatch(formChanged())
24+
}
25+
}
1826

1927
export function setLocation (payload) {
2028
return function (dispatch, getState) {
@@ -23,9 +31,17 @@ export function setLocation (payload) {
2331
}
2432
}
2533

26-
export function clearLocation (payload) {
34+
export function switchLocations () {
2735
return function (dispatch, getState) {
28-
dispatch(clearingLocation(payload))
36+
const {from, to} = getState().otp.currentQuery
37+
dispatch(settingLocation({
38+
type: 'from',
39+
location: to
40+
}))
41+
dispatch(settingLocation({
42+
type: 'to',
43+
location: from
44+
}))
2945
dispatch(formChanged())
3046
}
3147
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React, { Component, PropTypes } from 'react'
2+
import { Button } from 'react-bootstrap'
3+
import { connect } from 'react-redux'
4+
5+
import { switchLocations } from '../../actions/map'
6+
7+
class SwitchButton extends Component {
8+
static propTypes = {
9+
onClick: PropTypes.func
10+
}
11+
_onClick = () => {
12+
this.props.switchLocations()
13+
}
14+
render () {
15+
return (
16+
<Button
17+
onClick={this._onClick || this.props.onClick}
18+
>Switch</Button>
19+
)
20+
}
21+
}
22+
23+
const mapStateToProps = (state, ownProps) => {
24+
return {}
25+
}
26+
27+
const mapDispatchToProps = (dispatch, ownProps) => {
28+
return {
29+
switchLocations: () => { dispatch(switchLocations()) }
30+
}
31+
}
32+
33+
export default connect(mapStateToProps, mapDispatchToProps)(SwitchButton)

0 commit comments

Comments
 (0)