Skip to content

Commit 17a9a4e

Browse files
authored
Merge pull request #11 from geops/lucien/load_routes
Show loading bar on route request
2 parents 2bdb266 + 4646367 commit 17a9a4e

File tree

7 files changed

+36
-4
lines changed

7 files changed

+36
-4
lines changed

src/Components/MapComponent/MapComponent.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,11 @@ class MapComponent extends Component {
460460
currentMot,
461461
APIKey,
462462
onShowNotification,
463+
onSetShowLoadingBar,
463464
} = this.props;
465+
466+
onSetShowLoadingBar(true);
467+
464468
Object.keys(currentStopsGeoJSON).forEach(key => {
465469
if (currentStopsGeoJSON[key].features) {
466470
// If the current item is a point selected on the map, not a station.
@@ -487,6 +491,7 @@ class MapComponent extends Component {
487491
fetch(reqUrl, { signal })
488492
.then(response => response.json())
489493
.then(response => {
494+
onSetShowLoadingBar(false);
490495
if (response.error) {
491496
onShowNotification("Couldn't find route", 'error');
492497
return;
@@ -567,6 +572,8 @@ const mapDispatchToProps = dispatch => {
567572
dispatch(actions.setClickLocation(clickLocation)),
568573
onShowNotification: (notificationMessage, notificationType) =>
569574
dispatch(actions.showNotification(notificationMessage, notificationType)),
575+
onSetShowLoadingBar: showLoadingBar =>
576+
dispatch(actions.setShowLoadingBar(showLoadingBar)),
570577
};
571578
};
572579

@@ -576,6 +583,7 @@ MapComponent.propTypes = {
576583
stationSearchUrl: PropTypes.string.isRequired,
577584
onSetClickLocation: PropTypes.func.isRequired,
578585
onShowNotification: PropTypes.func.isRequired,
586+
onSetShowLoadingBar: PropTypes.func.isRequired,
579587
onSetCurrentStops: PropTypes.func.isRequired,
580588
onSetCurrentStopsGeoJSON: PropTypes.func.isRequired,
581589
currentStops: propTypeCurrentStops.isRequired,

src/Components/RoutingMenu/RoutingMenu.jsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
setCurrentMot,
2121
showNotification,
2222
setIsFieldFocused,
23+
setShowLoadingBar,
2324
} from '../../store/actions/Map';
2425
import './RoutingMenu.css';
2526
import {
@@ -138,6 +139,7 @@ function RoutingMenu({
138139

139140
const clickLocation = useSelector(state => state.MapReducer.clickLocation);
140141
const currentStops = useSelector(state => state.MapReducer.currentStops);
142+
const showLoadingBar = useSelector(state => state.MapReducer.showLoadingBar);
141143
const currentStopsGeoJSON = useSelector(
142144
state => state.MapReducer.currentStopsGeoJSON,
143145
);
@@ -156,7 +158,6 @@ function RoutingMenu({
156158
const [currentSearchResults, setCurrentSearchResults] = useState([]);
157159
const [searchMotOnly, setSearchMotOnly] = React.useState(true);
158160
const [focusedFieldIndex, setFocusedFieldIndex] = useState(0);
159-
const [showLoadingBar, setShowLoadingBar] = useState(false);
160161
const [currentOtherMot, setCurrentOtherMot] = useState(undefined);
161162

162163
useEffect(() => {
@@ -352,13 +353,13 @@ function RoutingMenu({
352353
updatedCurrentStops[fieldIndex] = '';
353354
setCurrentSearchResults([]);
354355
dispatch(setCurrentStops(updatedCurrentStops));
355-
setShowLoadingBar(false);
356+
dispatch(setShowLoadingBar(false));
356357
return;
357358
}
358359
const updatedCurrentStops = _.clone(currentStops);
359360
updatedCurrentStops[fieldIndex] = event.target.value;
360361
dispatch(setCurrentStops(updatedCurrentStops));
361-
setShowLoadingBar(true);
362+
dispatch(setShowLoadingBar(true));
362363

363364
abortController.abort();
364365
abortController = new AbortController();
@@ -381,7 +382,7 @@ function RoutingMenu({
381382
dispatch(showNotification("Couldn't find stations", 'warning'));
382383
}
383384
setCurrentSearchResults(response.features);
384-
setShowLoadingBar(false);
385+
dispatch(setShowLoadingBar(false));
385386
})
386387
.catch(err => {
387388
if (err.name === 'AbortError') {

src/store/actions/Map.jsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,10 @@ export const setIsFieldFocused = isFieldFocused => {
4242
isFieldFocused,
4343
};
4444
};
45+
46+
export const setShowLoadingBar = showLoadingBar => {
47+
return {
48+
type: actionTypes.SET_SHOW_LOADING_BAR,
49+
showLoadingBar,
50+
};
51+
};

src/store/actions/actionTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export const SET_CURRENT_MOT = 'SET_CURRENT_MOT';
44
export const SET_CLICK_LOCATION = 'SET_CLICK_LOCATION';
55
export const SHOW_NOTIFICATION = 'SHOW_NOTIFICATION';
66
export const SET_IS_FIELD_FOCUSED = 'SET_IS_FIELD_FOCUSED';
7+
export const SET_SHOW_LOADING_BAR = 'SET_SHOW_LOADING_BAR';

src/store/actions/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export {
55
setClickLocation,
66
showNotification,
77
setIsFieldFocused,
8+
setShowLoadingBar,
89
} from './Map';

src/store/reducers/Map.jsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const initialState = {
88
notificationMessage: '',
99
notificationType: 'info',
1010
isFieldFocused: false,
11+
showLoadingBar: false,
1112
};
1213

1314
const setCurrentStops = (state, action) => {
@@ -71,6 +72,16 @@ const setIsFieldFocused = (state, action) => {
7172
};
7273
};
7374

75+
const setShowLoadingBar = (state, action) => {
76+
const updatedState = {
77+
showLoadingBar: action.showLoadingBar,
78+
};
79+
return {
80+
...state,
81+
...updatedState,
82+
};
83+
};
84+
7485
const reducer = (state = initialState, action) => {
7586
switch (action.type) {
7687
case actionTypes.SET_CURRENT_STOPS:
@@ -85,6 +96,8 @@ const reducer = (state = initialState, action) => {
8596
return showNotification(state, action);
8697
case actionTypes.SET_IS_FIELD_FOCUSED:
8798
return setIsFieldFocused(state, action);
99+
case actionTypes.SET_SHOW_LOADING_BAR:
100+
return setShowLoadingBar(state, action);
88101
default:
89102
return state;
90103
}

src/store/reducers/reducer.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe('map reducer', () => {
1212
notificationMessage: '',
1313
notificationType: 'info',
1414
isFieldFocused: false,
15+
showLoadingBar: false,
1516
});
1617
});
1718

0 commit comments

Comments
 (0)