todos-contra-el-fuego-web/imports/ui/components/LocationAutocomplete/LocationAutocomplete.js
vjrj 12bdbe8fed deps: i18next 10 -> 23, react-i18next 7 -> 14
Modernizes the whole i18n stack:
- 48 translate([], {wait:true}) / translate() HOCs -> withTranslation()
- react.wait:true -> react.useSuspense:false
- whitelist -> supportedLngs; added compatibilityJSON: 'v3' so our
  natural-language keys + v3 _plural layout keep working (custom
  separators ss/eth/dj preserved)
- backends: xhr -> i18next-http-backend (client), sync-fs ->
  i18next-fs-backend (server); dropped i18next-localstorage-cache
  (was enabled:false); languagedetector 2 -> 8
- ReSendEmail: <Interpolate> (removed in v10) -> <Trans values={{email}}/>,
  dropped the removed bare t export
- saveMissing handler signature (lngs, ns, key, fallbackValue)

Silences the per-component Translate/I18n legacy-context warnings.
Browser-verified es/en switch and <Trans> interpolation on /fires;
REST smoke byte-identical.
2026-07-18 06:28:41 +02:00

133 lines
4.2 KiB
JavaScript

/* eslint-disable react/jsx-indent-props */
import React from 'react';
import PropTypes from 'prop-types';
import { withTranslation } from 'react-i18next';
import PlacesAutocomplete, { geocodeByAddress, getLatLng } from 'react-places-autocomplete';
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 {
constructor(props) {
super(props);
this.state = {
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);
}
onError(error) {
if (error === 'ZERO_RESULTS') {
Bert.alert(this.props.t('Lugar no encontrado'), 'alert');
}
}
handleChange(address) {
this.setState({ address });
}
handleSelect(address) {
this.setState({
address,
loading: true
});
const self = this;
try {
geocodeByAddress(address)
.then(results => getLatLng(results[0]))
.then((result) => {
if (result && result.lat && result.lng) {
const { lat, lng } = result;
self.props.onChange({ lat, lng });
self.setState({
loading: false
});
}
})
.catch((error) => { self.onError(error); });
} catch (error) {
self.onError(error);
}
}
render() {
// https://www.npmjs.com/package/react-places-autocomplete
// https://github.com/kenny-hibino/react-places-autocomplete/issues/103
const myStyles = {
autocompleteContainer: {
paddingBottom: '20px',
backgroundSize: 'auto 12px',
backgroundPosition: 'bottom left 10px',
backgroundRepeat: 'no-repeat',
backgroundImage: "url('https://maps.gstatic.com/mapfiles/api-3/images/powered-by-google-on-white3_hdpi.png')"
}
};
// http://simplelineicons.com/
const AutocompleteItem = ({ formattedSuggestion }) => (
<div className="suggestion-item">
<i className="icons icon-location-pin suggestion-icon" />{' '}
<strong>{formattedSuggestion.mainText}</strong>{' '}
<small className="text-muted">{formattedSuggestion.secondaryText}</small>
</div>);
const {
t, label, placeHolder, helpText
} = this.props;
return (
<form>
<FormGroup>
{ label.length > 0 && <ControlLabel>{t(label)}</ControlLabel> }
<PlacesAutocomplete
styles={myStyles}
autocompleteItem={AutocompleteItem}
classNames={{
root: 'form-group',
input: 'form-control',
autocompleteContainer: 'autocomplete-container'
}}
googleLogo={false}
highlightFirstSuggestion
onChange={this.handleChange}
onSelect={this.handleSelect}
onEnterKeyDown={this.handleSelect}
onError={this.onError}
options={{
// location: new google.maps.LatLng(-34, 151),
// radius: 2000,
// type: ['address'],
language: this.props.i18n.language
}}
inputProps={
{
value: this.state.address,
onChange: this.handleChange,
placeholder: t(placeHolder),
onBlur: () => { /* console.log('Blur event!'); */ },
onFocus: () => { /* console.log('Focused!'); */ },
autoFocus: this.props.focusInput
}}
/>
{ helpText.length > 0 && <HelpBlock>{t(helpText)}</HelpBlock> }
</FormGroup>
</form>
);
}
}
LocationAutocomplete.propTypes = {
focusInput: PropTypes.bool.isRequired,
t: PropTypes.func.isRequired,
label: PropTypes.string.isRequired,
placeHolder: PropTypes.string.isRequired,
helpText: PropTypes.string.isRequired,
i18n: PropTypes.object.isRequired
};
export default withTranslation()(LocationAutocomplete);