Skip to content

Commit

Permalink
feat(nearby view): add location field
Browse files Browse the repository at this point in the history
  • Loading branch information
miles-grant-ibigroup committed Dec 9, 2024
1 parent 5caf0b8 commit 57fefd8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
1 change: 1 addition & 0 deletions i18n/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,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
Expand Up @@ -2,12 +2,17 @@ import { connect } from 'react-redux'
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 @@ type Props = {
radius?: number,
currentServiceWeek?: ServiceWeek
) => void
geocoderConfig: GeocoderConfig
getCurrentPosition: TODO
hideBackButton?: boolean
location: string
mobile?: boolean
Expand Down Expand Up @@ -114,6 +121,8 @@ function NearbyView({
displayedCoords,
entityId,
fetchNearby,
geocoderConfig,
getCurrentPosition,
location,
mobile,
nearby,
Expand All @@ -127,6 +136,7 @@ function NearbyView({
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 @@ function NearbyView({
[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() {
Expand All @@ -149,10 +165,12 @@ function NearbyView({
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 @@ function NearbyView({
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 @@ function NearbyView({
>
{/* This is used to scroll to top */}
<div aria-hidden ref={firstItemRef} />
<LocationField
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={() => (
<Search style={{ marginRight: 5, padding: 5 }} />
)}
locationType="to"
onLocationSelected={(selection) => {
const { location } = selection
setViewedNearbyCoords(location)
map && zoomToPlace(map, location)
setReversedPoint(location.name || '')
}}
sortByDistance
/>
{loading && (
<FloatingLoadingIndicator>
<Loading extraSmall />
Expand Down Expand Up @@ -327,6 +373,7 @@ const mapStateToProps = (state: AppReduxState) => {
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 mapStateToProps = (state: AppReduxState) => {

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;
}

0 comments on commit 57fefd8

Please sign in to comment.