-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
99 lines (87 loc) · 3.61 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Exchange Rate Viewer</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<h1>Exchange Rate Data</h1>
<label for="base-currency">Choose Base Currency: </label>
<select id="base-currency">
<option value="USD">USD</option>
<option value="EUR">EUR</option>
<option value="GBP">GBP</option>
<option value="CNY">CNY</option>
<option value="AUD">AUD</option>
</select>
<div class="update-info">
<p><strong>Last Update:</strong> <span id="last-update">Loading...</span></p>
</div>
<table>
<thead>
<tr>
<th>#</th> <!-- Index column added -->
<th class="currency">Currency</th>
<th>Indirect Rate (Currency to Base)</th>
<th>Direct Rate (Base to Currency)</th>
</tr>
</thead>
<tbody id="exchange-rates">
</tbody>
</table>
<script>
let exchangeRates = {};
function fetchExchangeRates(baseCurrency) {
fetch(`data/latest_${baseCurrency}.json`)
.then(response => response.json())
.then(data => {
exchangeRates[baseCurrency] = data.conversion_rates;
document.getElementById('last-update').textContent = data.time_last_update_utc;
displayExchangeRates(baseCurrency);
})
.catch(error => {
console.error('Error fetching exchange rates:', error);
});
}
function displayExchangeRates(baseCurrency) {
const rates = exchangeRates[baseCurrency];
const tableBody = document.getElementById('exchange-rates');
tableBody.innerHTML = '';
let index = 1; // Start index at 1
for (let currency in rates) {
if (currency !== baseCurrency) {
const row = document.createElement('tr');
const directRate = rates[currency];
const indirectRate = 1 / directRate;
row.innerHTML = `
<td>${index}</td> <!-- Index column -->
<td class="currency">${currency}</td>
<td>${indirectRate.toFixed(4)}</td>
<td>${directRate.toFixed(4)}</td>
`;
tableBody.appendChild(row);
index++; // Increment the index
}
}
}
// Function to update base currency selection from URL
function getBaseCurrencyFromUrl() {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get('baseCurrency') || 'USD'; // Default to 'USD' if not found
}
// Set the base currency dropdown based on the URL or default to 'USD'
const baseCurrencyFromUrl = getBaseCurrencyFromUrl();
document.getElementById('base-currency').value = baseCurrencyFromUrl;
fetchExchangeRates(baseCurrencyFromUrl);
// Handle base currency change event
document.getElementById('base-currency').addEventListener('change', (event) => {
const selectedBaseCurrency = event.target.value;
// Update the URL with the selected base currency
window.history.replaceState({}, '', `?baseCurrency=${selectedBaseCurrency}`);
fetchExchangeRates(selectedBaseCurrency);
});
</script>
</body>
</html>