-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathApp.jsx
161 lines (147 loc) · 4.3 KB
/
App.jsx
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { useEffect, useState } from "react";
import Header from "./components/Header/Header";
import Logo from "./components/Logo";
import Status from "./components/Status/Status";
import Converter from "./components/Converter/Converter";
import Result from "./components/Converter/Result/Result";
import Convert from "./components/Converter/Convert/Convert";
import Footer from "./components/Footer/Footer";
import ThemeSwitch from "./components/ThemeSwitch/ThemeSwitch";
import { isInputNum } from "./utils/validate-input";
const initialcurrency = [
{
id: 147,
name: "US Dollar",
short_code: "USD",
symbol: "$",
},
{
id: 64,
name: "Indian Rupee",
short_code: "INR",
symbol: "₹",
},
];
const API_URL =
import.meta.env.MODE === "production"
? import.meta.env.VITE_API_URL
: "http://localhost:5000";
export default function App() {
const [data, setData] = useState([]);
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const [result, setResult] = useState({});
// select currency and conversion amount
const [amount, setAmount] = useState("100");
const [baseCurrency, setBaseCurrency] = useState(initialcurrency.at(0));
const [foreignCurrency, setForeignCurrency] = useState(initialcurrency.at(1));
// fetch currencies
useEffect(() => {
async function getCurrencies() {
try {
const res = await fetch(`${API_URL}/currencies`);
if (!res.ok) {
throw new Error("Error Fetching Data ⛔");
}
const currencies = await res.json();
if (currencies.meta.code === 503) {
throw new Error("Service Unavailable");
}
setData(currencies.response);
setLoading(false);
} catch (error) {
console.log(error);
setError(error.message);
} finally {
setLoading(false);
}
}
setError("");
getCurrencies();
}, []);
// validate amount input
function handleAmount(e) {
if (isInputNum(e.target.value) && e.target.value.length <= 20) {
setAmount(e.target.value);
}
return;
}
function swapCurrency() {
setBaseCurrency(foreignCurrency);
setForeignCurrency(baseCurrency);
if (!amount) {
setResult({});
}
}
const handleConversion = async () => {
try {
setLoading(true);
const base = baseCurrency.short_code.toUpperCase();
const foreign = foreignCurrency.short_code.toUpperCase();
const res = await fetch(
`${API_URL}/convert?base=${base}&foreign=${foreign}&amount=${amount}`
);
if (!res.ok) {
throw new Error("Conversion Failed ⛔");
}
const conversion = await res.json();
setResult(conversion);
setLoading(false);
} catch (error) {
console.log("Conversion Failed:", error);
} finally {
setLoading(false);
}
};
// perform conversion when base or foreign currency changes
useEffect(() => {
if (amount > 0 && result) {
handleConversion();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [baseCurrency, foreignCurrency]);
// update title when conversion is performed
useEffect(() => {
if (!result?.from || !result?.to) return;
document.title = `${Number(
amount
).toLocaleString()} ${result?.from.toUpperCase()} to ${result?.to.toUpperCase()} Conversion - Currencee`;
return () => {
document.title =
"Currencee | The World's Most Trusted and Secure Currency Conveter";
};
}, [amount, result]);
return (
<div className="app">
<Header>
<Logo />
<Status />
</Header>
<Converter>
<Convert
data={data}
error={error}
amount={amount}
handleAmount={handleAmount}
setAmount={setAmount}
baseCurrency={baseCurrency}
foreignCurrency={foreignCurrency}
setBaseCurrency={setBaseCurrency}
setForeignCurrency={setForeignCurrency}
swapCurrency={swapCurrency}
handleConversion={handleConversion}
/>
<Result
result={result}
loading={loading}
amount={amount}
baseCurrency={baseCurrency}
foreignCurrency={foreignCurrency}
/>
</Converter>
<Footer>
<ThemeSwitch />
</Footer>
</div>
);
}