-2
Hello. I am trying to create a form with four selects (Animal, Size, State and City) the state and city selects that are being a problem. One makes a request to present the Brazilian states (is working normally) and the other select will display the cities of the selected state, but I can not continue this part, I could not pass the id dynamically in the URL of the municipalities. Instead of passing only a selected id is going to the entire array.
Follow the code below as far as I can:
const FormFinderPets = () => {
const [species, setSpecies] = useState('');
const [port, setPort] = useState('');
const [UFs, setUFs] = useState('');
const [cities, setCities] = useState('');
const [listUFs, setListUFs] = useState([]);
const [listCities, setListCities] = useState([]);
useEffect(() => {
const populatedStates = axios.get('https://servicodados.ibge.gov.br/api/v1/localidades/estados');
populatedStates.then((res) => {
setListUFs(res.data);
const stateId = res.data.map(dataid => dataid.id);
console.log(stateId);
const populatedCities = axios.get(`https://servicodados.ibge.gov.br/api/v1/localidades/estados/${stateId}/municipios`);
populatedCities.then((res) => {
setListCities(res.data);
console.log(res.data);
})
})
}, []);
const optionsSpecies = [
{ value: '', name: 'Selecione uma espécie' },
{ value: 1, name: 'Cachorro'},
{ value: 2, name: 'Gato' },
{ value: 3, name: 'Outro' }
];
const optionsPort = [
{ value: '', name: 'Selecione o porte do animal' },
{ value: 1, name: 'Pequeno'},
{ value: 2, name: 'Médio' },
{ value: 2, name: 'Grande' },
];
return (
<form>
<div>
<fieldset>
<Select
label="Espécie"
name="species"
options={optionsSpecies}
value={species}
onChange={(event) => setSpecies(event.target.value)}
/>
</fieldset>
<fieldset>
<Select
label="Porte do animal"
name="port"
options={optionsPort}
value={port}
onChange={event => setPort(event.target.value)}
/>
</fieldset>
<fieldset>
<label htmlFor="states">Estado</label>
<select value={UFs} onChange={event => {setUFs(event.target.value); console.log(event.target.selectedIndex)}} name="states">
<option value="">Selecione um estado</option>
{listUFs.map(listUF => (
<option key={listUF.id}>
{listUF.nome}
</option>
))}
</select>
</fieldset>
<fieldset>
<label htmlFor="states">Cidade</label>
<select
value={cities}
onChange={event => setCities(event.target.value)}
name="states">
<option value="">Selecione uma cidade</option>
{listCities.map(listCity => (
<option key={listCity.id}>
{listCity.nome}
</option>
))}
</select>
</fieldset>
</div>
<button type="submit">Procurar</button>
</form>
);
};