Skip to content

Commit 9ff5c6a

Browse files
committed
Currency Converter Project Upload
1 parent 651236d commit 9ff5c6a

File tree

8 files changed

+117
-46
lines changed

8 files changed

+117
-46
lines changed

07_currencyConverter/src/App.css

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,6 @@
55
text-align: center;
66
}
77

8-
.logo {
9-
height: 6em;
10-
padding: 1.5em;
11-
will-change: filter;
12-
transition: filter 300ms;
13-
}
14-
.logo:hover {
15-
filter: drop-shadow(0 0 2em #646cffaa);
16-
}
17-
.logo.react:hover {
18-
filter: drop-shadow(0 0 2em #61dafbaa);
19-
}
20-
21-
@keyframes logo-spin {
22-
from {
23-
transform: rotate(0deg);
24-
}
25-
to {
26-
transform: rotate(360deg);
27-
}
28-
}
29-
30-
@media (prefers-reduced-motion: no-preference) {
31-
a:nth-of-type(2) .logo {
32-
animation: logo-spin infinite 20s linear;
33-
}
34-
}
35-
36-
.card {
37-
padding: 2em;
38-
}
39-
40-
.read-the-docs {
41-
color: #888;
8+
body {
9+
background-image: url('./assets/1.jpg');
4210
}

07_currencyConverter/src/App.jsx

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,93 @@
1-
import { useState } from 'react';
1+
import React, { useState, useEffect } from 'react';
2+
import { InputBox } from './components';
3+
import useCurrencyInfo from './hooks/useCurrencyInfo';
24

35
import './App.css';
46

57
function App() {
8+
const [amount, setAmount] = useState('');
9+
const [from, setFrom] = useState('usd');
10+
const [to, setTo] = useState('pkr');
11+
const [convertedAmount, setConvertedAmount] = useState();
12+
const currencyInfo = useCurrencyInfo(from);
13+
const options = Object.keys(currencyInfo);
14+
15+
const swap = () => {
16+
setFrom(to);
17+
setTo(from);
18+
};
19+
20+
const convert = () => {
21+
const amountValue = parseFloat(amount);
22+
setConvertedAmount(amountValue * currencyInfo[to]);
23+
};
24+
25+
const handleAmountChange = (newAmount) => {
26+
if (newAmount >= 0 || newAmount === '') {
27+
setAmount(newAmount);
28+
}
29+
};
30+
31+
useEffect(() => {
32+
convert();
33+
}, [amount, from, to, currencyInfo]);
34+
635
return (
7-
<>
8-
<h1 className='text-3xl bg-orange-500'>Currency Converter</h1>
9-
</>
36+
<div
37+
className='w-full h-screen flex flex-wrap justify-center items-center bg-cover'
38+
style={{
39+
backgroundImage: 'url("")',
40+
}}
41+
>
42+
<div className='w-full h-screen flex flex-wrap justify-center items-center bg-cover bg-no-repeat'>
43+
<div className='w-full'>
44+
<div className='w-full max-w-md mx-auto border border-gray-60 rounded-lg p-5 backdrop-blur-sm bg-white/30'>
45+
<form
46+
onSubmit={(e) => {
47+
e.preventDefault();
48+
convert();
49+
}}
50+
>
51+
<div className='w-full mb-1'>
52+
<InputBox
53+
label='From'
54+
amount={amount}
55+
currencyOptions={options}
56+
onCurrencyChange={(currency) => setFrom(currency)}
57+
selectCurrency={from}
58+
onAmountChange={handleAmountChange}
59+
/>
60+
</div>
61+
<div className='relative w-full h-0.5'>
62+
<button
63+
type='button'
64+
className='absolute left-1/2 -translate-x-1/2 -translate-y-1/2 border-2 border-white rounded-md bg-blue-600 text-white px-2 py-0.5'
65+
onClick={swap}
66+
>
67+
swap
68+
</button>
69+
</div>
70+
<div className='w-full mt-1 mb-4'>
71+
<InputBox
72+
label='To'
73+
amount={convertedAmount}
74+
currencyOptions={options}
75+
onCurrencyChange={(currency) => setTo(currency)}
76+
selectCurrency={to}
77+
amountDisable
78+
/>
79+
</div>
80+
<button
81+
type='submit'
82+
className='w-full bg-blue-600 text-white px-4 py-3 rounded-lg'
83+
>
84+
Convert {from.toUpperCase()} to {to.toUpperCase()}
85+
</button>
86+
</form>
87+
</div>
88+
</div>
89+
</div>
90+
</div>
1091
);
1192
}
1293

07_currencyConverter/src/assets/1.jpg

378 KB
Loading
4.6 MB
Loading

07_currencyConverter/src/assets/react.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

07_currencyConverter/src/components/InputBox.jsx

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
1-
import React from 'react';
1+
import React, { useId } from 'react';
22

33
function InputBox({
44
label,
55
amount,
66
onAmountChange,
77
onCurrencyChange,
8-
currencyOption = [],
8+
currencyOptions = [],
99
selectCurrency = 'usd',
1010
amountDisable = false,
11-
11+
currencyDisable = false,
1212
className = '',
1313
}) {
14+
const amountInputId = useId();
1415
return (
1516
<div className={`bg-white p-3 rounded-lg text-sm flex ${className}`}>
1617
<div className='w-1/2'>
17-
<label className='text-black/40 mb-2 inline-block'>label</label>
18+
<label
19+
className='text-black/40 mb-2 inline-block'
20+
htmlFor={amountInputId}
21+
>
22+
{label}
23+
</label>
1824
<input
25+
id={amountInputId}
1926
className='outline-none w-full bg-transparent py-1.5'
2027
type='number'
2128
placeholder='Amount'
29+
disabled={amountDisable}
30+
value={amount}
31+
onChange={(e) =>
32+
onAmountChange && onAmountChange(Number(e.target.value))
33+
}
2234
/>
2335
</div>
2436
<div className='w-1/2 flex flex-wrap justify-end text-right'>
2537
<p className='text-black/40 mb-2 w-full'>Currency Type</p>
26-
<select className='rounded-lg px-1 py-1 bg-gray-100 cursor-pointer outline-none'>
27-
<option value='usd'>usd</option>
38+
<select
39+
className='rounded-lg px-1 py-1 bg-gray-100 cursor-pointer outline-none'
40+
value={selectCurrency}
41+
onChange={(e) => onCurrencyChange && onCurrencyChange(e.target.value)}
42+
disabled={currencyDisable}
43+
>
44+
{currencyOptions.map((currency) => (
45+
<option value={currency} key={currency}>
46+
{currency}
47+
</option>
48+
))}
2849
</select>
2950
</div>
3051
</div>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import InputBox from './InputBox';
2+
export { InputBox };

07_currencyConverter/src/hooks/useCurrencyInfo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function useCurrencyInfo(currency) {
99
.then((res) => res.json())
1010
.then((res) => setData(res[currency]));
1111
}, [currency]);
12-
console.log(data);
12+
1313
return data;
1414
}
1515
export default useCurrencyInfo;

0 commit comments

Comments
 (0)