Skip to content

Commit

Permalink
fix(ts/components/NearbyStops): refresh stops when inputs to function…
Browse files Browse the repository at this point in the history
… change (#2258)

* fix(ts/components/NearbyStops): refresh stops when inputs to function change


Asana Ticket: [⚙️🐞 When selected route direction changes excluded nearby bus stops do not update](https://app.asana.com/0/1148853526253420/1205560497394206/f)
Co-authored-by: Eddie Maldonado <emaldonado@mbta.com>
  • Loading branch information
firestack authored Oct 20, 2023
1 parent ed5c944 commit f197371
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions assets/src/components/mapPage/mapDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -455,13 +455,28 @@ const NearbyStops = ({ stops }: { stops: Stop[] }) => {
)
const [nearbyStops, setNearbyStops] = useState<Stop[]>([])
const map = useMap()
map.addEventListener("moveend", () => {

const filterStopsToBounds = useCallback(() => {
const bounds = map.getBounds()
// Only show nearby stations or bus stops
setNearbyStops(
stationsAndBus.filter((s) => bounds.contains([s.lat, s.lon]))
)
})
}, [map, stationsAndBus, setNearbyStops])

// Filter stops whenever function, and its inputs, change
useEffect(() => {
filterStopsToBounds()
}, [filterStopsToBounds])

// Re-Filter stops when finished moving the map
useEffect(() => {
const mapEvent = "moveend"
map.addEventListener(mapEvent, filterStopsToBounds)
return () => {
map.removeEventListener(mapEvent, filterStopsToBounds)
}
}, [map, filterStopsToBounds])

return (
<ZoomLevelWrapper>
Expand Down

0 comments on commit f197371

Please sign in to comment.