Skip to content

Commit

Permalink
Merge pull request #21 from opentripplanner/dev
Browse files Browse the repository at this point in the history
New release
  • Loading branch information
evansiroky authored Mar 28, 2017
2 parents b0370f0 + c5fbfce commit 7d03c5b
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 34 deletions.
17 changes: 17 additions & 0 deletions __tests__/actions/__snapshots__/api.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`actions > api > planTrip should gracefully handle bad response 1`] = `
Array [
Array [
Object {
"type": "PLAN_REQUEST",
},
],
Array [
Object {
"error": true,
"payload": [Error: Received error from server],
"type": "PLAN_ERROR",
},
],
]
`;

exports[`actions > api > planTrip should make a query to OTP with customOtpQueryBuilder in state config 1`] = `"/api/plan?from=here&to=there"`;

exports[`actions > api > planTrip should make a query to OTP with customOtpQueryBuilder in state config 2`] = `
Expand Down
20 changes: 20 additions & 0 deletions __tests__/actions/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,25 @@ describe('actions > api', () => {
expect(mockDispatch.mock.calls).toMatchSnapshot()
})
})

it('should gracefully handle bad response', async () => {
const planTripAction = planTrip()

nock('http://mock-host.com')
.get(/api\/plan/)
.reply(500, {
fake: 'response'
})

const mockDispatch = jest.fn()
planTripAction(mockDispatch, () => {
return defaultState
})

// wait for request to complete
await timeoutPromise(100)

expect(mockDispatch.mock.calls).toMatchSnapshot()
})
})
})
9 changes: 0 additions & 9 deletions example.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,3 @@
margin: 0px;
padding: 0px;
}

button.header, button.step, .intermediate-stops button {
background: inherit;
color: inherit;
border: 0;
text-align: inherit;
text-decoration: none;
width: 100%;
}
26 changes: 16 additions & 10 deletions lib/actions/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ if (typeof (fetch) === 'undefined') {

import { queryIsValid } from '../util/state'

export const receivedPlanError = createAction('PLAN_ERROR')
export const receivedPlanResponse = createAction('PLAN_RESPONSE')
export const requestPlanResponse = createAction('PLAN_REQUEST')

export function planTrip (customOtpQueryBuilder) {
return function (dispatch, getState) {
return async function (dispatch, getState) {
const otpState = getState().otp
const latest = otpState.searches.length && otpState.searches[otpState.searches.length - 1]
// check for query change
Expand All @@ -27,15 +28,20 @@ export function planTrip (customOtpQueryBuilder) {
const queryBuilderFn = customOtpQueryBuilder || otpState.config.customOtpQueryBuilder || constructPlanQuery
const url = queryBuilderFn(otpState.config.api, otpState.currentQuery)
// setURLSearch(url)
fetch(url)
.then(response => {
if (response.status >= 400) {
throw new Error('Bad response from server')
}
return response.json()
}).then(json => {
dispatch(receivedPlanResponse(json))
})
let response, json
try {
response = await fetch(url)
if (response.status >= 400) {
const error = new Error('Received error from server')
error.response = response
throw error
}
json = await response.json()
} catch (err) {
return dispatch(receivedPlanError(err))
}

dispatch(receivedPlanResponse(json))
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/components/form/error-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class ErrorMessage extends Component {
if (!error) return null

return (
<div className='error'>
<div className='alert alert-danger error'>
<div className='header'>Error!</div>
<div className='message'>{error.msg}</div>
<div className='message'>{error.message}</div>
</div>
)
}
Expand Down
11 changes: 1 addition & 10 deletions lib/components/form/form.css
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
.otp .error {
margin-bottom: 16px;
padding: 10px;
background-color: #f5f5f5;
border: 1px solid #e3e3e3;
border-radius: 4px
}

.otp .error > .header {
font-size: 16px;
padding-bottom: 4px;
border-bottom: 1px solid black;
margin-bottom: 6px;
border-bottom: 1px solid #dbbec3;
}

.otp .error > .header > .title {
Expand Down
9 changes: 9 additions & 0 deletions lib/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@
@import url(lib/components/map/map.css);
@import url(lib/components/form/form.css);
@import url(lib/components/narrative/narrative.css);

button.header, button.step, .intermediate-stops button {
background: inherit;
color: inherit;
border: 0;
text-align: inherit;
text-decoration: none;
width: 100%;
}
18 changes: 15 additions & 3 deletions lib/reducers/create-otp-reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function createOtpReducer (config, initialQuery) {
}

return (state = initialState, action) => {
const latestSearchIndex = state.searches.length - 1
switch (action.type) {
case 'PLAN_REQUEST':
return update(state, {
Expand All @@ -43,14 +44,25 @@ function createOtpReducer (config, initialQuery) {
activeStep: null
}]}
})
case 'PLAN_ERROR':
return update(state, {
searches: {[latestSearchIndex]: {
planResponse: {
$set: {
error: action.payload
}
},
pending: {$set: false}
}},
activeSearch: { $set: latestSearchIndex }
})
case 'PLAN_RESPONSE':
const latestIndex = state.searches.length - 1
return update(state, {
searches: {[latestIndex]: {
searches: {[latestSearchIndex]: {
planResponse: {$set: action.payload},
pending: {$set: false}
}},
activeSearch: { $set: state.searches.length - 1 }
activeSearch: { $set: latestSearchIndex }
})
case 'SET_ACTIVE_ITINERARY':
if (state.activeSearch !== null) {
Expand Down

0 comments on commit 7d03c5b

Please sign in to comment.