-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
93 lines (76 loc) · 2.35 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import './src/Sass/style.scss'
import { animation } from './Js/animation';
import IMask from 'imask';
animation();
const inputCep = document.querySelector(".input-cep");
const btnBuscar = document.querySelector(".search");
const inputs = document.querySelectorAll(".input-get input");
const inputMask = IMask(inputCep, {
mask: '00000-000'
})
btnBuscar.addEventListener("click", (event) => {
event.preventDefault();
console.log(inputCep.value);
if (inputCep.value !== "") {
buscarCEP(inputCep.value);
}
else {
inputCep.setAttribute("placeholder", "DIGITE UM CEP!");
}
})
inputCep.addEventListener("keypress", (event) => {
if (event.key === "Enter" && event.currentTarget.value != "") {
console.log(event.key);
event.preventDefault();
buscarCEP(inputCep.value);
}
if (event.key === "Enter" && event.currentTarget.value === "") {
event.preventDefault();
inputCep.setAttribute("placeholder", "DIGITE UM CEP!");
}
})
function getTopo(json) {
const cep = document.querySelector(".top p:first-child")
const estado = document.querySelector(".top p:last-child")
cep.innerText = `CEP: ${json.cep}`;
estado.innerText = json.localidade;
}
async function buscarCEP(cep) {
try {
const response = await fetch(`https://viacep.com.br/ws/${cep}/json/`);
console.log(response);
const data = await response.json();
if (data.erro) {
clearInput();
inputCep.setAttribute("placeholder", "NÃO ACHAMOS!");
}
else {
exibirDados(data);
clearInput();
inputCep.setAttribute("placeholder", "00000-000");
}
}
catch (error) {
console.log(error);
clearInput();
inputCep.setAttribute("placeholder", "NÃO ACHAMOS!");
}
}
function exibirDados(json) {
inputs.forEach((item) => {
item.value = json[item.getAttribute("id")].toLocaleUpperCase();
});
getTopo(json);
const endereco = json.cep
const mapa = document.querySelector(".mapa a");
mapa.href = `https://www.google.com/maps/search/${endereco}`
const card = document.querySelector(".card")
card.dataset.animation = "fadeIn";
card.scrollIntoView({
behavior: 'smooth',
block: 'center'
})
}
function clearInput() {
inputMask.value = "";
}