Skip to content

Commit 0fae62d

Browse files
committed
v1.2 - fix empty cityName issue and refactor code
1 parent a8b1fce commit 0fae62d

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

src/containers/autocomplete/AutoCompleteContainer.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import LoaderComponent from '../../components/loader/LoaderComponent'
88
import ErrorComponent from '../../components/error/ErrorComponent'
99
import {AddressContext} from '../../context/AddressContext'
1010
import SearchComponent from '../../components/search/SearchComponent'
11-
import {isEmpty, isUndefined, isNull} from 'lodash-es'
11+
import {isEmpty, isUndefined} from 'lodash-es'
1212
import * as Sentry from '@sentry/browser'
1313
import {Event} from '../../utils/ReactAnalytics'
1414
import HEADERS from '../../utils/AlgoliaHeaders'
15+
import validName from '../../utils/ValidCityName'
1516

1617
// Exponential back-off retry delay between requests
1718
axiosRetry(axios, {retryDelay: axiosRetry.exponentialDelay})
@@ -31,12 +32,6 @@ class AutoCompleteContainer extends Component {
3132
this.setState({errorMessage: message})
3233
}
3334

34-
validName(name, showDelimeter = true) {
35-
return !isEmpty(name) && !isUndefined(name) && !isNull(name)
36-
? `${name}${showDelimeter ? ', ' : ''}`
37-
: ''
38-
}
39-
4035
// debounced function
4136
debounceAddress = debounce(this.getAddresses, 1000)
4237

@@ -83,7 +78,7 @@ class AutoCompleteContainer extends Component {
8378
}`
8479

8580
// prettier-ignore
86-
const cityName = `${this.validName(city)}${this.validName(state)}${this.validName(country, false)}`
81+
const cityName = `${validName(city)}${validName(state)}${validName(country, false)}`
8782
const {lat, lng} = hit['_geoloc']
8883
return {
8984
cityName: cityName,

src/context/AddressContext.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import axios from 'axios'
44
import * as Sentry from '@sentry/browser'
55
import {isEmpty, isUndefined} from 'lodash-es'
66
import HEADERS from '../utils/AlgoliaHeaders'
7+
import validName from './../utils/ValidCityName'
78

89
// const token = process.env.REACT_APP_IPINFO_TOKEN
910
const AddressContext = React.createContext(null)
@@ -45,9 +46,13 @@ class AddressContextProvider extends Component {
4546
hit = hits[0]
4647

4748
if (!isEmpty(hit) && !isUndefined(hit)) {
48-
const cityName = `${hit.city ? hit.city[0] : ''}, ${
49-
hit.administrative ? hit.administrative[0] : ''
50-
}, ${hit.country ? hit.country : ''}`
49+
const city = hit.city ? hit.city[0] : ''
50+
const state = hit.administrative ? hit.administrative[0] : ''
51+
const country = hit.country ? hit.country : ''
52+
const cityName = `${validName(city)}${validName(state)}${validName(
53+
country,
54+
false
55+
)}`
5156
const cityId = hit.objectID ? hit.objectID : ''
5257
this.updateState({
5358
address: {

src/utils/ValidCityName.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {isEmpty, isUndefined, isNull} from 'lodash-es'
2+
3+
/**
4+
* util func to check and return the valid name
5+
* @param {*} name (city or state or country)
6+
* @param {*} showDelimeter (false for country)
7+
*/
8+
const validName = (name, showDelimeter = true) => {
9+
return !isEmpty(name) && !isUndefined(name) && !isNull(name)
10+
? `${name}${showDelimeter ? ', ' : ''}`
11+
: ''
12+
}
13+
14+
export default validName

0 commit comments

Comments
 (0)