Pablo Solar VilariƱo
2020-06-19 49a69db8fa634d24fc03f35cb8589e87c260fa1f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import React, { Component } from 'react';
import {
    Alert, 
    AlertActionCloseButton,
    Form,
    FormGroup,
    FormSelect,
    FormSelectOption,
    Button,
    TextContent,
    Text,
    Flex,
    FlexItem,
} from '@patternfly/react-core';
 
import Spinner from './Loading';
import RenderedChart from './Graph'
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 });
    };
 
    onChangeTarget = (target) => {
        this.setState({ target });
    };
 
    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/historicalData`, {
            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 (
            <React.Fragment>
                 {error.isActive && 
                    <Alert
                        className="popup"
                        variant="danger"
                        title={error.header}
                        action={<AlertActionCloseButton onClose={this.closeAlert} />}>
                {error.message}
              </Alert>}
                <TextContent>
                    <Text component="h1" className="centered">
                        <b>Historical Currency Data</b>
                    </Text>
                </TextContent>
                <Form onSubmit={this.submit} >
                    <Flex>
                        <FlexItem>
                            <FormGroup
                                isInline={true}
                                label="Source currency"
                                fieldId="source_group"
                            >
                                <FormSelect
                                    value={this.state.src}
                                    onChange={this.onChangeSrc}
                                    id="src"
                                    aria-label="FormSelect Input"
                                >
 
                                    {this.state.loading
                                        ? <FormSelectOption isDisabled={true} label="Loading currencies" />
                                        : this.state.currencies.map((curr, index) => (
                                            <FormSelectOption key={index} value={curr} label={curr} />
                                        ))
                                    }
                                </FormSelect>
                            </FormGroup>
                        </FlexItem>
                        <FlexItem>
                            <FormGroup
                                label="Target currency"
                                isInline={true}
                                fieldId="target_group"
                            >
                                <FormSelect
                                    value={this.state.target}
                                    onChange={this.onChangeTarget}
                                    id="target"
                                    aria-label="FormSelect Input"
                                >
                                    {this.state.loading
                                        ? <FormSelectOption isDisabled={true} label="Loading currencies" />
                                        : this.state.currencies.map((curr, index) => (
                                            <FormSelectOption key={index} value={curr} label={curr} />
                                        ))
                                    }
                                </FormSelect>
                            </FormGroup>
                        </FlexItem>
                    </Flex>
                    <span>
                        <Button isDisabled={this.state.loading || this.state.src === this.state.target} type="submit" variant="primary">Submit</Button>
                    </span>
                </Form>
                {requestExchangeData && exchangeData && <RenderedChart data={exchangeData} target={this.state.target} amount={this.state.amount} />}
                {requestExchangeData && !exchangeData && <Spinner />}
            </React.Fragment>
        )
    }
};
 
export default CurrencyPicker;