-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include trip shape on the VPP map (#415)
* cleaner shape loading * load GTFS shapes for non shuttle routes * /api/shapes/trip/:trip_id endpoint change /api/shapes/:route_id to /api/shapes/route/:route_id add Gtfs.shape_for_trip rename useRouteShapes.ts to useShapes.ts add useTripShape to useShapes.ts * useTripShape in VehiclePropertiesPanel * useRouteShapes returns Shape[] instead of ByRouteId<Shape[] | null> * useTripShape returns Shape[] instead of Shape | null
- Loading branch information
Showing
17 changed files
with
427 additions
and
220 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
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 was deleted.
Oops, something went wrong.
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,77 @@ | ||
import { useEffect, useState } from "react" | ||
import { fetchShapeForRoute, fetchShapeForTrip } from "../api" | ||
import { flatten } from "../helpers/array" | ||
import { isASubwayRoute, subwayRouteShapes } from "../models/subwayRoute" | ||
import { ByRouteId, RouteId, Shape, TripId } from "../schedule" | ||
|
||
// An undefined value indicates that the shapes need to be loaded | ||
// A null value indicates that we are currently loading the shapes | ||
type LoadableShapes = Shape[] | null | undefined | ||
|
||
export const useRouteShapes = (selectedRouteIds: RouteId[]): Shape[] => { | ||
const [shapesByRouteId, setShapesByRouteId] = useState< | ||
ByRouteId<LoadableShapes> | ||
>({}) | ||
|
||
const setLoadingShapesForRoute = (routeId: RouteId) => { | ||
setShapesByRouteId(previousShapesByRouteId => ({ | ||
...previousShapesByRouteId, | ||
[routeId]: null, | ||
})) | ||
} | ||
|
||
const setShapesForRoute = (routeId: RouteId, shapes: Shape[]) => { | ||
setShapesByRouteId(previousShapesByRouteId => ({ | ||
...previousShapesByRouteId, | ||
[routeId]: shapes, | ||
})) | ||
} | ||
|
||
useEffect(() => { | ||
selectedRouteIds.forEach((routeId: RouteId) => { | ||
if (!(routeId in shapesByRouteId)) { | ||
setLoadingShapesForRoute(routeId) | ||
|
||
if (isASubwayRoute(routeId)) { | ||
setShapesForRoute(routeId, subwayRouteShapes(routeId)) | ||
} else { | ||
fetchShapeForRoute(routeId).then((shapes: Shape[]) => | ||
setShapesForRoute(routeId, shapes) | ||
) | ||
} | ||
} | ||
}) | ||
}, [selectedRouteIds, shapesByRouteId]) | ||
|
||
return loadedShapes(shapesByRouteId, selectedRouteIds) | ||
} | ||
|
||
export const useTripShape = (tripId: TripId | null): Shape[] => { | ||
// null means loading | ||
const [shape, setShape] = useState<Shape | null>(null) | ||
|
||
useEffect(() => { | ||
if (tripId !== null) { | ||
fetchShapeForTrip(tripId).then((shapeResult: Shape | null) => | ||
setShape(shapeResult) | ||
) | ||
} | ||
}, []) | ||
|
||
return shape === null ? [] : [shape] | ||
} | ||
|
||
const loadedShapes = ( | ||
shapesByRouteId: ByRouteId<LoadableShapes>, | ||
routeIds: RouteId[] | ||
): Shape[] => | ||
flatten( | ||
routeIds.map((routeId: RouteId): Shape[] => { | ||
const loadableShapes: LoadableShapes = shapesByRouteId[routeId] | ||
if (loadableShapes === undefined || loadableShapes === null) { | ||
return [] | ||
} else { | ||
return loadableShapes | ||
} | ||
}) | ||
) |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.