Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nearby view: react to user location #1159

Merged
merged 8 commits into from
May 12, 2024
14 changes: 14 additions & 0 deletions __tests__/components/viewers/__snapshots__/nearby-view.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ exports[`components > viewers > nearby view renders nothing on a blank page 1`]
>
<Connect(NearbyView)>
<NearbyView
currentPosition={
Object {
"coords": null,
"error": null,
"fetching": false,
}
}
fetchNearby={[Function]}
homeTimezone="America/Los_Angeles"
nearby={Array []}
Expand Down Expand Up @@ -132,6 +139,13 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = `
>
<Connect(NearbyView)>
<NearbyView
currentPosition={
Object {
"coords": null,
"error": null,
"fetching": false,
}
}
fetchNearby={[Function]}
homeTimezone="America/Los_Angeles"
nearby={
Expand Down
10 changes: 9 additions & 1 deletion lib/components/map/default-map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '../../actions/api'
import { ComponentContext } from '../../util/contexts'
import { getActiveItinerary, getActiveSearch } from '../../util/state'
import { getCurrentPosition } from '../../actions/location'
import { MainPanelContent } from '../../actions/ui-constants'
import { setLocation, setMapPopupLocationAndGeocode } from '../../actions/map'
import { setViewedStop } from '../../actions/ui'
Expand Down Expand Up @@ -255,6 +256,7 @@ class DefaultMap extends Component {
carRentalQuery,
carRentalStations,
config,
getCurrentPosition,
intl,
itinerary,
mapConfig,
Expand Down Expand Up @@ -317,7 +319,12 @@ class DefaultMap extends Component {
<EndpointsOverlay />
<RouteViewerOverlay />
<TransitVehicleOverlay ModeIcon={ModeIcon} />
<GeolocateControl position="top-left" />
<GeolocateControl
onGeolocate={() => {
getCurrentPosition(intl)
}}
position="top-left"
/>
<TransitiveOverlay
getTransitiveRouteLabel={getTransitiveRouteLabel}
/>
Expand Down Expand Up @@ -433,6 +440,7 @@ const mapStateToProps = (state) => {
const mapDispatchToProps = {
bikeRentalQuery,
carRentalQuery,
getCurrentPosition,
setLocation,
setMapPopupLocationAndGeocode,
setViewedStop,
Expand Down
25 changes: 21 additions & 4 deletions lib/components/viewers/nearby/nearby-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ const AUTO_REFRESH_INTERVAL = 15000

// TODO: use lonlat package
type LatLonObj = { lat: number; lon: number }
type CurrentPosition = { coords?: { latitude: number; longitude: number } }

type Props = {
currentPosition?: CurrentPosition
displayedCoords?: LatLonObj
entityId?: string
fetchNearby: (latLon: LatLonObj, radius?: number) => void
Expand Down Expand Up @@ -68,13 +70,20 @@ const getNearbyItem = (place: any) => {
}
}

function getNearbyCoordsFromUrlOrMapCenter(
function getNearbyCoordsFromUrlOrLocationOrMapCenter(
coordsFromUrl?: LatLonObj,
currentPosition?: CurrentPosition,
map?: MapRef
): LatLonObj | null {
if (coordsFromUrl) {
return coordsFromUrl
}

if (currentPosition?.coords) {
const { latitude: lat, longitude: lon } = currentPosition.coords
return { lat, lon }
}

const rawMapCoords = map?.getCenter()
const mapCoords = rawMapCoords !== undefined && {
lat: rawMapCoords.lat,
Expand All @@ -87,6 +96,7 @@ function getNearbyCoordsFromUrlOrMapCenter(
}

function NearbyView({
currentPosition,
displayedCoords,
entityId,
fetchNearby,
Expand All @@ -105,8 +115,13 @@ function NearbyView({
const [loading, setLoading] = useState(true)
const firstItemRef = useRef<HTMLDivElement>(null)
const finalNearbyCoords = useMemo(
() => getNearbyCoordsFromUrlOrMapCenter(nearbyViewCoords, map),
[nearbyViewCoords, map]
() =>
getNearbyCoordsFromUrlOrLocationOrMapCenter(
nearbyViewCoords,
currentPosition,
map
),
[nearbyViewCoords, currentPosition, map]
)

// Make sure the highlighted location is cleaned up when leaving nearby
Expand Down Expand Up @@ -266,11 +281,13 @@ function NearbyView({
}

const mapStateToProps = (state: AppReduxState) => {
const { config, transitIndex, ui } = state.otp
const { config, location, transitIndex, ui } = state.otp
const { nearbyViewCoords } = ui
const { nearby } = transitIndex
const { entityId } = state.router.location.query
const { currentPosition } = location
return {
currentPosition,
displayedCoords: nearby?.coords,
entityId: entityId && decodeURIComponent(entityId),
homeTimezone: config.homeTimezone,
Expand Down
1 change: 1 addition & 0 deletions lib/util/state-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface OtpState {
type: string
}
}
location: any
overlay: any
serviceTimeRange?: {
end: number
Expand Down
Loading