Skip to content

Commit 1a57487

Browse files
authored
Merge pull request #5229 from HSLdevcom/DT-6675
DT-6675 stop stop page navigation fight
2 parents 53dcfd5 + 1a884d2 commit 1a57487

File tree

2 files changed

+45
-30
lines changed

2 files changed

+45
-30
lines changed

app/action/PositionActions.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getJson } from '../util/xhrPromise';
33
import geolocationMessages from '../util/geolocationMessages';
44
import { addAnalyticsEvent } from '../util/analyticsUtils';
55

6+
const MOCKPOS = false;
67
let geoWatchId;
78

89
function reverseGeocodeAddress(actionContext, coords) {
@@ -87,6 +88,20 @@ function dispatchGeolocationError(actionContext, error) {
8788
}
8889
}
8990

91+
function mockPositionChange(actionContext) {
92+
const pos = actionContext.getStore('PositionStore').getLocationState();
93+
if (pos.hasLocation) {
94+
const newPos = {
95+
coords: {
96+
latitude: pos.lat + (Math.random() - 0.5) * 0.001,
97+
longitude: pos.lon + (Math.random() - 0.5) * 0.001,
98+
heading: 0,
99+
},
100+
};
101+
geoCallback(actionContext, newPos);
102+
}
103+
}
104+
90105
// set watcher for geolocation
91106
function watchPosition(actionContext) {
92107
const quietTimeoutSeconds = 20;
@@ -96,6 +111,9 @@ function watchPosition(actionContext) {
96111
updateGeolocationMessage(actionContext, 'timeout');
97112
}, quietTimeoutSeconds * 1000);
98113
try {
114+
if (MOCKPOS) {
115+
setInterval(mockPositionChange, 5000, actionContext);
116+
}
99117
geoWatchId = navigator.geolocation.watchPosition(
100118
position => {
101119
updateGeolocationMessage(actionContext);

app/component/map/StopPageMap.js

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import PropTypes from 'prop-types';
2-
import React, { useEffect, useContext, useState } from 'react';
2+
import React, { useEffect, useContext, useState, useRef } from 'react';
33
import { matchShape, routerShape } from 'found';
44
import { connectToStores } from 'fluxible-addons-react';
55
import distance from '@digitransit-search-util/digitransit-search-util-distance';
@@ -57,11 +57,12 @@ function StopPageMap(
5757
const maxShowRouteDistance = breakpoint === 'large' ? 900 : 470;
5858
const { environment } = useContext(ReactRelayContext);
5959
const [walk, setWalk] = useState(null);
60+
const [bounds, setBounds] = useState(null);
61+
const isRouting = useRef(false);
6062

6163
useEffect(() => {
62-
let isMounted = true;
6364
const fetchWalk = async targetStop => {
64-
if (locationState.hasLocation && locationState.address) {
65+
if (locationState.hasLocation) {
6566
if (distance(locationState, stop) < maxShowRouteDistance) {
6667
const settings = getSettings(config);
6768
const variables = {
@@ -87,22 +88,32 @@ function StopPageMap(
8788
fetchQuery(environment, walkQuery, variables)
8889
.toPromise()
8990
.then(result => {
90-
if (isMounted) {
91-
setWalk(
92-
result.plan.edges.length ? result.plan.edges?.[0].node : null,
93-
);
94-
}
91+
setWalk(
92+
result.plan.edges.length ? result.plan.edges?.[0].node : null,
93+
);
9594
});
9695
}
9796
}
9897
};
99-
if (stop && locationState.hasLocation) {
98+
if (!walk && stop && locationState.hasLocation && !isRouting.current) {
99+
isRouting.current = true;
100100
fetchWalk(stop);
101101
}
102-
return () => {
103-
isMounted = false;
104-
};
105-
}, [locationState.status]);
102+
if (
103+
locationState.lat &&
104+
locationState.lon &&
105+
distance(locationState, stop) < maxShowRouteDistance
106+
) {
107+
setBounds([
108+
[locationState.lat, locationState.lon],
109+
[
110+
stop.lat + (stop.lat - locationState.lat),
111+
stop.lon + (stop.lon - locationState.lon),
112+
],
113+
]);
114+
}
115+
}, [stop, locationState.status]);
116+
106117
if (locationState.loadingPosition) {
107118
return <Loading />;
108119
}
@@ -145,26 +156,12 @@ function StopPageMap(
145156
}
146157
const id = match.params.stopId || match.params.terminalId || match.params.id;
147158

148-
const mwtProps = {};
149-
if (
150-
locationState &&
151-
locationState.lat &&
152-
locationState.lon &&
153-
stop.lat &&
154-
stop.lon &&
155-
distance(locationState, stop) < maxShowRouteDistance
156-
) {
157-
mwtProps.bounds = [
158-
[locationState.lat, locationState.lon],
159-
[
160-
stop.lat + (stop.lat - locationState.lat),
161-
stop.lon + (stop.lon - locationState.lon),
162-
],
163-
];
159+
const mwtProps = { zoom: 18 };
160+
if (bounds) {
161+
mwtProps.bounds = bounds;
164162
} else {
165163
mwtProps.lat = stop.lat;
166164
mwtProps.lon = stop.lon;
167-
mwtProps.zoom = !match.params.stopId || stop.platformCode ? 18 : 16;
168165
}
169166

170167
return (

0 commit comments

Comments
 (0)