Advertisement
Guest User

Exchange.jsx

a guest
Jan 18th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import React, { useEffect, useState } from "react";
  2. import axios from "axios";
  3.  
  4. const Exchange = () => {
  5. const [data, setData] = useState({});
  6. const [pln, setPln] = useState("");
  7. const [currency, setCurrency] = useState("USD");
  8. const [currencies, setCurrencies] = useState([]);
  9. const [result, setResult] = useState("");
  10.  
  11. useEffect(() => {
  12. getValues();
  13. }, []);
  14. const getValues = async () => {
  15.  
  16. const fetchApi = "https://api.exchangerate-api.com/v4/latest/PLN";
  17. await axios.get(fetchApi).then(res => {
  18. setData(res.data.rates);
  19. setCurrencies(Object.keys(res.data.rates));
  20. });
  21. };
  22.  
  23. const convertPlnToCurrency = () => {
  24. let result = pln * data[currency] + ` ${currency}`;
  25. setResult(`${pln} PLN = ${result}`);
  26. };
  27.  
  28. const changePln = e => {
  29. setPln(parseInt(e.target.value));
  30. };
  31.  
  32. const changeCurrency = e => {
  33. setCurrency(e.target.value);
  34. };
  35.  
  36. return (
  37. <div>
  38. <h1>Exchange</h1>
  39. PLN
  40. <input type="text" value={pln} onChange={changePln} />
  41. to
  42. <select value={currency} onChange={changeCurrency}>
  43. {currencies.map(currency => (
  44. <option key={currency} value={currency}>
  45. {currency}
  46. </option>
  47. ))}
  48. </select>
  49. <button onClick={convertPlnToCurrency}>Convert</button>
  50. <div>
  51. <h3>{result ? result : ""}</h3>
  52. </div>
  53. </div>
  54. );
  55. };
  56.  
  57. export default Exchange;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement