1
1
import Grid from '@mui/material/Unstable_Grid2'
2
2
import { useTranslation } from 'react-i18next'
3
3
import { useLoaderData } from 'react-router-dom'
4
- import { useContext , useEffect , useState } from 'react'
4
+ import { useCallback , useContext , useEffect , useMemo , useState } from 'react'
5
5
import moment from 'moment'
6
6
import { Tooltip } from 'antd'
7
7
import CircularProgress from '@mui/material/CircularProgress'
@@ -15,11 +15,11 @@ import LineProfileHeader from './LineProfileHeader'
15
15
import { LineProfileDetails } from './LineProfileDetails'
16
16
import { Route } from './Route.interface'
17
17
import Widget from 'src/shared/Widget'
18
- import { SearchContext } from 'src/model/pageState'
19
- import { getRoutesAsync } from 'src/api/gtfsService'
20
- import { BusRoute } from 'src/model/busRoute'
18
+ import { SearchContext , TimelinePageState } from 'src/model/pageState'
19
+ import { getStopsForRouteAsync } from 'src/api/gtfsService'
21
20
import { useSingleLineData } from 'src/hooks/useSingleLineData'
22
21
import './LineProfile.scss'
22
+ import StopSelector from 'src/pages/components/StopSelector'
23
23
24
24
const LineProfileWrapper = ( ) => (
25
25
< PageContainer className = "line-data-container" >
@@ -29,37 +29,41 @@ const LineProfileWrapper = () => (
29
29
30
30
const LineProfile = ( ) => {
31
31
const { t } = useTranslation ( )
32
- const {
33
- search : { timestamp } ,
34
- setSearch,
35
- } = useContext ( SearchContext )
36
32
const route = useLoaderData ( ) as Route & { message ?: string }
37
- const [ availableRoutes , setAvailableRoutes ] = useState < BusRoute [ ] | null > ( null )
38
- const [ selectedRouteKey , setSelectedRouteKey ] = useState < string > ( '' )
33
+ const [ state , setState ] = useState < TimelinePageState > ( { } )
34
+ const { stopKey, stops } = state
35
+ const { search, setSearch } = useContext ( SearchContext )
36
+ const { timestamp, routes, routeKey } = search
37
+ const selectedRoute = useMemo (
38
+ ( ) => routes ?. find ( ( route ) => route . key === routeKey ) ,
39
+ [ routes , routeKey ] ,
40
+ )
39
41
40
- useEffect ( ( ) => {
41
- window . scrollTo ( 0 , 0 )
42
- } , [ ] )
42
+ const selectedRouteIds = selectedRoute ?. routeIds
43
+ const clearStops = useCallback ( ( ) => {
44
+ setState ( ( current ) => ( {
45
+ ...current ,
46
+ stops : undefined ,
47
+ stopName : undefined ,
48
+ stopKey : undefined ,
49
+ gtfsHitTimes : undefined ,
50
+ siriHitTimes : undefined ,
51
+ } ) )
52
+ } , [ setState ] )
43
53
44
54
useEffect ( ( ) => {
45
- const controller = new AbortController ( )
46
- const signal = controller . signal
47
-
48
- getRoutesAsync (
49
- moment ( timestamp ) ,
50
- moment ( timestamp ) ,
51
- route . operator_ref . toString ( ) ,
52
- route . route_short_name ,
53
- signal ,
55
+ clearStops ( )
56
+ if ( ! routeKey || ! selectedRouteIds ) {
57
+ return
58
+ }
59
+ getStopsForRouteAsync ( selectedRouteIds , moment ( timestamp ) ) . then ( ( stops ) =>
60
+ setState ( ( current ) => ( { ...current , stops : stops } ) ) ,
54
61
)
55
- . then ( ( routes ) => setAvailableRoutes ( routes ) )
56
- . catch ( ( err ) => {
57
- console . error ( err )
58
- controller . abort ( )
59
- } )
62
+ } , [ route , routeKey , clearStops ] )
60
63
61
- return ( ) => controller . abort ( )
62
- } , [ route ] )
64
+ useEffect ( ( ) => {
65
+ window . scrollTo ( 0 , 0 )
66
+ } , [ ] )
63
67
64
68
const {
65
69
filteredPositions,
@@ -91,10 +95,22 @@ const LineProfile = () => {
91
95
}
92
96
/>
93
97
< RouteSelector
94
- routes = { availableRoutes ?? [ ] }
95
- routeKey = { selectedRouteKey }
96
- setRouteKey = { ( routeKey ) => setSelectedRouteKey ( routeKey ) }
98
+ routes = { routes ?? [ ] }
99
+ routeKey = { routeKey }
100
+ setRouteKey = { ( key ) => setSearch ( ( current ) => ( { ... current , routeKey : key } ) ) }
97
101
/>
102
+ { stops && (
103
+ < StopSelector
104
+ stops = { stops }
105
+ stopKey = { stopKey }
106
+ setStopKey = { ( key ) =>
107
+ setState ( ( current ) => {
108
+ const stop = current . stops ?. find ( ( stop ) => stop . key === key )
109
+ return { ...current , stopKey : key , stopName : stop ?. name }
110
+ } )
111
+ }
112
+ />
113
+ ) }
98
114
< div className = "startTime" >
99
115
{ locationsAreLoading && (
100
116
< Tooltip title = { t ( 'loading_times_tooltip_content' ) } >
0 commit comments