-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(RouteViewer): Fetch positions in route and pattern views.
This is done using new component VehiclePositionRetriever.
- Loading branch information
1 parent
a8bfab6
commit 0987271
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { connect } from 'react-redux' | ||
import { useCallback, useEffect, useState } from 'react' | ||
|
||
import * as apiActions from '../../actions/api' | ||
|
||
interface Props { | ||
getVehiclePositionsForRoute: (id: string) => void | ||
refreshSeconds: number | ||
routeId?: string | ||
} | ||
|
||
/** | ||
* Non-visual component that retrieves vehicle positions for the given route. | ||
*/ | ||
const VehiclePositionRetriever = ({ | ||
getVehiclePositionsForRoute, | ||
refreshSeconds, | ||
routeId | ||
}: Props) => { | ||
const [refreshTimer, setRefreshTimer] = useState<NodeJS.Timeout | null>(null) | ||
|
||
const refreshVehiclePositions = useCallback(() => { | ||
if (routeId) { | ||
getVehiclePositionsForRoute(routeId) | ||
} | ||
}, [routeId, getVehiclePositionsForRoute]) | ||
|
||
useEffect(() => { | ||
// Fetch vehicle positions when initially mounting component and a route id is available. | ||
if (routeId) { | ||
refreshVehiclePositions() | ||
|
||
if (!refreshTimer) { | ||
console.log('Set up timer for route ' + routeId || '') | ||
// Refresh vehicle positions per interval set in config. | ||
setRefreshTimer( | ||
setInterval(refreshVehiclePositions, refreshSeconds * 1000) | ||
) | ||
} | ||
} | ||
|
||
return () => { | ||
console.log('Veh pos retrieve unmounting for route ' + routeId || '') | ||
// Stop refreshing vehicle positions for the specified route when this component unmounts. | ||
if (refreshTimer) { | ||
clearInterval(refreshTimer) | ||
setRefreshTimer(null) | ||
} | ||
} | ||
}, [routeId, refreshVehiclePositions, refreshTimer, refreshSeconds]) | ||
|
||
// Component renders nothing. | ||
return null | ||
} | ||
|
||
// connect to redux store | ||
const mapStateToProps = (state: any) => { | ||
return { | ||
refreshSeconds: | ||
state.otp.config.routeViewer?.vehiclePositionRefreshSeconds || 30, | ||
routeId: state.otp.ui.viewedRoute?.routeId | ||
} | ||
} | ||
|
||
const mapDispatchToProps = { | ||
getVehiclePositionsForRoute: apiActions.getVehiclePositionsForRoute | ||
} | ||
|
||
export default connect( | ||
mapStateToProps, | ||
mapDispatchToProps | ||
)(VehiclePositionRetriever) |