1
1
import PropTypes from 'prop-types' ;
2
- import React , { useEffect , useContext , useState } from 'react' ;
2
+ import React , { useEffect , useContext , useState , useRef } from 'react' ;
3
3
import { matchShape , routerShape } from 'found' ;
4
4
import { connectToStores } from 'fluxible-addons-react' ;
5
5
import distance from '@digitransit-search-util/digitransit-search-util-distance' ;
@@ -57,11 +57,12 @@ function StopPageMap(
57
57
const maxShowRouteDistance = breakpoint === 'large' ? 900 : 470 ;
58
58
const { environment } = useContext ( ReactRelayContext ) ;
59
59
const [ walk , setWalk ] = useState ( null ) ;
60
+ const [ bounds , setBounds ] = useState ( null ) ;
61
+ const isRouting = useRef ( false ) ;
60
62
61
63
useEffect ( ( ) => {
62
- let isMounted = true ;
63
64
const fetchWalk = async targetStop => {
64
- if ( locationState . hasLocation && locationState . address ) {
65
+ if ( locationState . hasLocation ) {
65
66
if ( distance ( locationState , stop ) < maxShowRouteDistance ) {
66
67
const settings = getSettings ( config ) ;
67
68
const variables = {
@@ -87,22 +88,32 @@ function StopPageMap(
87
88
fetchQuery ( environment , walkQuery , variables )
88
89
. toPromise ( )
89
90
. 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
+ ) ;
95
94
} ) ;
96
95
}
97
96
}
98
97
} ;
99
- if ( stop && locationState . hasLocation ) {
98
+ if ( ! walk && stop && locationState . hasLocation && ! isRouting . current ) {
99
+ isRouting . current = true ;
100
100
fetchWalk ( stop ) ;
101
101
}
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
+
106
117
if ( locationState . loadingPosition ) {
107
118
return < Loading /> ;
108
119
}
@@ -145,26 +156,12 @@ function StopPageMap(
145
156
}
146
157
const id = match . params . stopId || match . params . terminalId || match . params . id ;
147
158
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 ;
164
162
} else {
165
163
mwtProps . lat = stop . lat ;
166
164
mwtProps . lon = stop . lon ;
167
- mwtProps . zoom = ! match . params . stopId || stop . platformCode ? 18 : 16 ;
168
165
}
169
166
170
167
return (
0 commit comments