-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
106 lines (80 loc) · 2.6 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
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
98
99
100
101
102
103
104
105
106
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': 'c599114a1emsh1142a01581c3d5ap1676dfjsndc89f719989b',
'X-RapidAPI-Host': 'live-crypto-prices.p.rapidapi.com'
}
};
let cryptos;
let query;
fetch('https://live-crypto-prices.p.rapidapi.com/pricefeed', options)
.then(response => response.json())
//.then((res) => console.log(res))
.then(cryptoRaw => cryptoRaw.result)
.then((cryptoRaw) => {
cryptos = cryptoRaw.map((crypto) => {
return {
Logo: crypto.Logo,
CoinName: crypto.CoinName,
Id: crypto.Id,
Price: crypto.Price,
Volume24h: crypto.Volume24h,
};
})
renderCryptoList(cryptos)
});
const filterCrypto = () => {
const filteredCrypto = cryptos.filter((crypto) => {
return (
crypto.CoinName.toLowerCase().includes(query)
);
});
renderCryptoList(filteredCrypto)
};
document.querySelector('#query').addEventListener('input', (e) => {
query = e.target.value.toLowerCase().trim();
console.log(query);
filterCrypto();
});
const createInfoElement = (labelName,value) => {
const infoElement = document.createElement('div');
const labelElement = document.createElement('strong');
const valueElement = document.createElement('span');
labelElement.innerText = labelName;
valueElement.innerText = value;
infoElement.appendChild(labelElement);
infoElement.appendChild(valueElement);
return infoElement;
}
const createIMG = (crypto) => {
const containerIMG = document.createElement('div');
const createImgCrypto = document.createElement('img');
createImgCrypto.src = crypto.Logo
containerIMG.appendChild(createImgCrypto);
createImgCrypto.width = 25;
createImgCrypto.height = 25;
return containerIMG;
}
const createCryptoItemElement = (x) => {
const cryptoElement = document.createElement('li');
const cryptoId = document.createElement('strong');
cryptoElement.appendChild(createIMG(x));
cryptoElement.appendChild(createInfoElement('Rank: ', x.Id))
cryptoElement.appendChild(createInfoElement('Name: ', x.CoinName))
cryptoElement.appendChild(createInfoElement('Price: ', x.Price))
cryptoElement.appendChild(createInfoElement('Volume 24h: ', x.Volume24h))
return cryptoElement;
}
const createListElement = (cryptos) => {
const listElement = document.createElement('ul');
cryptos.forEach((crypto) => {
listElement.appendChild(createCryptoItemElement(crypto))
});
return listElement;
}
const renderCryptoList = (x) => {
const rootElement = document.querySelector('#root');
rootElement.innerHTML = '';
rootElement.appendChild(createListElement(x));
};
console.log(Date);