Skip to content

Commit

Permalink
refactor(location): use otp-ui location-search component
Browse files Browse the repository at this point in the history
  • Loading branch information
evansiroky committed Mar 1, 2020
1 parent c84de22 commit 4f6f4da
Show file tree
Hide file tree
Showing 10 changed files with 682 additions and 634 deletions.
13 changes: 13 additions & 0 deletions lib/actions/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ export function clearLocation (payload) {
}
}

/**
* Handler for @opentripplanner/location-field onLocationSelected
*/
export function onLocationSelected ({ locationType, location, resultType }) {
return function (dispatch, getState) {
if (resultType === 'CURRENT_LOCATION') {
dispatch(setLocationToCurrent({ type: locationType }))
} else {
dispatch(setLocation({ location, type: locationType }))
}
}
}

export function setLocation (payload) {
return function (dispatch, getState) {
const otpState = getState().otp
Expand Down
76 changes: 76 additions & 0 deletions lib/components/form/connected-location-field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import LocationField from '@opentripplanner/location-field'
import {
DropdownContainer,
FormGroup,
Input,
InputGroup,
InputGroupAddon
} from '@opentripplanner/location-field/lib/styled'
import { connect } from 'react-redux'
import styled from 'styled-components'

import { clearLocation, onLocationSelected } from '../../actions/map'
import { addLocationSearch, getCurrentPosition } from '../../actions/location'
import { findNearbyStops } from '../../actions/api'
import { getActiveSearch, getShowUserSettings } from '../../util/state'

const StyledLocationField = styled(LocationField)`
width: 100%;
${DropdownContainer} {
display: table-cell;
vertical-align: middle;
width: 1%;
}
${FormGroup} {
display: table;
padding: 6px 12px;
width: 100%;
}
${Input} {
display: table-cell;
padding: 6px 12px;
width: 100%;
}
${InputGroup} {
width: 100%;
}
${InputGroupAddon} {
display: table-cell;
vertical-align: middle;
width: 1%;
}
`

// connect to redux store

const mapStateToProps = (state, ownProps) => {
const { config, currentQuery, location, transitIndex, user } = state.otp
const { currentPosition, nearbyStops, sessionSearches } = location
const activeSearch = getActiveSearch(state.otp)
const query = activeSearch ? activeSearch.query : currentQuery
return {
currentPosition,
geocoderConfig: config.geocoder,
location: query[ownProps.locationType],
nearbyStops,
sessionSearches,
showUserSettings: getShowUserSettings(state.otp),
stopsIndex: transitIndex.stops,
userLocationsAndRecentPlaces: [...user.locations, ...user.recentPlaces]
}
}

const mapDispatchToProps = {
addLocationSearch,
findNearbyStops,
getCurrentPosition,
onLocationSelected,
clearLocation
}

export default connect(mapStateToProps, mapDispatchToProps)(StyledLocationField)
10 changes: 5 additions & 5 deletions lib/components/form/default-search-form.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'

import LocationField from './location-field'
import LocationField from './connected-location-field'
import SwitchButton from './switch-button'
import TabbedFormPanel from './tabbed-form-panel'
import defaultIcons from '../icons'
Expand Down Expand Up @@ -34,14 +34,14 @@ export default class DefaultSearchForm extends Component {
<div>
<div className='locations'>
<LocationField
type='from'
label={`Enter start location or ${actionText} on map...`}
inputPlaceholder={`Enter start location or ${actionText} on map...`}
locationType='from'
showClearButton
/>

<LocationField
type='to'
label={`Enter destination or ${actionText} on map...`}
inputPlaceholder={`Enter destination or ${actionText} on map...`}
locationType='to'
showClearButton={!mobile}
/>

Expand Down
Loading

0 comments on commit 4f6f4da

Please sign in to comment.