Skip to content

Commit af09276

Browse files
authored
feat: add stops list in the line profile page (#929)
1 parent 11aa66d commit af09276

File tree

1 file changed

+48
-32
lines changed

1 file changed

+48
-32
lines changed

src/pages/lineProfile/LineProfile.tsx

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Grid from '@mui/material/Unstable_Grid2'
22
import { useTranslation } from 'react-i18next'
33
import { useLoaderData } from 'react-router-dom'
4-
import { useContext, useEffect, useState } from 'react'
4+
import { useCallback, useContext, useEffect, useMemo, useState } from 'react'
55
import moment from 'moment'
66
import { Tooltip } from 'antd'
77
import CircularProgress from '@mui/material/CircularProgress'
@@ -15,11 +15,11 @@ import LineProfileHeader from './LineProfileHeader'
1515
import { LineProfileDetails } from './LineProfileDetails'
1616
import { Route } from './Route.interface'
1717
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'
2120
import { useSingleLineData } from 'src/hooks/useSingleLineData'
2221
import './LineProfile.scss'
22+
import StopSelector from 'src/pages/components/StopSelector'
2323

2424
const LineProfileWrapper = () => (
2525
<PageContainer className="line-data-container">
@@ -29,37 +29,41 @@ const LineProfileWrapper = () => (
2929

3030
const LineProfile = () => {
3131
const { t } = useTranslation()
32-
const {
33-
search: { timestamp },
34-
setSearch,
35-
} = useContext(SearchContext)
3632
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+
)
3941

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])
4353

4454
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 })),
5461
)
55-
.then((routes) => setAvailableRoutes(routes))
56-
.catch((err) => {
57-
console.error(err)
58-
controller.abort()
59-
})
62+
}, [route, routeKey, clearStops])
6063

61-
return () => controller.abort()
62-
}, [route])
64+
useEffect(() => {
65+
window.scrollTo(0, 0)
66+
}, [])
6367

6468
const {
6569
filteredPositions,
@@ -91,10 +95,22 @@ const LineProfile = () => {
9195
}
9296
/>
9397
<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 }))}
97101
/>
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+
)}
98114
<div className="startTime">
99115
{locationsAreLoading && (
100116
<Tooltip title={t('loading_times_tooltip_content')}>

0 commit comments

Comments
 (0)