-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
97 lines (71 loc) · 2.48 KB
/
script.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
89
90
91
92
93
94
95
96
97
const api = 'https://api.coingecko.com/api/v3/exchange_rates'
const input = document.getElementById("searchValue")
const select = document.getElementById('searchItens')
const buttonSearch = document.getElementById('searchName')
const nameCripto = document.getElementById('nameCripto')
const simboloCripto = document.getElementById('simboloCripto')
const unit = document.getElementById('unit')
const price = document.getElementById('value')
async function rates(){
const resolve = await fetch(api)
const data = await resolve.json()
const rates = data.rates
const precoBTC = rates.brl.value
const criptos = Object.keys(rates).map((key) => {
return {
name: `${rates[key].name} (${key.toUpperCase()})`,
value: (precoBTC / rates[key].value).toFixed(6),
unit: rates[key].unit
}
})
return criptos
}
function limparFilhos(elemento){
while(elemento.firstChild){
elemento.removeChild(elemento.firstChild)
}
}
async function searchName (value) {
let fail = true
const criptos = await rates()
// limpa o select
limparFilhos(select)
criptos.forEach((valor, index, array) => {
if(criptos[index].name.toUpperCase().includes(value.toUpperCase())){
let option = document.createElement('option')
option.append(criptos[index].name)
select.append(option)
//console.log(criptos[index].name)
fail = false
}
})
if(fail){
let option = document.createElement('option')
limparFilhos(select)
option.append("Moeda não encontrada")
select.append(option)
}
}
async function searchCripto (name) {
const criptos = await rates()
const cripto = criptos.filter((valor, index, array) => {
return criptos[index].name == name
})
const nome = cripto[0].name.split(' (')
nameCripto.innerHTML = nome[0] + " "
simboloCripto.textContent = "(" + nome[1]
unit.innerHTML = cripto[0].unit
price.textContent = cripto[0].value
document.title = `Cripto Price - ${cripto[0].name}`
}
input.addEventListener("input", () => {
searchName(input.value)
})
buttonSearch.addEventListener('click', () => {
searchCripto(select.value)
})
input.addEventListener("keydown", (event) => {
if(event.key === "Enter"){
searchCripto(select.value)
}
})