import React, { Component } from 'react'; import { Alert, AlertActionCloseButton, Form, FormGroup, FormSelect, FormSelectOption, Button, TextContent, Text, TextInput, TextVariants, Flex, FlexItem, } from '@patternfly/react-core'; import Spinner from './Loading'; import FetchUtils from './FetchUtils' class CurrencyPicker extends Component { constructor(props) { super(props); this.state = { loading: false, currencies: [], src: 'Loading currencies', target: 'Loading currencies', exchangeData: '', requestExchangeData: false, inputValue: 1, inputValid: true, error: { isActive: false, } }; } componentDidMount() { this.setState({ loading: true }) this.getCurrencies() } getCurrencies = () => { FetchUtils.fetchWithRetry(`http://${process.env.REACT_APP_GW_ENDPOINT}/currencies`) .then(currencies => currencies.json()) .then(currencies => this.setState({ currencies, src: currencies[0], target: currencies[1], loading: false })) .catch(err => { console.log(err); this.setState({ error: { isActive: true, header: "Fetching currencies failed", message: `Got the following error trying to fetch currencies: ${err}`, } }) }); } onChangeSrc = (src) => { this.setState({ src, requestExchangeData: false }); }; onChangeTarget = (target) => { this.setState({ target, requestExchangeData: false }); }; onChangeInput = (inputValue) => { const inputValid = this.inputValidation(inputValue); this.setState({ inputValue, inputValid }); } inputValidation = (input) => { return input > 0 } submit = (e) => { e.preventDefault(); this.setState({ requestExchangeData: true }) const payload = { source: this.state.src, target: this.state.target } FetchUtils.fetchWithRetry(`http://${process.env.REACT_APP_GW_ENDPOINT}/exchangeRate/singleCurrency`, { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }) .then(exchangeData => exchangeData.json().then(exchangeData => this.setState({exchangeData}))) .catch(err => { console.log(err) this.setState({ error: { isActive: true, header: "Fetching exchange rate failed", message: `Got the following error trying to fetch currencies: ${err}`, }, requestExchangeData: false }) }) }; closeAlert = () => { this.setState({ error: { isActive: false, } }) } render() { const { exchangeData, requestExchangeData, error } = this.state; return ( {error.isActive && }> {error.message} } Single Currency Exchange
{this.state.loading ? : this.state.currencies.map((curr, index) => ( )) } {this.state.loading ? : this.state.currencies.map((curr, index) => ( )) }
{this.displayConversion() &&
{exchangeData.sign} {exchangeData.value * this.state.inputValue}
} {requestExchangeData && !exchangeData && }
) } displayConversion() { const {requestExchangeData, exchangeData, src, target, inputValue} = this.state; return requestExchangeData && exchangeData && (src !== target) && this.inputValidation(inputValue); } isLoadingOrCurrenciesEqual() { const {loading, src, target, inputValue} = this.state; return loading || (src === target) || (!this.inputValidation(inputValue)); } }; export default CurrencyPicker;