Skip to content

Commit

Permalink
Handled case of late request
Browse files Browse the repository at this point in the history
  • Loading branch information
underoot committed Jan 16, 2024
1 parent bfca411 commit 6b80678
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
26 changes: 24 additions & 2 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ function destinationPoint(coordinates) {
};
}

function directionsRequestStart(request) {
return dispatch => {
dispatch({
type: types.DIRECTIONS_REQUEST_START,
request
})
}
}

function setDirections(directions) {
return dispatch => {
dispatch({
Expand All @@ -51,10 +60,15 @@ 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
Expand All @@ -71,6 +85,8 @@ function fetchDirections() {
const request = new XMLHttpRequest();
request.open('GET', `${api}${profile}/${query}.json?${options.join('&')}`, true);

dispatch(directionsRequestStart(request))

request.onload = () => {
if (request.status >= 200 && request.status < 400) {
var data = JSON.parse(request.responseText);
Expand Down Expand Up @@ -189,7 +205,13 @@ export function clearOrigin() {
}

export function clearDestination() {
return dispatch => {
return (dispatch, getState) => {
const { fetchDirectionsRequest } = getState();

if (fetchDirectionsRequest) {
fetchDirectionsRequest.abort();
}

dispatch({
type: types.DESTINATION_CLEAR
});
Expand Down
2 changes: 1 addition & 1 deletion src/constants/action_types.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
10 changes: 8 additions & 2 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const initialState = {

// Directions data
directions: [],
fetchDirectionsRequest: null,
routeIndex: 0,
routePadding: 80
};
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 6b80678

Please sign in to comment.