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: Add Location Field Search #1320

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions i18n/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ components:
headsign: "{destination}"
nearbyListIntro: List of {count} nearby entities.
nothingNearby: There are no places nearby.
searchNearby: Search nearby...
spacesAvailable: "{spacesAvailable} empty spaces available"
NewAccountWizard:
createNewAccount: Create a new account
Expand Down
52 changes: 50 additions & 2 deletions lib/components/viewers/nearby/nearby-view.tsx
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

View workflow job for this annotation

GitHub Actions / test-build-release

Cannot find name 'TODO'.

Check failure on line 1 in lib/components/viewers/nearby/nearby-view.tsx

View workflow job for this annotation

GitHub Actions / test-build-release

Parameter 'coords' implicitly has an 'any' type.

Check failure on line 1 in lib/components/viewers/nearby/nearby-view.tsx

View workflow job for this annotation

GitHub Actions / test-build-release

Parameter 'result' implicitly has an 'any' type.
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'
Expand Down Expand Up @@ -45,6 +50,8 @@
radius?: number,
currentServiceWeek?: ServiceWeek
) => void
geocoderConfig: GeocoderConfig
getCurrentPosition: TODO
hideBackButton?: boolean
location: string
mobile?: boolean
Expand Down Expand Up @@ -114,6 +121,8 @@
displayedCoords,
entityId,
fetchNearby,
geocoderConfig,
getCurrentPosition,
location,
mobile,
nearby,
Expand All @@ -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(
() =>
Expand All @@ -139,6 +149,12 @@
[nearbyViewCoords, currentPosition, map]
)

const reverseCoords = (coords) => {
Copy link
Contributor

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 the currentPosition coords are the same as finalNearbyCoords and then runs reverseCoords? Right now if I use current location, there's no feedback that my location was picked up correctly

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() {
Expand All @@ -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)
}
}

Expand All @@ -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)
Expand Down Expand Up @@ -286,6 +306,32 @@
>
{/* This is used to scroll to top */}
<div aria-hidden ref={firstItemRef} />
<LocationField
Copy link
Contributor

@amy-corson-ibigroup amy-corson-ibigroup Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're missing a currentPosition prop here that's making it impossible to use current location

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={() => (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm this isn't showing up on chrome or safari
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, when it does show up it feels v close to the placeholder text? Probably better suited to a OTP-UI PR, but I guess just flagging it here

image

<Search style={{ marginRight: 5, padding: 5 }} />
)}
locationType="to"
onLocationSelected={(selection) => {
const { location } = selection
setViewedNearbyCoords(location)
map && zoomToPlace(map, location)
setReversedPoint(location.name || '')
}}
sortByDistance
/>
{loading && (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loading spinner is showing up on the location field which is a little misleading? Could we position it down to the results container

image

<FloatingLoadingIndicator>
<Loading extraSmall />
Expand Down Expand Up @@ -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,
Expand All @@ -337,6 +384,7 @@

const mapDispatchToProps = {
fetchNearby: apiActions.fetchNearby,
getCurrentPosition: locationActions.getCurrentPosition,
setHighlightedLocation: uiActions.setHighlightedLocation,
setLocation: mapActions.setLocation,
setMainPanelContent: uiActions.setMainPanelContent,
Expand Down
21 changes: 21 additions & 0 deletions lib/components/viewers/viewers.css
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,24 @@
justify-content: center;
padding-top: 10px;
}

/* Nearby View Location Field Styles */
.nearby-view-location-field {
background-color: rgba(255, 255, 255, 0.85);
transition: background-color 0.1s ease-out;
border-radius: 10px;
height: 50px;
color: #111;
display: flex !important;
border: none !important;
box-shadow: 0px 0px 5px 1px inset rgb(0 0 0 / 0.05);
}
.nearby-view-location-field:hover,
.nearby-view-location-field:active {
background-color: rgba(255, 255, 255, 0.95);
box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
}
.nearby-view-location-field input {
width: 100% !important;
background: transparent !important;
}
Loading