-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
71 lines (49 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
67
68
69
70
71
import { useState } from 'react';
import {FiSearch} from 'react-icons/fi';
import './styles.css';
import api from './services/api';
function App() {
const [input, setInput] = useState ('')
const [cep, setCep] = useState({});
async function handleSearch(){
// 21220310/json/
if (input === ''){
alert("Digita um CEP válido aí por favor meu consagrado 🗿🍷")
return;
}
try{
const response = await api.get(`${input}/json`);
setCep(response.data)
setInput("");
}catch{
alert("Ops!! Algo deu Errado😕 Parece que você digitou um CEP inválido...");
setInput("")
}
}
return (
<div className="container">
<h1 className="title">Buscador de CEP</h1>
<div className="containerInput">
<input
type="text"
placeholder="Digite seu Cep..."
value={input}
onChange={(e) => setInput(e.target.value) }
/>
<button 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> {cep.logradouro}</span>
<span>Complemento: {cep.Complemento}</span>
<span> {cep.bairro}</span>
<span>{cep.localidade} - {cep.uf}</span>
</main>
)}
</div>
);
}
export default App;