@@ -5,16 +5,64 @@ const amountTwo = document.getElementById("amount-two");
5
5
const rate = document . getElementById ( "rate" ) ;
6
6
const swap = document . getElementById ( "swap" ) ;
7
7
8
- function calculate ( ) {
8
+ // function calculate() {
9
+ // // Show a Loading State
10
+ // rate.innerText = "Calculating...";
11
+ // const currency_one = currencyOne.value;
12
+ // const currency_two = currencyTwo.value;
13
+ // // Cache API Results in sessionStorage
14
+ // const cached = sessionStorage.getItem(currency_one);
15
+
16
+ // if (cached) {
17
+ // const data = JSON.parse(cached);
18
+ // const currentRate = data.rates[currency_two];
19
+ // rate.innerText = `1 ${currency_one} = ${currentRate} ${currency_two}`;
20
+ // amountTwo.value = (amountOne.value * currentRate).toFixed(2);
21
+ // } else {
22
+ // fetch(`https://api.exchangerate-api.com/v4/latest/${currency_one}`)
23
+ // .then((res) => res.json())
24
+ // .then((data) => {
25
+ // sessionStorage.setItem(currency_one, JSON.stringify(data));
26
+ // const currentRate = data.rates[currency_two];
27
+ // rate.innerText = `1 ${currency_one} = ${currentRate} ${currency_two}`;
28
+ // amountTwo.value = (amountOne.value * currentRate).toFixed(2);
29
+ // })
30
+ // // Add Error Handling for API Requests
31
+ // .catch(() => {
32
+ // rate.innerText = "Error: Could not fetch exchange rates.";
33
+ // amountTwo.value = "0.00";
34
+ // });
35
+ // }
36
+ // }
37
+
38
+ // Refactor to Use async/await
39
+ async function calculate ( ) {
40
+ // Show a Loading State
41
+ rate . innerText = "Calculating..." ;
9
42
const currency_one = currencyOne . value ;
10
43
const currency_two = currencyTwo . value ;
11
- fetch ( `https://api.exchangerate-api.com/v4/latest/${ currency_one } ` )
12
- . then ( ( res ) => res . json ( ) )
13
- . then ( ( data ) => {
14
- const currentRate = data . rates [ currency_two ] ;
15
- rate . innerText = `1 ${ currency_one } = ${ currentRate } ${ currency_two } ` ;
16
- amountTwo . value = ( amountOne . value * currentRate ) . toFixed ( 2 ) ;
17
- } ) ;
44
+ // Cache API Results in sessionStorage
45
+ const cached = sessionStorage . getItem ( currency_one ) ;
46
+
47
+ try {
48
+ let data ;
49
+ if ( cached ) {
50
+ data = JSON . parse ( cached ) ;
51
+ } else {
52
+ const res = await fetch (
53
+ `https://api.exchangerate-api.com/v4/latest/${ currency_one } `
54
+ ) ;
55
+ data = await res . json ( ) ;
56
+ sessionStorage . setItem ( currency_one , JSON . stringify ( data ) ) ;
57
+ }
58
+ const currentRate = data . rates [ currency_two ] ;
59
+ rate . innerText = `1 ${ currency_one } = ${ currentRate } ${ currency_two } ` ;
60
+ amountTwo . value = ( amountOne . value * currentRate ) . toFixed ( 2 ) ;
61
+ // Add Error Handling for API Requests
62
+ } catch ( error ) {
63
+ rate . innerText = "Error: Could not fetch exchange rates." ;
64
+ amountTwo . value = "0.00" ;
65
+ }
18
66
}
19
67
20
68
currencyOne . addEventListener ( "change" , calculate ) ;
0 commit comments