-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.js
100 lines (96 loc) · 3.7 KB
/
crypto.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
const form = document.querySelector(".top-banner form");
const input = document.querySelector(".top-banner input");
//.class1.class2 vs. .class1 .class2
const msgSpan = document.querySelector(".container .msg");
const coinList = document.querySelector(".ajax-section .container .coins");
//localStorage
localStorage.setItem(
"apiKey",
EncryptStringAES(
"coinranking7b1abcb95522fe483f1fe4444b60367d0763031ba652ba38"
)
);
form.addEventListener("submit", (e) => {
e.preventDefault();
getCoinDataFromApi();
//form.reset == e.target.reset
// e.currentTarget.reset()
e.target.reset();//formun icinde birden fazla input oldugunda hangisi tiklanirsa onun icini bosaltmak icin
});
const getCoinDataFromApi = async () => {
//alert("Get Coin Data!!");
const apiKey = DecryptStringAES(localStorage.getItem("apiKey"));
//console.log(apiKey);
//!!!template literal!!!
const url = `https://api.coinranking.com/v2/coins?search=${input.value}&limit=1`;
const options = {
headers: {
"x-access-token": apiKey,
},
};
//fetch vs. axios
// const response = await fetch(url, options)
// .then((response) => response.json())
// .then((result) => console.log(result.data.coins[0]));
try {
const response = await axios(url, options);
//console.log(response.data.data.coins[0]);
//obj. dest.
const { price, name, change, iconUrl, symbol } =
response.data.data.coins[0];
//console.log(iconUrl);
//Coin Control!!!
const coinNameSpans = coinList.querySelectorAll("h2 span"); //coinNameSpans nodeList dönüyor sdc forEch() calisir o nedenle filter map icin asagidaki gibi array a döndürmeliyiz
//filter vs. map(array)
if (coinNameSpans.length > 0) {//kart yoksa bosuna filter islemi yapmasin diye
const filteredArray = [...coinNameSpans].filter(
(span) => span.innerText == name
);
console.log(filteredArray);
if (filteredArray.length > 0) {
msgSpan.innerText = `You already know the data for ${name}, Please search for another coin 😉`;
setTimeout(() => {
msgSpan.innerText = "";
}, 3000);
return;
}
}
//continue xxxx return
const createdLi = document.createElement("li");
createdLi.classList.add("coin");
createdLi.innerHTML = `
<h2 class="coin-name" data-name=${name}>
<span>${name}</span>
<sup>${symbol}</sup>
</h2>
<div class="coin-temp">$${Number(price).toFixed(6)}</div>
<figure>
<img class="coin-icon" src=${iconUrl}>
<figcaption style='color:${change < 0 ? "red" : "green"}'>
<span><i class="fa-solid fa-chart-line"></i></span>
<span>${change}%</span>
</figcaption>
</figure>
<span class="remove-icon">
<i class="fas fa-window-close" style="color:red"></i>
</span>`;
//append vs. prepend
//append vs appendChild
coinList.prepend(createdLi);
//remove func.
createdLi
.querySelector(".remove-icon")
.addEventListener("click", () => {
createdLi.remove();
});
} catch (error) {
//error logging
//postErrorLog("crypto.js", "getCoinDataFromApi", new Date(), error...);
msgSpan.innerText = `Coin not found!`;
setTimeout(() => {
msgSpan.innerText = "";
}, 3000);
}
};
//http response codes
//rest api vs. soap api