-
Notifications
You must be signed in to change notification settings - Fork 53
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: Add Location Field Search #1320
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
import { connect } from 'react-redux' | ||
Check failure on line 1 in lib/components/viewers/nearby/nearby-view.tsx GitHub Actions / test-build-release
Check failure on line 1 in lib/components/viewers/nearby/nearby-view.tsx GitHub Actions / test-build-release
|
||
import { FormattedMessage, useIntl } from 'react-intl' | ||
import { Location } from '@opentripplanner/types' | ||
import { MapRef, useMap } from 'react-map-gl' | ||
import { Search } from '@styled-icons/fa-solid' | ||
import getGeocoder from '@opentripplanner/geocoder' | ||
import LocationField from '@opentripplanner/location-field' | ||
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react' | ||
|
||
import * as apiActions from '../../../actions/api' | ||
import * as locationActions from '../../../actions/location' | ||
import * as mapActions from '../../../actions/map' | ||
import * as uiActions from '../../../actions/ui' | ||
import { AppReduxState } from '../../../util/state-types' | ||
import { GeocoderConfig } from '../../../util/config-types' | ||
import { getCurrentServiceWeek } from '../../../util/current-service-week' | ||
import { SetLocationHandler, ZoomToPlaceHandler } from '../../util/types' | ||
import Loading from '../../narrative/loading' | ||
|
@@ -45,6 +50,8 @@ | |
radius?: number, | ||
currentServiceWeek?: ServiceWeek | ||
) => void | ||
geocoderConfig: GeocoderConfig | ||
getCurrentPosition: TODO | ||
hideBackButton?: boolean | ||
location: string | ||
mobile?: boolean | ||
|
@@ -114,6 +121,8 @@ | |
displayedCoords, | ||
entityId, | ||
fetchNearby, | ||
geocoderConfig, | ||
getCurrentPosition, | ||
location, | ||
mobile, | ||
nearby, | ||
|
@@ -127,6 +136,7 @@ | |
const map = useMap().default | ||
const intl = useIntl() | ||
const [loading, setLoading] = useState(true) | ||
const [reversedPoint, setReversedPoint] = useState('') | ||
const firstItemRef = useRef<HTMLDivElement>(null) | ||
const finalNearbyCoords = useMemo( | ||
() => | ||
|
@@ -139,6 +149,12 @@ | |
[nearbyViewCoords, currentPosition, map] | ||
) | ||
|
||
const reverseCoords = (coords) => { | ||
getGeocoder(geocoderConfig) | ||
.reverse({ point: coords }) | ||
.then((result) => setReversedPoint(result.name)) | ||
} | ||
|
||
// Make sure the highlighted location is cleaned up when leaving nearby | ||
useEffect(() => { | ||
return function cleanup() { | ||
|
@@ -149,10 +165,12 @@ | |
useEffect(() => { | ||
const moveListener = (e: mapboxgl.EventData) => { | ||
if (e.geolocateSource) { | ||
setViewedNearbyCoords({ | ||
const coords = { | ||
lat: e.viewState.latitude, | ||
lon: e.viewState.longitude | ||
}) | ||
} | ||
setViewedNearbyCoords(coords) | ||
reverseCoords(coords) | ||
} | ||
} | ||
|
||
|
@@ -162,9 +180,11 @@ | |
lon: e.viewState.longitude | ||
} | ||
setViewedNearbyCoords(coords) | ||
reverseCoords(coords) | ||
|
||
// Briefly flash the highlight to alert the user that we've moved | ||
setHighlightedLocation(coords) | ||
|
||
setTimeout(() => { | ||
setHighlightedLocation(null) | ||
}, 500) | ||
|
@@ -286,6 +306,32 @@ | |
> | ||
{/* This is used to scroll to top */} | ||
<div aria-hidden ref={firstItemRef} /> | ||
<LocationField | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you're missing a |
||
className="nearby-view-location-field" | ||
geocoderConfig={geocoderConfig} | ||
getCurrentPosition={getCurrentPosition} | ||
inputPlaceholder={intl.formatMessage({ | ||
id: 'components.NearbyView.searchNearby' | ||
})} | ||
location={{ | ||
// Provide a 0 default in case the nearby view coords are null | ||
lat: 0, | ||
lon: 0, | ||
...nearbyViewCoords, | ||
name: reversedPoint | ||
}} | ||
LocationIconComponent={() => ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
<Search style={{ marginRight: 5, padding: 5 }} /> | ||
)} | ||
locationType="to" | ||
onLocationSelected={(selection) => { | ||
const { location } = selection | ||
setViewedNearbyCoords(location) | ||
map && zoomToPlace(map, location) | ||
setReversedPoint(location.name || '') | ||
}} | ||
sortByDistance | ||
/> | ||
{loading && ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
<FloatingLoadingIndicator> | ||
<Loading extraSmall /> | ||
|
@@ -327,6 +373,7 @@ | |
defaultLatLon, | ||
displayedCoords: nearby?.coords, | ||
entityId: entityId && decodeURIComponent(entityId), | ||
geocoderConfig: config.geocoder, | ||
homeTimezone: config.homeTimezone, | ||
location: state.router.location.hash, | ||
nearby: nearby?.data, | ||
|
@@ -337,6 +384,7 @@ | |
|
||
const mapDispatchToProps = { | ||
fetchNearby: apiActions.fetchNearby, | ||
getCurrentPosition: locationActions.getCurrentPosition, | ||
setHighlightedLocation: uiActions.setHighlightedLocation, | ||
setLocation: mapActions.setLocation, | ||
setMainPanelContent: uiActions.setMainPanelContent, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a
useEffect
that checks if thecurrentPosition
coords are the same asfinalNearbyCoords
and then runsreverseCoords
? Right now if I use current location, there's no feedback that my location was picked up correctly