Skip to content

Commit

Permalink
[SDESK-1925] Split location field into location name and address
Browse files Browse the repository at this point in the history
test case fix

Address schema fix in events
  • Loading branch information
nrvikas committed Sep 15, 2017
1 parent 453f634 commit 547f0f5
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 60 deletions.
10 changes: 6 additions & 4 deletions client/actions/locations.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ import { get } from 'lodash'

export function saveNominatim(nominatim) {
return (dispatch, getState, { api }) => {
const { shortName, address } = formatAddress(nominatim)
const { address } = formatAddress(nominatim)
return api('locations').save({}, {
unique_name: nominatim.display_name,
name: shortName,
name: nominatim.namedetails.name,
address: address,
position: {
latitude: nominatim.lat,
longitude: nominatim.lon,
},
type: nominatim.type,
})
}
}
Expand Down Expand Up @@ -50,7 +49,6 @@ export function saveLocation(newLocation) {
.then(data => {
const eventData = {
name: data.name,
type: data.type,
qcode: data.guid,
}

Expand All @@ -61,6 +59,10 @@ export function saveLocation(newLocation) {
}
}

if (get(data, 'address.external.nominatim.address')) {
eventData.address = data.address.external.nominatim.address
}

return eventData
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ export class AddGeoLookupResultsPopUp extends React.Component {
const shortName = formatAddress(suggest.raw).shortName
return (<li key={index} className='addgeolookup__item'
onClick={this.props.onChange.bind(null, suggest)}>
<span><span className='label addgeolookup__suggestItemLabel'>{suggest.raw.type.replace('_', ' ')}</span>
&nbsp;&nbsp;{shortName}</span>
<span>&nbsp;&nbsp;{shortName}</span>
</li>)
})}
</ul>
Expand Down
34 changes: 31 additions & 3 deletions client/components/AddGeoLookupInput/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,42 @@ class GeoLookupInput extends React.Component {
{ disabledInput: this.props.readOnly }
)


let locationName = locationName = get(this.props.initialValue, 'name')
let formattedAddress = ''
let displayText = locationName

if (get(this.props.initialValue, 'address')) {
// Location from local locations collection in database
formattedAddress = formatAddress(this.props.initialValue).formattedAddress
displayText = displayText + '\n' + formattedAddress
} else if (get(this.props.initialValue, 'nominatim.address')) {
// Location select from lookup suggests
locationName = get(this.props.initialValue.nominatim, 'namedetails.name')
formattedAddress = formatAddress(this.props.initialValue.nominatim).formattedAddress
displayText = locationName + '\n' + formattedAddress
}

return (
<div className='addgeolookup sd-line-input__input'>
<span className='addgeolookup__input-wrapper'><TextareaAutosize
{this.props.readOnly &&
<span className='addgeolookup__input-wrapper'>
{locationName}
<span style={{
'font-style': 'italic',
'font-size': 'small',
}}>
<br />
{formattedAddress}
</span>
</span>
}
{!this.props.readOnly && <span className='addgeolookup__input-wrapper'><TextareaAutosize
className={textAreaClassNames}
disabled={this.props.readOnly ? 'disabled' : ''}
value={get(this.props.initialValue, 'name')}
value={displayText}
onChange={this.handleInputChange.bind(this)} />
</span>
</span>}
{!this.props.readOnly &&
<span><button type='button' className='btn' disabled={this.state.searching}
onClick={this.handleSearchClick.bind(this)} >
Expand Down
3 changes: 0 additions & 3 deletions client/components/fields/GeoLookupInput.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AddGeoLookupInput } from '../index'
import classNames from 'classnames'
import React from 'react'
import { get } from 'lodash'

export const GeoLookupInput = ({ input, label, readOnly, meta: { touched, error, warning } }) => {

Expand All @@ -12,11 +11,9 @@ export const GeoLookupInput = ({ input, label, readOnly, meta: { touched, error,
{ 'sd-line-input--no-margin': !showMessage }
)

const type = input.value && (input.value.type || get(input.value.nominatim, 'type') || null)
return (<div className={divClass}>
{label && <label className="sd-line-input__label">{label}</label>}

{type && <span className='label addgeolookup__suggestItemLabel'>{type.replace('_', ' ')}</span>}
<AddGeoLookupInput
onChange={input.onChange}
initialValue={input.value || {}}
Expand Down
41 changes: 0 additions & 41 deletions client/components/fields/tests/GeoLookupInput_test.jsx

This file was deleted.

5 changes: 3 additions & 2 deletions client/components/tests/AddGeoLookupInput_test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ const nominatimLocation = {
postcode: 'postcode',
country: 'country',
},
namedetails: { name: 'Location' },
},
}

const formattedLocation = {
unique_name: 'display location',
name: '123 road, town, state, postcode, country',
name: 'Location',
address: {
line: ['123 road'],
locality: 'state',
Expand Down Expand Up @@ -76,7 +77,6 @@ describe('<AddGeoLookupInput />', () => {
expect(savedLocation).toEqual({
qcode: 'location-guid',
name: '123 road, town, state, postcode, country',
type: 'residential',
})
})
})
Expand Down Expand Up @@ -105,6 +105,7 @@ describe('<AddGeoLookupInput />', () => {
country: 'France',
country_code: 'fr',
},
namedetails: { name: 'Paris, France' },
})
action(dispatch, getState, { api })
})
Expand Down
8 changes: 6 additions & 2 deletions client/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,17 +499,21 @@ export const formatAddress = (nominatim) => {
postal_code: nominatim.address.postcode,
external: { nominatim },
}
const shortName = [
get(address, 'title'),

const formattedAddress = [
get(address, 'line[0]'),
get(address, 'area'),
get(address, 'locality'),
get(address, 'postal_code'),
get(address, 'country'),
].filter(d => d).join(', ')

const shortName = get(address, 'title') ? get(address, 'title') + ', ' + formattedAddress :
formattedAddress

return {
address,
formattedAddress,
shortName,
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/planning/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,8 @@ def _is_only_time_updated(self, original_dates, updated_dates):
'properties': {
'qcode': {'type': 'string'},
'name': {'type': 'string'},
'address': {'type': 'object'},
'geo': {'type': 'string'},
'type': {'type': 'string'},
'location': {'type': 'geo_point'},
}
}
Expand Down
3 changes: 1 addition & 2 deletions server/planning/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ def on_create(self, docs):
# the field where we store the formatted address, which can have variations (street number vs number street)
# for the same address
'name': {
'type': 'string',
'unique': True,
'type': 'string'
},
'type': {
'type': 'string',
Expand Down

0 comments on commit 547f0f5

Please sign in to comment.