Skip to content

Commit dffd054

Browse files
authored
Merge pull request #9 from opentripplanner/react-native-improvements
React native improvements
2 parents 91b442a + 1d79d97 commit dffd054

27 files changed

+1613
-1337
lines changed

Diff for: __tests__/actions/__snapshots__/api.js.snap

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`actions > api should make a query to OTP 1`] = `"/api/plan?arriveBy=false&fromPlace=12%2C34&showIntermediateStops=true&toPlace=34%2C12"`;
4+
5+
exports[`actions > api should make a query to OTP 2`] = `
6+
Array [
7+
Array [
8+
Object {
9+
"type": "PLAN_REQUEST",
10+
},
11+
],
12+
Array [
13+
Object {
14+
"payload": Object {
15+
"fake": "response",
16+
},
17+
"type": "PLAN_RESPONSE",
18+
},
19+
],
20+
]
21+
`;

Diff for: __tests__/actions/api.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* globals describe, expect, it, jest */
2+
3+
import nock from 'nock'
4+
5+
import {planTrip} from '../../lib/actions/api'
6+
7+
function timeoutPromise (ms) {
8+
return new Promise((resolve, reject) => {
9+
setTimeout(resolve, ms)
10+
})
11+
}
12+
13+
const planTripAction = planTrip()
14+
15+
describe('actions > api', () => {
16+
it('should make a query to OTP', async () => {
17+
nock('http://mock-host.com')
18+
.get(/api\/plan/)
19+
.reply(200, {
20+
fake: 'response'
21+
})
22+
.on('request', (req, interceptor) => {
23+
expect(req.path).toMatchSnapshot()
24+
})
25+
26+
const mockDispatch = jest.fn()
27+
planTripAction(mockDispatch, () => {
28+
return {
29+
otp: {
30+
config: {
31+
api: {
32+
host: 'http://mock-host.com',
33+
path: '/api',
34+
port: 80
35+
}
36+
},
37+
currentQuery: {
38+
from: {
39+
lat: 12,
40+
lon: 34
41+
},
42+
to: {
43+
lat: 34,
44+
lon: 12
45+
}
46+
},
47+
searches: []
48+
}
49+
}
50+
})
51+
52+
// wait for request to complete
53+
await timeoutPromise(100)
54+
55+
expect(mockDispatch.mock.calls).toMatchSnapshot()
56+
})
57+
})

Diff for: __tests__/util/__snapshots__/state.js.snap

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`util > state getDefaultQuery should parse window hash if available 1`] = `
4+
Object {
5+
"date": "2017-02-03",
6+
"departArrive": "DEPART",
7+
"from": Object {
8+
"lat": "12",
9+
"lon": "34",
10+
"name": "12.00000, 34.00000",
11+
},
12+
"mode": undefined,
13+
"time": "12:34",
14+
"to": Object {
15+
"lat": "34",
16+
"lon": "12",
17+
"name": "34.00000, 12.00000",
18+
},
19+
"type": "ITINERARY",
20+
}
21+
`;
File renamed without changes.

Diff for: __tests__/util/state.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* globals describe, expect, it */
2+
3+
import {getDefaultQuery} from '../../lib/util/state'
4+
5+
describe('util > state', () => {
6+
it('getDefaultQuery should parse window hash if available', () => {
7+
window.location.hash = '#plan?arriveBy=false&date=2017-02-03&fromPlace=12,34&toPlace=34,12&time=12:34'
8+
expect(getDefaultQuery()).toMatchSnapshot()
9+
})
10+
})

Diff for: dist/index.css

+58-48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: dist/index.css.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: example.css

+9
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,12 @@
1818
margin: 0px;
1919
padding: 0px;
2020
}
21+
22+
button.header, button.step, .intermediate-stops button {
23+
background: inherit;
24+
color: inherit;
25+
border: 0;
26+
text-align: inherit;
27+
text-decoration: none;
28+
width: 100%;
29+
}

Diff for: lib/actions/api.js

+13-18
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
/* globals fetch */
2+
13
import deepEqual from 'deep-equal'
24
import { createAction } from 'redux-actions'
3-
import fetch from 'isomorphic-fetch'
45
import qs from 'qs'
56

7+
if (typeof (fetch) === 'undefined') {
8+
require('isomorphic-fetch')
9+
}
10+
611
import { hasValidLocation } from '../util/state'
712

813
export const receivedPlanResponse = createAction('PLAN_RESPONSE')
914
export const requestPlanResponse = createAction('PLAN_REQUEST')
1015

11-
export function planTrip () {
16+
export function planTrip (customOtpQueryBuilder) {
1217
return function (dispatch, getState) {
1318
const otpState = getState().otp
1419
const latest = otpState.searches.length && otpState.searches[otpState.searches.length - 1]
@@ -19,7 +24,8 @@ export function planTrip () {
1924
}
2025
if (!hasValidLocation(otpState, 'from') || !hasValidLocation(otpState, 'to')) return // TODO: replace with isQueryValid?
2126
dispatch(requestPlanResponse())
22-
const url = constructPlanQuery(getState().otp.config.api, getState().otp.currentQuery)
27+
const queryBuilderFn = customOtpQueryBuilder || constructPlanQuery
28+
const url = queryBuilderFn(otpState.config.api, otpState.currentQuery)
2329
// setURLSearch(url)
2430
fetch(url)
2531
.then(response => {
@@ -41,24 +47,13 @@ function constructPlanQuery (api, query) {
4147
const planEndpoint = `${api.host}:${api.port}${api.path}/plan`
4248
const { mode, time, date } = query
4349
const params = {
50+
arriveBy: query.departArrive === 'ARRIVE',
51+
date,
4452
fromPlace: `${query.from.lat},${query.from.lon}`,
4553
showIntermediateStops: true,
4654
toPlace: `${query.to.lat},${query.to.lon}`,
47-
mode
48-
}
49-
switch (query.departArrive) {
50-
case 'ARRIVE':
51-
params.arriveBy = true
52-
params.date = date
53-
params.time = time
54-
break
55-
case 'DEPART':
56-
params.arriveBy = false
57-
params.date = date
58-
params.time = time
59-
break
60-
default:
61-
break
55+
mode,
56+
time
6257
}
6358
const stringParams = qs.stringify(params)
6459
// TODO: set url hash based on params

Diff for: lib/components/form/date-time-selector.js

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { connect } from 'react-redux'
77
import { setDepart, setDate, setTime } from '../../actions/form'
88

99
class DateTimeSelector extends Component {
10-
1110
static propTypes = {
1211
location: PropTypes.object,
1312
label: PropTypes.string,

Diff for: lib/components/form/error-message.js

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { connect } from 'react-redux'
44
import { getActiveSearch } from '../../util/state'
55

66
class ErrorMessage extends Component {
7-
87
static propTypes = {
98
itineraries: PropTypes.array
109
}

Diff for: lib/components/form/location-field.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import Geocoder from 'react-select-geocoder'
77
import { setLocation, clearLocation } from '../../actions/map'
88

99
class LocationField extends Component {
10-
1110
static propTypes = {
1211
location: PropTypes.object,
1312
label: PropTypes.string,
@@ -21,12 +20,12 @@ class LocationField extends Component {
2120
_onChange = (value, option) => {
2221
console.log(value, option)
2322
if (value && value.geometry) {
24-
let location = lonlat.fromCoordinates(value.geometry.coordinates)
23+
const location = lonlat.fromCoordinates(value.geometry.coordinates)
2524
location.name = value.properties.label
2625
this.props.setLocation(this.props.type, location)
2726
} else if (value.feature) {
2827
// TODO: remove temp fix required for handling r-s-g geolocated option
29-
let location = lonlat.fromCoordinates(value.feature.geometry.coordinates)
28+
const location = lonlat.fromCoordinates(value.feature.geometry.coordinates)
3029
location.name = value.feature.properties.label
3130
this.props.setLocation(this.props.type, location)
3231
} else {

Diff for: lib/components/form/mode-selector.js

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { connect } from 'react-redux'
55
import { setMode } from '../../actions/form'
66

77
class ModeSelector extends Component {
8-
98
static propTypes = {
109
location: PropTypes.object,
1110
label: PropTypes.string,

Diff for: lib/components/form/plan-trip-button.js

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { connect } from 'react-redux'
55
import { planTrip } from '../../actions/api'
66

77
class PlanTripButton extends Component {
8-
98
static propTypes = {
109
onClick: PropTypes.func
1110
}

Diff for: lib/components/map/base-map.js

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { constructLocation } from '../../util/map'
1010
import { getActiveItinerary, getActiveSearch } from '../../util/state'
1111

1212
class BaseMap extends Component {
13-
1413
static propTypes = {
1514
config: PropTypes.object,
1615
mapClick: PropTypes.func,

Diff for: lib/components/map/endpoints-overlay.js

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import Endpoint from './endpoint'
55
import { setLocation } from '../../actions/map'
66

77
class EndpointsOverlay extends Component {
8-
98
static propTypes = {
109
query: PropTypes.object
1110
}

0 commit comments

Comments
 (0)