Currency exchange app
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I'm making a currency exchange app, it works as follows:
User types in on desired currency input and the rest of the input fields change with state based on the currency value from the input used, I'm providing the main component (container), but this codesandbox has a working demo.
- I need some help or guidance on how to
setState
in a cleaner way probably with a loop, map or a new function. - Any help on how to create my
CURRENCIES
in a better way, considering that maybe someday the API I'm using will add an extra currency, and right now I would have to add it manually. - Considering point #2 mentioned above, I guess I will also need to improve my switch statement to adapt to the API growing in currencies.
This is a working codesandbox.
import React, Component from "react";
import render from "react-dom";
import SingleInput from "./SingleInput.js";
import fetchedCurrencies from "./data.json";
const CURRENCIES = [
name: "MXN" ,
name: "USD", value: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE ,
name: "USDFix", value: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE ,
name: "EUR", value: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE ,
name: "CAD", value: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE ,
name: "JPY", value: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE ,
name: "GBP", value: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
];
class App extends Component
constructor(props)
super(props);
this.state =
MXN: "",
USD: "",
USDFix: "",
EUR: "",
CAD: "",
JPY: "",
GBP: ""
;
handleCurrencyInput = event =>
const name, value = event.target;
switch (name)
case "USD":
this.setState(
MXN: value * CURRENCIES[1].value,
USD: value,
USDFix: value * CURRENCIES[1].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[1].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[1].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[1].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[1].value / CURRENCIES[6].value
);
break;
case "USDFix":
this.setState(
MXN: value * CURRENCIES[2].value,
USD: value * CURRENCIES[2].value / CURRENCIES[1].value,
USDFix: value,
EUR: value * CURRENCIES[2].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[2].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[2].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[2].value / CURRENCIES[6].value
);
break;
case "EUR":
this.setState(
MXN: value * CURRENCIES[3].value,
USD: value * CURRENCIES[3].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[3].value / CURRENCIES[2].value,
EUR: value,
CAD: value * CURRENCIES[3].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[3].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[3].value / CURRENCIES[6].value
);
break;
case "CAD":
this.setState(
MXN: value * CURRENCIES[4].value,
USD: value * CURRENCIES[4].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[4].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[4].value / CURRENCIES[3].value,
CAD: value,
JPY: value * CURRENCIES[4].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[4].value / CURRENCIES[6].value
);
break;
case "JPY":
this.setState(
MXN: value * CURRENCIES[5].value,
USD: value * CURRENCIES[5].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[5].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[5].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[5].value / CURRENCIES[4].value,
JPY: value,
GBP: value * CURRENCIES[5].value / CURRENCIES[6].value
);
break;
case "GBP":
this.setState(
MXN: value * CURRENCIES[6].value,
USD: value * CURRENCIES[6].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[6].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[6].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[6].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[6].value / CURRENCIES[5].value,
GBP: value
);
break;
default:
this.setState(
MXN: value,
USD: value / CURRENCIES[1].value,
USDFix: value / CURRENCIES[2].value,
EUR: value / CURRENCIES[3].value,
CAD: value / CURRENCIES[4].value,
JPY: value / CURRENCIES[5].value,
GBP: value / CURRENCIES[6].value
);
;
render()
return (
<div>
CURRENCIES.map(currency => (
<SingleInput
type="text"
maxLength=12
key=currency.name
name=currency.name
placeholder=`$currency.name $`
value= ""
onChange=this.handleCurrencyInput
/>
))
</div>
);
export default App;
javascript finance react.js state
add a comment |Â
up vote
1
down vote
favorite
I'm making a currency exchange app, it works as follows:
User types in on desired currency input and the rest of the input fields change with state based on the currency value from the input used, I'm providing the main component (container), but this codesandbox has a working demo.
- I need some help or guidance on how to
setState
in a cleaner way probably with a loop, map or a new function. - Any help on how to create my
CURRENCIES
in a better way, considering that maybe someday the API I'm using will add an extra currency, and right now I would have to add it manually. - Considering point #2 mentioned above, I guess I will also need to improve my switch statement to adapt to the API growing in currencies.
This is a working codesandbox.
import React, Component from "react";
import render from "react-dom";
import SingleInput from "./SingleInput.js";
import fetchedCurrencies from "./data.json";
const CURRENCIES = [
name: "MXN" ,
name: "USD", value: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE ,
name: "USDFix", value: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE ,
name: "EUR", value: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE ,
name: "CAD", value: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE ,
name: "JPY", value: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE ,
name: "GBP", value: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
];
class App extends Component
constructor(props)
super(props);
this.state =
MXN: "",
USD: "",
USDFix: "",
EUR: "",
CAD: "",
JPY: "",
GBP: ""
;
handleCurrencyInput = event =>
const name, value = event.target;
switch (name)
case "USD":
this.setState(
MXN: value * CURRENCIES[1].value,
USD: value,
USDFix: value * CURRENCIES[1].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[1].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[1].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[1].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[1].value / CURRENCIES[6].value
);
break;
case "USDFix":
this.setState(
MXN: value * CURRENCIES[2].value,
USD: value * CURRENCIES[2].value / CURRENCIES[1].value,
USDFix: value,
EUR: value * CURRENCIES[2].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[2].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[2].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[2].value / CURRENCIES[6].value
);
break;
case "EUR":
this.setState(
MXN: value * CURRENCIES[3].value,
USD: value * CURRENCIES[3].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[3].value / CURRENCIES[2].value,
EUR: value,
CAD: value * CURRENCIES[3].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[3].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[3].value / CURRENCIES[6].value
);
break;
case "CAD":
this.setState(
MXN: value * CURRENCIES[4].value,
USD: value * CURRENCIES[4].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[4].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[4].value / CURRENCIES[3].value,
CAD: value,
JPY: value * CURRENCIES[4].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[4].value / CURRENCIES[6].value
);
break;
case "JPY":
this.setState(
MXN: value * CURRENCIES[5].value,
USD: value * CURRENCIES[5].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[5].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[5].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[5].value / CURRENCIES[4].value,
JPY: value,
GBP: value * CURRENCIES[5].value / CURRENCIES[6].value
);
break;
case "GBP":
this.setState(
MXN: value * CURRENCIES[6].value,
USD: value * CURRENCIES[6].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[6].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[6].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[6].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[6].value / CURRENCIES[5].value,
GBP: value
);
break;
default:
this.setState(
MXN: value,
USD: value / CURRENCIES[1].value,
USDFix: value / CURRENCIES[2].value,
EUR: value / CURRENCIES[3].value,
CAD: value / CURRENCIES[4].value,
JPY: value / CURRENCIES[5].value,
GBP: value / CURRENCIES[6].value
);
;
render()
return (
<div>
CURRENCIES.map(currency => (
<SingleInput
type="text"
maxLength=12
key=currency.name
name=currency.name
placeholder=`$currency.name $`
value= ""
onChange=this.handleCurrencyInput
/>
))
</div>
);
export default App;
javascript finance react.js state
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm making a currency exchange app, it works as follows:
User types in on desired currency input and the rest of the input fields change with state based on the currency value from the input used, I'm providing the main component (container), but this codesandbox has a working demo.
- I need some help or guidance on how to
setState
in a cleaner way probably with a loop, map or a new function. - Any help on how to create my
CURRENCIES
in a better way, considering that maybe someday the API I'm using will add an extra currency, and right now I would have to add it manually. - Considering point #2 mentioned above, I guess I will also need to improve my switch statement to adapt to the API growing in currencies.
This is a working codesandbox.
import React, Component from "react";
import render from "react-dom";
import SingleInput from "./SingleInput.js";
import fetchedCurrencies from "./data.json";
const CURRENCIES = [
name: "MXN" ,
name: "USD", value: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE ,
name: "USDFix", value: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE ,
name: "EUR", value: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE ,
name: "CAD", value: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE ,
name: "JPY", value: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE ,
name: "GBP", value: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
];
class App extends Component
constructor(props)
super(props);
this.state =
MXN: "",
USD: "",
USDFix: "",
EUR: "",
CAD: "",
JPY: "",
GBP: ""
;
handleCurrencyInput = event =>
const name, value = event.target;
switch (name)
case "USD":
this.setState(
MXN: value * CURRENCIES[1].value,
USD: value,
USDFix: value * CURRENCIES[1].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[1].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[1].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[1].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[1].value / CURRENCIES[6].value
);
break;
case "USDFix":
this.setState(
MXN: value * CURRENCIES[2].value,
USD: value * CURRENCIES[2].value / CURRENCIES[1].value,
USDFix: value,
EUR: value * CURRENCIES[2].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[2].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[2].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[2].value / CURRENCIES[6].value
);
break;
case "EUR":
this.setState(
MXN: value * CURRENCIES[3].value,
USD: value * CURRENCIES[3].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[3].value / CURRENCIES[2].value,
EUR: value,
CAD: value * CURRENCIES[3].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[3].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[3].value / CURRENCIES[6].value
);
break;
case "CAD":
this.setState(
MXN: value * CURRENCIES[4].value,
USD: value * CURRENCIES[4].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[4].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[4].value / CURRENCIES[3].value,
CAD: value,
JPY: value * CURRENCIES[4].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[4].value / CURRENCIES[6].value
);
break;
case "JPY":
this.setState(
MXN: value * CURRENCIES[5].value,
USD: value * CURRENCIES[5].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[5].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[5].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[5].value / CURRENCIES[4].value,
JPY: value,
GBP: value * CURRENCIES[5].value / CURRENCIES[6].value
);
break;
case "GBP":
this.setState(
MXN: value * CURRENCIES[6].value,
USD: value * CURRENCIES[6].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[6].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[6].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[6].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[6].value / CURRENCIES[5].value,
GBP: value
);
break;
default:
this.setState(
MXN: value,
USD: value / CURRENCIES[1].value,
USDFix: value / CURRENCIES[2].value,
EUR: value / CURRENCIES[3].value,
CAD: value / CURRENCIES[4].value,
JPY: value / CURRENCIES[5].value,
GBP: value / CURRENCIES[6].value
);
;
render()
return (
<div>
CURRENCIES.map(currency => (
<SingleInput
type="text"
maxLength=12
key=currency.name
name=currency.name
placeholder=`$currency.name $`
value= ""
onChange=this.handleCurrencyInput
/>
))
</div>
);
export default App;
javascript finance react.js state
I'm making a currency exchange app, it works as follows:
User types in on desired currency input and the rest of the input fields change with state based on the currency value from the input used, I'm providing the main component (container), but this codesandbox has a working demo.
- I need some help or guidance on how to
setState
in a cleaner way probably with a loop, map or a new function. - Any help on how to create my
CURRENCIES
in a better way, considering that maybe someday the API I'm using will add an extra currency, and right now I would have to add it manually. - Considering point #2 mentioned above, I guess I will also need to improve my switch statement to adapt to the API growing in currencies.
This is a working codesandbox.
import React, Component from "react";
import render from "react-dom";
import SingleInput from "./SingleInput.js";
import fetchedCurrencies from "./data.json";
const CURRENCIES = [
name: "MXN" ,
name: "USD", value: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE ,
name: "USDFix", value: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE ,
name: "EUR", value: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE ,
name: "CAD", value: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE ,
name: "JPY", value: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE ,
name: "GBP", value: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
];
class App extends Component
constructor(props)
super(props);
this.state =
MXN: "",
USD: "",
USDFix: "",
EUR: "",
CAD: "",
JPY: "",
GBP: ""
;
handleCurrencyInput = event =>
const name, value = event.target;
switch (name)
case "USD":
this.setState(
MXN: value * CURRENCIES[1].value,
USD: value,
USDFix: value * CURRENCIES[1].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[1].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[1].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[1].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[1].value / CURRENCIES[6].value
);
break;
case "USDFix":
this.setState(
MXN: value * CURRENCIES[2].value,
USD: value * CURRENCIES[2].value / CURRENCIES[1].value,
USDFix: value,
EUR: value * CURRENCIES[2].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[2].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[2].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[2].value / CURRENCIES[6].value
);
break;
case "EUR":
this.setState(
MXN: value * CURRENCIES[3].value,
USD: value * CURRENCIES[3].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[3].value / CURRENCIES[2].value,
EUR: value,
CAD: value * CURRENCIES[3].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[3].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[3].value / CURRENCIES[6].value
);
break;
case "CAD":
this.setState(
MXN: value * CURRENCIES[4].value,
USD: value * CURRENCIES[4].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[4].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[4].value / CURRENCIES[3].value,
CAD: value,
JPY: value * CURRENCIES[4].value / CURRENCIES[5].value,
GBP: value * CURRENCIES[4].value / CURRENCIES[6].value
);
break;
case "JPY":
this.setState(
MXN: value * CURRENCIES[5].value,
USD: value * CURRENCIES[5].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[5].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[5].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[5].value / CURRENCIES[4].value,
JPY: value,
GBP: value * CURRENCIES[5].value / CURRENCIES[6].value
);
break;
case "GBP":
this.setState(
MXN: value * CURRENCIES[6].value,
USD: value * CURRENCIES[6].value / CURRENCIES[1].value,
USDFix: value * CURRENCIES[6].value / CURRENCIES[2].value,
EUR: value * CURRENCIES[6].value / CURRENCIES[3].value,
CAD: value * CURRENCIES[6].value / CURRENCIES[4].value,
JPY: value * CURRENCIES[6].value / CURRENCIES[5].value,
GBP: value
);
break;
default:
this.setState(
MXN: value,
USD: value / CURRENCIES[1].value,
USDFix: value / CURRENCIES[2].value,
EUR: value / CURRENCIES[3].value,
CAD: value / CURRENCIES[4].value,
JPY: value / CURRENCIES[5].value,
GBP: value / CURRENCIES[6].value
);
;
render()
return (
<div>
CURRENCIES.map(currency => (
<SingleInput
type="text"
maxLength=12
key=currency.name
name=currency.name
placeholder=`$currency.name $`
value= ""
onChange=this.handleCurrencyInput
/>
))
</div>
);
export default App;
javascript finance react.js state
edited May 1 at 1:35
Jamalâ¦
30.1k11114225
30.1k11114225
asked Apr 17 at 4:38
ricardoNava
325
325
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
In handleCurrencyInput
the formula is always converted = value * inputcurrencyrate / outputcurrencyrate;
The two special cases MXN, and identity (eg USD -> USD) still follow the equation if you set MXN to 1
So if you structure CURRENCIES as a dict instead of an array you can simplify to
const CURRENCIES =
MXN: 1,
USD: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE,
USDFix: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE,
EUR: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE,
CAD: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE,
JPY: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE,
GBP: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
;
...
handleCurrencyInput = event =>
const name, value = event.target;
const newState = ;
for (const currency in CURRENCIES)
this.setState([currency]: value * (CURRENCIES[name] / CURRENCIES[currency]))
And adding additional currencies is trivial
Adapted render:
render()
return (
<div>
Object.keys(CURRENCIES).map(currency => (
<SingleInput
type="text"
maxLength=12
key=currency
name=currency
placeholder=
value=
onChange=this.handleCurrencyInput
/>
))
</div>
);
Is a dict basically an object? or what is the difference?
â ricardoNava
May 3 at 2:28
1
Yes, just an simple object
â generalhenry
May 3 at 2:31
So I encountered a problem with the way I'm using.map()
onCURRENCIES
if you see the codebox example you can see what I'm talking about, any ideas how to solve this considering that? I know i can useObject.keys(CURRENCIES).map()
but it doesn't let me input decimal points wouldn't know if it's related though, just an observation.
â ricardoNava
May 3 at 3:50
I added an updated render, I'm not sure what decimal point issue you're encountering.
â generalhenry
May 3 at 3:55
So there are two cases: Trying to use a decimal point as in 15.50 and also numbers like 15 triggers some kind of bug on currencies different from MXN. I adapted the sandbox with your code and it replicates the problem codesandbox
â ricardoNava
May 3 at 4:06
 |Â
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
In handleCurrencyInput
the formula is always converted = value * inputcurrencyrate / outputcurrencyrate;
The two special cases MXN, and identity (eg USD -> USD) still follow the equation if you set MXN to 1
So if you structure CURRENCIES as a dict instead of an array you can simplify to
const CURRENCIES =
MXN: 1,
USD: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE,
USDFix: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE,
EUR: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE,
CAD: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE,
JPY: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE,
GBP: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
;
...
handleCurrencyInput = event =>
const name, value = event.target;
const newState = ;
for (const currency in CURRENCIES)
this.setState([currency]: value * (CURRENCIES[name] / CURRENCIES[currency]))
And adding additional currencies is trivial
Adapted render:
render()
return (
<div>
Object.keys(CURRENCIES).map(currency => (
<SingleInput
type="text"
maxLength=12
key=currency
name=currency
placeholder=
value=
onChange=this.handleCurrencyInput
/>
))
</div>
);
Is a dict basically an object? or what is the difference?
â ricardoNava
May 3 at 2:28
1
Yes, just an simple object
â generalhenry
May 3 at 2:31
So I encountered a problem with the way I'm using.map()
onCURRENCIES
if you see the codebox example you can see what I'm talking about, any ideas how to solve this considering that? I know i can useObject.keys(CURRENCIES).map()
but it doesn't let me input decimal points wouldn't know if it's related though, just an observation.
â ricardoNava
May 3 at 3:50
I added an updated render, I'm not sure what decimal point issue you're encountering.
â generalhenry
May 3 at 3:55
So there are two cases: Trying to use a decimal point as in 15.50 and also numbers like 15 triggers some kind of bug on currencies different from MXN. I adapted the sandbox with your code and it replicates the problem codesandbox
â ricardoNava
May 3 at 4:06
 |Â
show 2 more comments
up vote
1
down vote
accepted
In handleCurrencyInput
the formula is always converted = value * inputcurrencyrate / outputcurrencyrate;
The two special cases MXN, and identity (eg USD -> USD) still follow the equation if you set MXN to 1
So if you structure CURRENCIES as a dict instead of an array you can simplify to
const CURRENCIES =
MXN: 1,
USD: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE,
USDFix: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE,
EUR: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE,
CAD: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE,
JPY: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE,
GBP: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
;
...
handleCurrencyInput = event =>
const name, value = event.target;
const newState = ;
for (const currency in CURRENCIES)
this.setState([currency]: value * (CURRENCIES[name] / CURRENCIES[currency]))
And adding additional currencies is trivial
Adapted render:
render()
return (
<div>
Object.keys(CURRENCIES).map(currency => (
<SingleInput
type="text"
maxLength=12
key=currency
name=currency
placeholder=
value=
onChange=this.handleCurrencyInput
/>
))
</div>
);
Is a dict basically an object? or what is the difference?
â ricardoNava
May 3 at 2:28
1
Yes, just an simple object
â generalhenry
May 3 at 2:31
So I encountered a problem with the way I'm using.map()
onCURRENCIES
if you see the codebox example you can see what I'm talking about, any ideas how to solve this considering that? I know i can useObject.keys(CURRENCIES).map()
but it doesn't let me input decimal points wouldn't know if it's related though, just an observation.
â ricardoNava
May 3 at 3:50
I added an updated render, I'm not sure what decimal point issue you're encountering.
â generalhenry
May 3 at 3:55
So there are two cases: Trying to use a decimal point as in 15.50 and also numbers like 15 triggers some kind of bug on currencies different from MXN. I adapted the sandbox with your code and it replicates the problem codesandbox
â ricardoNava
May 3 at 4:06
 |Â
show 2 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
In handleCurrencyInput
the formula is always converted = value * inputcurrencyrate / outputcurrencyrate;
The two special cases MXN, and identity (eg USD -> USD) still follow the equation if you set MXN to 1
So if you structure CURRENCIES as a dict instead of an array you can simplify to
const CURRENCIES =
MXN: 1,
USD: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE,
USDFix: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE,
EUR: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE,
CAD: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE,
JPY: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE,
GBP: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
;
...
handleCurrencyInput = event =>
const name, value = event.target;
const newState = ;
for (const currency in CURRENCIES)
this.setState([currency]: value * (CURRENCIES[name] / CURRENCIES[currency]))
And adding additional currencies is trivial
Adapted render:
render()
return (
<div>
Object.keys(CURRENCIES).map(currency => (
<SingleInput
type="text"
maxLength=12
key=currency
name=currency
placeholder=
value=
onChange=this.handleCurrencyInput
/>
))
</div>
);
In handleCurrencyInput
the formula is always converted = value * inputcurrencyrate / outputcurrencyrate;
The two special cases MXN, and identity (eg USD -> USD) still follow the equation if you set MXN to 1
So if you structure CURRENCIES as a dict instead of an array you can simplify to
const CURRENCIES =
MXN: 1,
USD: fetchedCurrencies[0]["bm:Obs"][0].$.OBS_VALUE,
USDFix: fetchedCurrencies[1]["bm:Obs"][0].$.OBS_VALUE,
EUR: fetchedCurrencies[2]["bm:Obs"][0].$.OBS_VALUE,
CAD: fetchedCurrencies[3]["bm:Obs"][0].$.OBS_VALUE,
JPY: fetchedCurrencies[4]["bm:Obs"][0].$.OBS_VALUE,
GBP: fetchedCurrencies[5]["bm:Obs"][0].$.OBS_VALUE
;
...
handleCurrencyInput = event =>
const name, value = event.target;
const newState = ;
for (const currency in CURRENCIES)
this.setState([currency]: value * (CURRENCIES[name] / CURRENCIES[currency]))
And adding additional currencies is trivial
Adapted render:
render()
return (
<div>
Object.keys(CURRENCIES).map(currency => (
<SingleInput
type="text"
maxLength=12
key=currency
name=currency
placeholder=
value=
onChange=this.handleCurrencyInput
/>
))
</div>
);
edited May 3 at 17:29
ricardoNava
325
325
answered May 1 at 2:33
generalhenry
24616
24616
Is a dict basically an object? or what is the difference?
â ricardoNava
May 3 at 2:28
1
Yes, just an simple object
â generalhenry
May 3 at 2:31
So I encountered a problem with the way I'm using.map()
onCURRENCIES
if you see the codebox example you can see what I'm talking about, any ideas how to solve this considering that? I know i can useObject.keys(CURRENCIES).map()
but it doesn't let me input decimal points wouldn't know if it's related though, just an observation.
â ricardoNava
May 3 at 3:50
I added an updated render, I'm not sure what decimal point issue you're encountering.
â generalhenry
May 3 at 3:55
So there are two cases: Trying to use a decimal point as in 15.50 and also numbers like 15 triggers some kind of bug on currencies different from MXN. I adapted the sandbox with your code and it replicates the problem codesandbox
â ricardoNava
May 3 at 4:06
 |Â
show 2 more comments
Is a dict basically an object? or what is the difference?
â ricardoNava
May 3 at 2:28
1
Yes, just an simple object
â generalhenry
May 3 at 2:31
So I encountered a problem with the way I'm using.map()
onCURRENCIES
if you see the codebox example you can see what I'm talking about, any ideas how to solve this considering that? I know i can useObject.keys(CURRENCIES).map()
but it doesn't let me input decimal points wouldn't know if it's related though, just an observation.
â ricardoNava
May 3 at 3:50
I added an updated render, I'm not sure what decimal point issue you're encountering.
â generalhenry
May 3 at 3:55
So there are two cases: Trying to use a decimal point as in 15.50 and also numbers like 15 triggers some kind of bug on currencies different from MXN. I adapted the sandbox with your code and it replicates the problem codesandbox
â ricardoNava
May 3 at 4:06
Is a dict basically an object? or what is the difference?
â ricardoNava
May 3 at 2:28
Is a dict basically an object? or what is the difference?
â ricardoNava
May 3 at 2:28
1
1
Yes, just an simple object
â generalhenry
May 3 at 2:31
Yes, just an simple object
â generalhenry
May 3 at 2:31
So I encountered a problem with the way I'm using
.map()
on CURRENCIES
if you see the codebox example you can see what I'm talking about, any ideas how to solve this considering that? I know i can use Object.keys(CURRENCIES).map()
but it doesn't let me input decimal points wouldn't know if it's related though, just an observation.â ricardoNava
May 3 at 3:50
So I encountered a problem with the way I'm using
.map()
on CURRENCIES
if you see the codebox example you can see what I'm talking about, any ideas how to solve this considering that? I know i can use Object.keys(CURRENCIES).map()
but it doesn't let me input decimal points wouldn't know if it's related though, just an observation.â ricardoNava
May 3 at 3:50
I added an updated render, I'm not sure what decimal point issue you're encountering.
â generalhenry
May 3 at 3:55
I added an updated render, I'm not sure what decimal point issue you're encountering.
â generalhenry
May 3 at 3:55
So there are two cases: Trying to use a decimal point as in 15.50 and also numbers like 15 triggers some kind of bug on currencies different from MXN. I adapted the sandbox with your code and it replicates the problem codesandbox
â ricardoNava
May 3 at 4:06
So there are two cases: Trying to use a decimal point as in 15.50 and also numbers like 15 triggers some kind of bug on currencies different from MXN. I adapted the sandbox with your code and it replicates the problem codesandbox
â ricardoNava
May 3 at 4:06
 |Â
show 2 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192258%2fcurrency-exchange-app%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password