-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (119 loc) · 4.14 KB
/
index.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const id = document.querySelector("#blank");
const searchButton = document.querySelector("#searchbutton");
const cryptoInput = document.querySelector("#from-crypto");
const currencyOutput = document.querySelector("#to-fiat");
const amount = document.querySelector("#amount");
const convertButton = document.querySelector("#convert");
const resultsContainer = document.querySelector('#result-container');
const submit = document.querySelector("#submit");
function removeCrypto() {
const removeCurrent = document.querySelector("#cryptocontainer");
while (removeCurrent.lastChild) {
removeCurrent.removeChild (removeCurrent.lastChild);
}
}
// removes convert search
function removeConverted() {
const removeContainer = document.querySelector("#convertcontainer");
while (removeContainer.lastChild) {
removeContainer.removeChild (removeContainer.lastChild);
}
}
//gets list of all crypto
const allCrypto = async () => {
const url = `https://api.coincap.io/v2/assets/`
try {
const result = await axios.get(url)
let cryptoNames = result.data.data;
let names = cryptoNames.map(item => {
return item.name
})
id.addEventListener("input", (event => filterAndAppendResults(event, names)));
} catch(error) {
console.error(error);
}
}
const removeResults = () => {
resultsContainer.innerHTML = "";
resultsContainer.classList.add("hidden");
}
//filters dropdown list
const filterAndAppendResults = (event, names) => {
const { value } = event.target;
removeResults();
if (!value) return;
const filteredCrypto = names.filter((cryptoName) => {
return cryptoName.toLowerCase().includes(value.toLowerCase());
});
filteredCrypto.forEach((cryptoName) => {
const cryptoTag = document.createElement('p');
cryptoTag.addEventListener("click", () => {
id.value=cryptoName;
removeResults();
resultsContainer.innerHTML = "";
})
cryptoTag.innerText = cryptoName;
resultsContainer.append(cryptoTag);
});
resultsContainer.classList.remove("hidden");
}
allCrypto();
removeResults();
//gets data about a specific crypto
const getData = async () => {
try {
const cryptoId = id.value.toLowerCase();
const secondUrl = `https://api.coincap.io/v2/assets/${cryptoId}`;
const response = await axios.get(secondUrl);
console.log(response);
displayContent(response);
} catch (error) {
console.error(error);
}
}
//converts crypto into regular currency
const cryptoConvert = async () => {
const currency = currencyOutput.value;
const crypto = cryptoInput.value;
const base_url = `https://api.coingecko.com/api/v3/simple/price?ids=${crypto}&vs_currencies=${currency}`
try {
const result = await axios.get(base_url);
let convertResult = result.data[crypto][currency];
console.log(convertResult)
// display content on the page
removeConverted();
const convertContainer = document.querySelector("#convertcontainer");
const value = document.createElement("h1");
const newVal = amount.value * result.data[crypto][currency];
value.innerText = `${amount.value} ${crypto} = ${newVal} ${currency}`;
convertContainer.appendChild(value);
} catch(error) {
console.error(error);
}
}
// displays the content of a specific crypto on the page
const displayContent = (response) => {
removeCrypto();
const cryptoContainer = document.querySelector("#cryptocontainer");
const title = document.createElement("h1");
title.innerText = response.data.data.name;
cryptoContainer.appendChild(title);
const symbol = document.createElement("h2");
symbol.innerText = response.data.data.symbol;
cryptoContainer.appendChild(symbol);
const rank = document.createElement("h3");
rank.innerText = response.data.data.rank;
cryptoContainer.appendChild(rank);
const priceUsd = document.createElement("h4");
priceUsd.innerText = (response.data.data.priceUsd);
cryptoContainer.appendChild(priceUsd);
}
convertButton.addEventListener("click", (event) => {
event.preventDefault();
cryptoConvert();
})
searchButton.addEventListener("click", (event)=> {
event.preventDefault();
getData();
})
submit.addEventListener("click", (event)=> event.preventDefault())