diff --git a/src/actions/index.js b/src/actions/index.js index cae9c47..7f7570b 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -25,6 +25,15 @@ function destinationPoint(coordinates) { }; } +function directionRequestStart(request) { + return dispatch => { + dispatch({ + type: types.DIRECTIONS_REQUEST_START, + request + }) + } +} + function setDirections(directions) { return dispatch => { dispatch({ @@ -51,10 +60,14 @@ function setHoverMarker(feature) { function fetchDirections() { return (dispatch, getState) => { - const { api, accessToken, routeIndex, profile, alternatives, congestion, destination, language, exclude } = getState(); + const { api, accessToken, routeIndex, profile, alternatives, congestion, destination, language, exclude, fetchDirectionsRequest } = getState(); // if there is no destination set, do not make request because it will fail if (!(destination && destination.geometry)) return; + if (fetchDirectionsRequest) { + fetchDirectionsRequest.abort(); + } + const query = buildDirectionsQuery(getState); // Request params diff --git a/src/constants/action_types.js b/src/constants/action_types.js index 639faee..9377c6d 100644 --- a/src/constants/action_types.js +++ b/src/constants/action_types.js @@ -1,9 +1,9 @@ export const DESTINATION = 'DESTINATION'; export const DESTINATION_CLEAR = 'DESTINATION_CLEAR'; export const DESTINATION_QUERY = 'DESTINATION_QUERY'; -export const DESTINATION_QUERY_START = 'DESTINATION_QUERY_START'; export const DESTINATION_FROM_COORDINATES = 'DESTINATION_FROM_COORDINATES'; export const DIRECTIONS = 'DIRECTIONS'; +export const DIRECTIONS_REQUEST_START = 'DIRECTIONS_REQUEST_START'; export const DIRECTIONS_PROFILE = 'DIRECTIONS_PROFILE'; export const EVENTS = 'EVENTS'; export const ERROR = 'ERROR'; diff --git a/src/reducers/index.js b/src/reducers/index.js index 02c4d5d..db0103d 100644 --- a/src/reducers/index.js +++ b/src/reducers/index.js @@ -46,6 +46,7 @@ const initialState = { // Directions data directions: [], + fetchDirectionsRequest: null, routeIndex: 0, routePadding: 80 }; @@ -118,10 +119,15 @@ function data(state = initialState, action) { waypoints: [], directions: [] }); - + + case types.DIRECTIONS_REQUEST_START: + return Object.assign({}, state, { + fetchDirectionsRequest: action.request + }); case types.DIRECTIONS: return Object.assign({}, state, { - directions: action.directions + directions: action.directions, + fetchDirectionsRequest: null }); case types.ROUTE_INDEX: