-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchRequests.js
More file actions
66 lines (62 loc) · 2.5 KB
/
fetchRequests.js
File metadata and controls
66 lines (62 loc) · 2.5 KB
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
class fetchClass {
fetchReqIndex() {
const searchResults = document.getElementById("resultq");
const userSearch = document.getElementById("search");
const searchBtn = document.getElementById("searchbtn");
searchBtn.addEventListener("click", resultsLoad);
async function resultsLoad(event) {
activeSettings();
event.preventDefault();
try {
let response = await fetch(
`https://stock-exchange-dot-full-stack-course-services.ew.r.appspot.com/api/v3/search?query=${userSearch.value}&limit=10&exchange=NASDAQ`
);
searchResults.innerHTML = " ";
let result = await response.json();
for (i = 0; i < result.length; i++) {
// DETAILED QUERY STRING
let detailResponse = await fetch(
`https://stock-exchange-dot-full-stack-course-services.ew.r.appspot.com/api/v3/company/profile/${result[i].symbol}`
);
// FETCHES THE API ASYNC FOR EACH RESULT IN ARRAY
let detailResult = await detailResponse.json();
// RETURNS THE API.JSON FOR ALL RESULTS
// DETAILED QUERY STRING
searchResults.innerHTML += `<a href='./company.html?symbol=${result[i].symbol}'><li class="resultq__item"><img src='https://financialmodelingprep.com/image-stock/${result[i].symbol}.png' onerror="this.onerror=null;this.src='https://financialmodelingprep.com/image-stock/PEER.jpg'">${result[i].name}(${result[i].symbol})<i id='change'>${detailResult.profile.changesPercentage}</i>
<li></a>`;
}
} catch (error) {
console.log(error);
}
defaultSettings();
const context = document.querySelector(".resultq");
const instance = new Mark(context);
instance.mark(userSearch.value);
}
}
async fetchReqTicker(element) {
try {
let fetched = await fetch(
`https://financialmodelingprep.com/api/v3/gainers?apikey=38f0db76372c4155ed8a5fd5e0b70efd`
);
let resulted = await fetched.json();
for (i = 0; i < resulted.length; i++) {
const ticker = new tickerValues(
resulted[i].ticker,
resulted[i].companyName,
resulted[i].price,
resulted[i].changesPercentage
);
ticker.changeHtml(element);
}
} catch (error) {
console.log(error);
}
}
}
document.addEventListener("DOMContentLoaded", () => {
const fetchInst = new fetchClass();
fetchInst.fetchReqIndex();
const marquee = document.getElementById("marquee");
fetchInst.fetchReqTicker(marquee);
});