-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
66 lines (50 loc) · 1.4 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { useState} from 'react'
import { FiSearch } from 'react-icons/fi';
import './styles.css';
//viacep.com.br/ws/${input}/jasons/
import api from './services/api.js';
function App() {
const [input, setInput] = useState('');
const [cep, setCep] = useState({});
async function handleSearch(){
if(input === '') {
alert('Preencha algum CEP !')
return;
}
try{
const response = await api.get(`${input}/json/`);
setCep(response.data)
setInput("");
}catch{
alert("ops error ao buscar");
setInput("")
}
}
return (
<div className="container">
<h1 className="title">Buscador CEP</h1>
<div className="containerInput">
<input
type="text"
placeholder="Digite seu cep..."
maxLength={8}
value={input}
onChange={(event) => setInput(event.target.value) }
/>
<button id = "btn" className="buttonSearch" onClick={handleSearch}>
<FiSearch size={25} color="FFF"/>
</button>
</div>
{Object.keys(cep).length > 0 && (
<main className="main">
<h2>CEP: {cep.cep}</h2>
<span>Rua: {cep.logradouro}</span>
<span>Complemento: {cep.complemento}</span>
<span>Bairro: {cep.bairro}</span>
<span>Cidade: {cep. localidade} - {cep.uf}</span>
</main>
)}
</div>
);
}
export default App;