Improved autocomplete error handling

This commit is contained in:
vjrj 2018-03-01 16:47:37 +01:00
parent a83495deea
commit 621cf4e2ad
3 changed files with 49 additions and 18 deletions

View file

@ -4,30 +4,55 @@ import PropTypes from 'prop-types';
import { translate } from 'react-i18next'; import { translate } from 'react-i18next';
import PlacesAutocomplete, { geocodeByAddress, getLatLng } from 'react-places-autocomplete'; import PlacesAutocomplete, { geocodeByAddress, getLatLng } from 'react-places-autocomplete';
import { FormGroup, ControlLabel, HelpBlock } from 'react-bootstrap'; import { FormGroup, ControlLabel, HelpBlock } from 'react-bootstrap';
import { Bert } from 'meteor/themeteorchef:bert';
// Sample:
// https://github.com/kenny-hibino/react-places-autocomplete/blob/master/demo/components/SearchBar.js
class LocationAutocomplete extends React.Component { class LocationAutocomplete extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
address: '' address: '',
loading: false // we can use it with a spinner
}; };
this.handleSelect = this.handleSelect.bind(this);
this.handleChange = this.handleChange.bind(this);
this.onError = this.onError.bind(this);
} }
onChange(address) { this.setState({ address }); } onError(error) {
if (error === 'ZERO_RESULTS') {
Bert.alert(this.props.t('Lugar no encontrado'), 'alert');
}
}
handleChange(address) {
this.setState({ address });
}
handleSelect(address) { handleSelect(address) {
this.setState({
address,
loading: true
});
const self = this; const self = this;
geocodeByAddress(address) try {
.then(results => getLatLng(results[0])) geocodeByAddress(address)
.then(({ lat, lng }) => { .then(results => getLatLng(results[0]))
self.props.onChange({ lat, lng }); .then((result) => {
}) if (result && result.lat && result.lng) {
.catch((error) => { const { lat, lng } = result;
console.log(error); self.props.onChange({ lat, lng });
if (error === 'ZERO_RESULTS') { self.setState({
console.log('No results'); loading: false
} });
}); }
})
.catch((error) => { self.onError(error); });
} catch (error) {
self.onError(error);
}
} }
render() { render() {
@ -69,8 +94,10 @@ class LocationAutocomplete extends React.Component {
}} }}
googleLogo={false} googleLogo={false}
highlightFirstSuggestion highlightFirstSuggestion
onSelect={address => this.handleSelect(address)} onChange={this.handleChange}
onEnterKeyDown={address => this.handleSelect(address)} onSelect={this.handleSelect}
onEnterKeyDown={this.handleSelect}
onError={this.onError}
options={{ options={{
// location: new google.maps.LatLng(-34, 151), // location: new google.maps.LatLng(-34, 151),
// radius: 2000, // radius: 2000,
@ -80,7 +107,7 @@ class LocationAutocomplete extends React.Component {
inputProps={ inputProps={
{ {
value: this.state.address, value: this.state.address,
onChange: address => this.onChange(address), onChange: this.handleChange,
placeholder: t(placeHolder), placeholder: t(placeHolder),
onBlur: () => { /* console.log('Blur event!'); */ }, onBlur: () => { /* console.log('Blur event!'); */ },
onFocus: () => { /* console.log('Focused!'); */ }, onFocus: () => { /* console.log('Focused!'); */ },

View file

@ -230,5 +230,7 @@
"geo-not-avail-error": "Upppps: it seems your location is not available", "geo-not-avail-error": "Upppps: it seems your location is not available",
"geo-not-timeout-error": "Upppps: Your device is taking too long to know your location", "geo-not-timeout-error": "Upppps: Your device is taking too long to know your location",
"Estás subscrito a una zona muy grande": "Estás subscrito a una zona muy grande":
"You are subscribed to a very large area" "You are subscribed to a very large area",
"Lugar no encontrado":
"Place not found"
} }

View file

@ -317,5 +317,7 @@
"geo-not-avail-error": "Upppps: parece tu ubicación no está disponible", "geo-not-avail-error": "Upppps: parece tu ubicación no está disponible",
"geo-not-timeout-error": "Upppps: tu dispositivo está tardando demasiado en conocer tu ubicación", "geo-not-timeout-error": "Upppps: tu dispositivo está tardando demasiado en conocer tu ubicación",
"Estás subscrito a una zona muy grande": "Estás subscrito a una zona muy grande":
"Estás subscrito a una zona muy grande" "Estás subscrito a una zona muy grande",
"Lugar no encontrado":
"Lugar no encontrado"
} }