-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
473 lines (396 loc) · 17.4 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
const BASE_URL = 'https://api.coincap.io/v2/assets?amount=100';
const btn = document.querySelector('input');
const main = document.querySelector('main')
const div = document.createElement('div');
div.setAttribute('class', 'container');
const submit = document.querySelector('submit')
/**
* WHEN THE FORM IS SUBMITTED
*/
document.querySelector("form").addEventListener('submit', (event) => {
event.preventDefault()
fetch(`https://api.coincap.io/v2/assets/${event.target.crypto.value.toLowerCase()}`)
.then((res) => res.json())
.then((response) => {
console.log('fetch successful')
//GRABBING SEARCH VALUE
let search = document.getElementById('search').value
console.log(search)
//REMOVING DIV THAT HOLDS LIST ONCE FORM IS SUBMITTED
div.remove()
//GRABBING COING DATA
let coins = response.data
//ERROR HANDELING: IF COIN IS NOT ON THE LIST
if (!coins) {
alert(`COIN IS NOT ON THE HOT 100 LIST.`)
window.location.reload();
}
/**
* CREATE SECTION
* ADD CLASS
* ADD SECTION TO THE END OF MAIN
*/
let section = document.createElement('section');
section.setAttribute('class', 'search-container');
main.append(section)
//CLEARING CONTAINER AFTER SEARCH
let sContainer = document.querySelector('.search-container');
sContainer.innerHTML = '';
//RANK DATA
let rank = response.data.rank;
console.log(rank)
//COIN NAME DATE
let name = response.data.name;
console.log(name)
//SYMBOL DATA
let symbol = response.data.symbol;
console.log(symbol)
//PRICE DATA
let price = parseFloat(response.data.priceUsd).toFixed(2);
console.log(price)
//MARKETCAP DATE
let marketCap = parseFloat(response.data.marketCapUsd).toFixed(2)
console.log(marketCap)
//PERCENT CHANGE IN 24HRS
let change = parseFloat(response.data.changePercent24Hr).toFixed(2)
console.log(change)
/**
* CREATE AN ARTICLE
* ADD CLASS
*/
let article = document.createElement('article');
article.className = 'card';
console.log(article)
/**
* CREATE H2
* UPDATE TEXT CONTENT TO RANK DATA
* ADD TO SECTION
*/
let heading = document.createElement('h2');
heading.innerHTML = `#${rank}`
section.append(heading);
/**
* CREATE H3
* UPDATE TEXT CONTENT TO SYMBOL DATA
* ADD TO SECTION AFTER HEADING
*/
let shortened = document.createElement('h3')
shortened.innerHTML = symbol
heading.after(shortened)
/**
* CREATE H4
* UPDATE TEXT CONTENT TO NAME DATA
* ADD TO SECTION AFTER SYMBOL
*/
let fullname = document.createElement('h4');
fullname.innerHTML = name
shortened.after(fullname)
/**
* CREATE P
* UPDATE TEXT CONTENT TO PRICE DATA
* ADD TO SECTION AFTER NAME
*/
let usd = document.createElement('p');
usd.innerHTML = `$${price}`;
fullname.after(usd)
/**
* CHECKING TO SEE IF MARKET CAP IS IN THE BILLIONS
* ADDS 'B' TO ADD OF PRICE
*/
console.log(`Marketcap: ${marketCap}`)
let market = document.createElement('p');
if (marketCap.length >= 10) {
market.textContent = `$${marketCap.slice(0, 3)}B`;
usd.after(market)
}
/**
* CHECKING TO SEE IF MARKET CAP IS IN THE MILLIONS
* ADDS 'M' TO ADD OF PRICE
*/
if (marketCap >= 1000000 && marketCap < 1000000000) {
market.textContent = `$${marketCap.slice(0, 3)}M`;
usd.after(market)
}
/**
* CREATE P ELEMENT
* UPDATING CONTENT WITH MARKET CHANGE %
*/
let marketChange = document.createElement('p');
marketChange.textContent = `${change}%`;
market.after(marketChange)
//IF THE MMARKET CHANGES NEG OR POS IN 24HRS
if (change < 0) {
marketChange.style.color = "red";
marketChange.innerHTML = change + '%'
} else if (change > 0) {
marketChange.style.color = "green";
marketChange.innerHTML = change + '%';
} else {
marketChange.style.color = "black";
}
//CREATING ELEMENT FOR CHART
const tvDiv = document.createElement('div');
tvDiv.setAttribute('class', 'tradingview-widget-container');
tvDiv.setAttribute('id', 'tvchart')
marketChange.after(tvDiv)
//FUNCTION IS CALLED WHEN PAGE IS LOADED
loadCharts()
function loadCharts() {
//GETTING CHARTS
//SYMBOlSS IS VALUE FROM TV/BINANCE API
//SYMBOL IS COINCAP API
let symbolss = symbol
console.log(symbolss);
new TradingView.widget(
{
"autosize": true,
//PULLS THE CHART DATA
"symbol": symbolss,
"interval": "5",
"timezone": "America/New_York",
"theme": "light",
"style": "1",
"locale": "in",
"toolbar_bg": "#f1f3f6",
"enable_publishing": false,
"allow_symbol_change": true,
"container_id": "tvchart"
}//OBJ CLOSING TAH
);//TRADINGVIEW WIDGET CLOSING TAG
}//FUNCTION CLOSING TAg
//clearing form after submit
const form = document.querySelector('form')
form.reset();
})
.catch((error) => {
console.log(error)
});
}) //eventlistener closing tag
// /**
// * WHEN WINDOW LOADS, LIST OF 100 TOP CRYPTO COINS WILL LOAD
// */
window.addEventListener('load', (event) => {
event.preventDefault()
fetch(`${BASE_URL}`)
.then((res) => res.json())
.then((response) => {
console.log(response)
//GRABBING LIST OF COINS
let coins = response.data
//CREATING A FUNCTION THAT PULLS THE DATA, PLACES INTO VARIABLES, AND FORMATS
function getCoin() {
for (let i = 0; i < coins.length; i++) {
//RANK INFO
let rank = response.data[i].rank;
console.log(rank)
//NAME INFO
let name = response.data[i].name;
console.log(name)
//SYMBOL INFO
let symbol = response.data[i].symbol;
console.log(symbol)
//PRICE INFO
let price = parseFloat(response.data[i].priceUsd).toFixed(2);
console.log(price)
//MARKETCAP INFO
let marketCap = parseFloat(response.data[i].marketCapUsd).toFixed(2)
console.log(marketCap)
//PERCENT CHANGE INFO
let change = parseFloat(response.data[i].changePercent24Hr).toFixed(2)
console.log(change)
/**
* CREATING ARTICLE
* ADD CLASS NAME
*/
let article = document.createElement('article');
article.className = 'card';
console.log(article)
/**
* CREATING H2
* SETTING H2 TO RANK VALUE
*/
let heading = document.createElement('h2');
heading.textContent = `#${rank}`
article.append(heading);
/**
* CREATING H3
* SETTING H3 TO SYMBOL VALUE
*/
let shortened = document.createElement('h3')
shortened.textContent = symbol
heading.after(shortened)
/**
* CREATING H4
* SETTING H4 TO NAME VALUE
*/
let fullname = document.createElement('h4');
fullname.innerHTML = name
shortened.after(fullname)
/**
* CREATING P
* SETTING P TO PRICE VALUE
*/
let usd = document.createElement('p');
usd.textContent = `$${price}`;
fullname.after(usd)
/**
* CREATING P
* CHECKING TO SEE IF GREATHER THAN 10
* IF TRUE, SETTING P TO MARKETCAP VALUE + 'B' FOR BILLIONS
*/
let market = document.createElement('p');
if (marketCap.length >= 10) {
market.textContent = `$${marketCap.slice(0, 3)}B`;
usd.after(market)
}
/**
* CREATING P
* CHECKING TO SEE IF BETWEEN 1MILLION & LESS THAN 1 BILLION
* IF TRUE, SETTING P TO MARKETCAP VALUE + 'M' FOR MILLIONS
*/
if (marketCap >= 1000000 && marketCap < 1000000000) {
market.textContent = `$${marketCap.slice(0, 3)}M`;
usd.after(market)
}
/**
* CREATING P
* SETTING MARKET CHANGE TO CHANGE VALUE %
*/
let marketChange = document.createElement('p');
marketChange.textContent = `${change}%`;
market.after(marketChange)
//IF THE MARKET CHANGES POS OR NEG IN 24HRS
if (change < 0) {
marketChange.style.color = "red";
marketChange.innerHTML = change + '%'
} else if (change > 0) {
marketChange.style.color = "green";
marketChange.innerHTML = change + '%';
} else {
marketChange.style.color = "black";
}
//ADDING TO DOM
main.append(div)
div.append(article)
article.addEventListener('click', (event) => {
event.preventDefault()
article.style.marginBottom = '10px'
console.log('yes this is true')
//CREATING DIV FOR CHART *IMPORTANT*
const tvDiv = document.createElement('div');
tvDiv.setAttribute('class', 'tradingview-widget-container');
tvDiv.setAttribute('id', 'tvchart')
article.append(tvDiv)
/**
* WHEHN DBLCLICK SET ARTICLE STYLE BACK TO ORGINAL SIZE
* REMOVE THE CHART
*/
article.addEventListener('dblclick', (event) => {
event.preventDefault()
// article.addClass('no-hover')
article.style.width = 'auto';
article.style.height = 'auto';
tvDiv.remove()
article.style.marginBottom = '1px';
})
//FUNCTION IS CALLED WHEN PAGE IS LOADED
loadCharts()
function loadCharts() {
//GETTING CHARTS
//SYMBOlSS IS VALUE FROM TV/BINANCE API
//SYMBOL IS COINCAP API
let symbolss = symbol
console.log(symbolss);
new TradingView.widget(
{
"autosize": true,
//PULLS THE CHART DATA
"symbol": symbolss,
"interval": "5",
"timezone": "America/New_York",
"theme": "light",
"style": "1",
"locale": "in",
"toolbar_bg": "#f1f3f6",
"enable_publishing": false,
"allow_symbol_change": true,
"container_id": "tvchart"
}//OBJ CLOSING TAH
);//TRADINGVIEW WIDGET CLOSING TAG
}//FUNCTION CLOSING TAg
})//EVENT LISTENER CLOSING TAG
} //FOR CLOSING TAG
}//FUNCTION CLOSING TAG
/**
* CREATING THE TITLE CATEGORIES
*/
let section = document.createElement('section');
let p = document.createElement('p')
p.setAttribute('class', 'title')
p.textContent = 'Rank'
let p2 = document.createElement('p')
p2.setAttribute('class', 'title')
p2.textContent = 'Symbol '
let p3 = document.createElement('p')
p3.setAttribute('class', 'title')
p3.textContent = 'Name'
let p4 = document.createElement('p')
p4.setAttribute('class', 'title')
p4.textContent = 'Price'
let p5 = document.createElement('p')
p5.setAttribute('class', 'title')
p5.textContent = 'Marketcap'
let p6 = document.createElement('p')
p6.setAttribute('class', 'title')
p6.textContent = '24hr'
/**
* NAME OF LIST
*/
let hot = document.createElement('h1')
hot.setAttribute('class', 'hot')
hot.textContent = 'Hot 100'
hot.style.fontFamily = 'Impact'
main.append(hot)
/**
* CURRENT DATE
*/
let hotP = document.createElement('p')
hotP.setAttribute('class', 'date')
let today = new Date();
let dd = String(today.getDate()).padStart(2, '0');
let mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
let yyyy = today.getFullYear();
today = mm + '.' + dd + '.' + yyyy;
hotP.innerHTML = today
hot.append(hotP)
//ADDING TO DOM:
main.append(section)
section.append(p)
p.after(p2)
p2.after(p3)
p3.after(p4)
p4.after(p5)
p5.after(p6)
//CALLING FUNCTION TO CREATE THE FORMAT FOR THE LIST
getCoin()
})
.catch((error) => {
console.log(error)
})
})
// Get the button:
let mybutton = document.getElementById("myBtn");
// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
// When the user clicks on the button, scroll to the top of the document
function topFunction() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}