-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript(improved).js
More file actions
55 lines (49 loc) · 1.92 KB
/
script(improved).js
File metadata and controls
55 lines (49 loc) · 1.92 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
const currencies = {
dollar: {
uri: 'https://open.er-api.com/v6/latest/USD',
getValue: (value) => value.rates.ARS,
paragraph: document.getElementById('dollarValue'),
},
bitcoin: {
uri: 'https://api.coindesk.com/v1/bpi/currentprice.json',
getValue: (value) => value.bpi.USD.rate,
paragraph: document.getElementById('bitcoinValue'),
},
pesoArgentino: {
uri: 'https://open.er-api.com/v6/latest/ARS',
getValue: (value) => value.rates.USD,
paragraph: document.getElementById('pesoValue'),
},
};
// This function gets the updated value of a given currency
function getCurrencyValue(currencyKey) {
const currency = currencies[currencyKey];
return fetch(currency.uri)
.then((data) => data.json())
.then((value) => currency.getValue(value))
.catch((error) => console.error('Can not get info from API' + error));
}
async function loadCurrency(currencyKey, symbol) {
const currency = currencies[currencyKey];
// Create an 'img' element to contain the loading gif animation
var img = document.createElement('img');
// Fill the 'img' element with the loading gif animation
img.src = './src/loading.gif';
// Add the loading animation meanwhile the next promise is completed
currency.paragraph.appendChild(img);
// Get the currency value
return await getCurrencyValue(currencyKey)
// Replace the HTML paragraph content with the current value and remove the loading animation
.then((value) => {
currency.paragraph.removeChild(img);
currency.paragraph.textContent = `${symbol} ${value}`;
})
// If something goes wrong rise an error
.catch((error) => console.error('There was an error' + error));
}
// Show dollar current price
loadCurrency('dollar', '$').then();
// Show bitcoin current price
loadCurrency('bitcoin', 'B').then();
// Show peso argentino current price
loadCurrency('pesoArgentino', 'ARS').then();