-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
94 lines (77 loc) · 3.12 KB
/
app.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
const BASE_URL = "https://api.exchangerate-api.com/v4/latest/";
const dropdowns = document.querySelectorAll(".dropdown select");
const btn = document.querySelector("form button");
const swapBtn = document.querySelector(".fa-arrow-right-arrow-left"); // Select the swap icon
const fromCurr = document.querySelector(".from select");
const toCurr = document.querySelector(".to select");
const amountInput = document.querySelector(".amount input");
// Load country list options
for (let select of dropdowns) {
for (let currCode in countryList) {
let newOption = document.createElement("option");
newOption.innerText = currCode;
newOption.value = currCode;
if (select.name === "from" && currCode === "USD") {
newOption.selected = "selected";
} else if (select.name === "to" && currCode === "INR") {
newOption.selected = "selected";
}
select.append(newOption);
}
select.addEventListener("change", (evt) => {
updateFlag(evt.target);
});
}
const updateFlag = (element) => {
let currCode = element.value;
let countryCode = countryList[currCode];
let newSrc = `https://flagsapi.com/${countryCode}/flat/64.png`;
let img = element.parentElement.querySelector("img");
img.src = newSrc;
};
btn.addEventListener("click", async (evt) => {
evt.preventDefault();
await performConversion();
});
// Swap currencies
swapBtn.addEventListener("click", () => {
const temp = fromCurr.value;
fromCurr.value = toCurr.value;
toCurr.value = temp;
// Update flags after swapping
updateFlag(fromCurr);
updateFlag(toCurr);
// Trigger the conversion calculation after swapping
btn.click();
});
// Perform conversion
const performConversion = async () => {
let amount = document.querySelector(".amount input");
let amtVal = parseFloat(amount.value) || 1;
if (amtVal < 1) amtVal = 1;
const fromCurrency = fromCurr.value.toUpperCase();
const toCurrency = toCurr.value.toUpperCase();
const URL = `${BASE_URL}${fromCurrency}`;
try {
const response = await fetch(URL);
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const data = await response.json();
const rate = data.rates[toCurrency];
const convertedAmount = (amtVal * rate).toFixed(2);
const msg = `${amtVal} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
document.querySelector(".msg").innerText = msg;
console.log(msg); // Log message to console
} catch (error) {
console.error('Error fetching the exchange rate:', error);
const errorMsg = "Error fetching the exchange rate. Check console for details.";
document.querySelector(".msg").innerText = errorMsg;
console.log(errorMsg); // Log error message to console
}
};
// Trigger conversion on Enter key press
amountInput.addEventListener("keypress", (evt) => {
if (evt.key === "Enter") {
evt.preventDefault();
btn.click(); // Simulate button click
}
});