|
| 1 | +import React, { useContext, useEffect, useState } from 'react'; |
| 2 | +import { |
| 3 | + Card, |
| 4 | + Container, FormControl, InputLabel, MenuItem, Select, TextField, |
| 5 | +} from '@material-ui/core'; |
| 6 | +import axios from 'axios'; |
| 7 | +import Grid from '@material-ui/core/Grid'; |
| 8 | +import CardContent from '@material-ui/core/CardContent'; |
| 9 | +import MuiAlert from '@material-ui/lab/Alert'; |
| 10 | +import Snackbar from '@material-ui/core/Snackbar'; |
| 11 | +import { setPageIndex } from '../../../reducers/MainReducer/Actions'; |
| 12 | +import PageHeader from '../../../components/PageHeader'; |
| 13 | +import Layout from '../../../components/Layout'; |
| 14 | +import { MainContext } from '../../../contexts/MainContextProvider'; |
| 15 | +import LoadingBar from '../../../components/LoadingBar'; |
| 16 | + |
| 17 | +const EgldPriceCalculator = () => { |
| 18 | + const [, dispatch] = useContext(MainContext); |
| 19 | + |
| 20 | + const [pricePerEgld, setPricePerEgld] = useState(null); |
| 21 | + const [currency, setCurrency] = useState('usd'); |
| 22 | + const [supportedCurrencies, setSupportedCurrencies] = useState(null); |
| 23 | + |
| 24 | + const [egld, setEgld] = useState(1); |
| 25 | + const [currencyAmount, setCurrencyAmount] = useState(0); |
| 26 | + |
| 27 | + const [loading, setLoading] = useState(false); |
| 28 | + const [error, setError] = useState(null); |
| 29 | + |
| 30 | + /** |
| 31 | + * Close the snackbar |
| 32 | + */ |
| 33 | + const closeSnackbar = () => { |
| 34 | + setError(null); |
| 35 | + }; |
| 36 | + |
| 37 | + /** |
| 38 | + * Get a list of supported currencies |
| 39 | + * @param override True if the loading indicator should be ignored, otherwise false |
| 40 | + */ |
| 41 | + const getSupportedCurrencies = (override) => { |
| 42 | + if (loading && !override) return; |
| 43 | + |
| 44 | + setLoading(true); |
| 45 | + setError(null); |
| 46 | + setSupportedCurrencies(null); |
| 47 | + |
| 48 | + axios.get('https://api.coingecko.com/api/v3/simple/supported_vs_currencies') |
| 49 | + .then((res) => { |
| 50 | + setSupportedCurrencies(res.data); |
| 51 | + }) |
| 52 | + .catch((err) => { |
| 53 | + setError(err); |
| 54 | + }) |
| 55 | + .finally(() => { |
| 56 | + setLoading(false); |
| 57 | + }); |
| 58 | + }; |
| 59 | + |
| 60 | + /** |
| 61 | + * Get the price for EGLD |
| 62 | + * @param override True if the loading indicator should be ignored, otherwise false |
| 63 | + * @param changeEgld Change the EGLD amount or not |
| 64 | + * @param newCurrency The currency for which the price should be retrieved |
| 65 | + */ |
| 66 | + const getEgldPrice = (override, changeEgld, newCurrency) => { |
| 67 | + if (loading && !override) return; |
| 68 | + |
| 69 | + setLoading(true); |
| 70 | + setError(null); |
| 71 | + setPricePerEgld(null); |
| 72 | + |
| 73 | + axios.get(`https://api.coingecko.com/api/v3/simple/price?ids=elrond-erd-2&vs_currencies=${newCurrency}`) |
| 74 | + .then((res) => { |
| 75 | + const values = Object.values(res.data['elrond-erd-2']); |
| 76 | + const ppegld = parseFloat(values[0]); |
| 77 | + |
| 78 | + setPricePerEgld(ppegld); |
| 79 | + if (changeEgld) { |
| 80 | + setEgld((1 / ppegld) * currencyAmount); |
| 81 | + } else { |
| 82 | + setCurrencyAmount(egld * ppegld); |
| 83 | + } |
| 84 | + }) |
| 85 | + .catch((err) => { |
| 86 | + setError(err); |
| 87 | + }) |
| 88 | + .finally(() => { |
| 89 | + setLoading(false); |
| 90 | + }); |
| 91 | + }; |
| 92 | + |
| 93 | + /** |
| 94 | + * Change the EGLD amount |
| 95 | + * @param e |
| 96 | + */ |
| 97 | + const changeEgldAmount = (e) => { |
| 98 | + if (e.target.value < 0) return; |
| 99 | + |
| 100 | + setEgld(e.target.value); |
| 101 | + setCurrencyAmount(e.target.value * pricePerEgld); |
| 102 | + }; |
| 103 | + |
| 104 | + /** |
| 105 | + * Change the amount of the selected currency |
| 106 | + * @param e The event argument |
| 107 | + */ |
| 108 | + const changeCurrencyAmount = (e) => { |
| 109 | + if (e.target.value < 0) return; |
| 110 | + |
| 111 | + setEgld((1 / pricePerEgld) * e.target.value); |
| 112 | + setCurrencyAmount(e.target.value); |
| 113 | + }; |
| 114 | + |
| 115 | + /** |
| 116 | + * Change the currency |
| 117 | + * @param e The event argument |
| 118 | + */ |
| 119 | + const changeCurrency = (e) => { |
| 120 | + if (!e.target.value) return; |
| 121 | + |
| 122 | + setCurrency(e.target.value); |
| 123 | + getEgldPrice(false, false, e.target.value); |
| 124 | + }; |
| 125 | + |
| 126 | + useEffect(() => { |
| 127 | + dispatch(setPageIndex(-1)); |
| 128 | + getSupportedCurrencies(false); |
| 129 | + getEgldPrice(true, false, currency); |
| 130 | + }, []); |
| 131 | + |
| 132 | + return ( |
| 133 | + <Layout> |
| 134 | + <PageHeader title="Elrond (EGLD) Price Calculator" subTitle="Simple Elrond (EGLD) price calculator" /> |
| 135 | + <Container maxWidth="md" style={{ marginTop: 10 }}> |
| 136 | + <Card> |
| 137 | + <CardContent> |
| 138 | + {loading ? <LoadingBar /> : ( |
| 139 | + <Grid container spacing={2}> |
| 140 | + <Grid item xs={12} md={12} lg={12}> |
| 141 | + <Grid container> |
| 142 | + <Grid item xs={12} md={10} lg={10}> |
| 143 | + <TextField |
| 144 | + variant="outlined" |
| 145 | + label="Amount" |
| 146 | + value={egld} |
| 147 | + type="number" |
| 148 | + fullWidth |
| 149 | + onChange={changeEgldAmount} |
| 150 | + /> |
| 151 | + </Grid> |
| 152 | + <Grid item xs={12} md={2} lg={2}> |
| 153 | + <FormControl |
| 154 | + variant="outlined" |
| 155 | + fullWidth |
| 156 | + > |
| 157 | + <InputLabel>Currency</InputLabel> |
| 158 | + <Select |
| 159 | + value="egld" |
| 160 | + label="Currency" |
| 161 | + fullWidth |
| 162 | + > |
| 163 | + <MenuItem value="egld"> |
| 164 | + <em>EGLD</em> |
| 165 | + </MenuItem> |
| 166 | + </Select> |
| 167 | + </FormControl> |
| 168 | + </Grid> |
| 169 | + </Grid> |
| 170 | + </Grid> |
| 171 | + <Grid item xs={12} md={12} lg={12}> |
| 172 | + <Grid container> |
| 173 | + <Grid item xs={12} md={10} lg={10}> |
| 174 | + <TextField |
| 175 | + variant="outlined" |
| 176 | + label="Amount" |
| 177 | + value={currencyAmount} |
| 178 | + type="number" |
| 179 | + fullWidth |
| 180 | + onChange={changeCurrencyAmount} |
| 181 | + /> |
| 182 | + </Grid> |
| 183 | + <Grid item xs={12} md={2} lg={2}> |
| 184 | + <FormControl |
| 185 | + variant="outlined" |
| 186 | + fullWidth |
| 187 | + > |
| 188 | + <InputLabel>Currency</InputLabel> |
| 189 | + <Select |
| 190 | + value={currency} |
| 191 | + label="Currency" |
| 192 | + fullWidth |
| 193 | + onChange={changeCurrency} |
| 194 | + > |
| 195 | + <MenuItem value=""> |
| 196 | + Select a currency |
| 197 | + </MenuItem> |
| 198 | + {supportedCurrencies && supportedCurrencies.length > 0 |
| 199 | + ? supportedCurrencies.map((item) => ( |
| 200 | + <MenuItem |
| 201 | + key={item} |
| 202 | + value={item} |
| 203 | + > |
| 204 | + {item.toUpperCase()} |
| 205 | + </MenuItem> |
| 206 | + )) : null} |
| 207 | + </Select> |
| 208 | + </FormControl> |
| 209 | + </Grid> |
| 210 | + </Grid> |
| 211 | + </Grid> |
| 212 | + </Grid> |
| 213 | + )} |
| 214 | + </CardContent> |
| 215 | + </Card> |
| 216 | + </Container> |
| 217 | + <Snackbar open={!!error} autoHideDuration={6000} onClose={closeSnackbar}> |
| 218 | + {error |
| 219 | + ? ( |
| 220 | + <MuiAlert elevation={6} variant="filled" severity="error" onClose={closeSnackbar}> |
| 221 | + {error.message} |
| 222 | + </MuiAlert> |
| 223 | + ) |
| 224 | + : null} |
| 225 | + </Snackbar> |
| 226 | + </Layout> |
| 227 | + ); |
| 228 | +}; |
| 229 | + |
| 230 | +export default EgldPriceCalculator; |
0 commit comments