-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRatesList.js
More file actions
79 lines (72 loc) · 1.92 KB
/
RatesList.js
File metadata and controls
79 lines (72 loc) · 1.92 KB
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
import React from 'react';
import { StyleSheet, FlatList , View ,Text} from 'react-native';
import ListItem, { listItem } from './ListItem';
export default class RatesList extends React.Component {
constructor(props) {
super(props);
props.showExchangeRates = this.showExchangeRates;
this.state = {
baseCurrency : this.props.baseCurrency ,
exchangeRates : [] ,
loaded : false
}
}
getRates() {
fetch('https://api.exchangeratesapi.io/latest?base='+this.state.baseCurrency)
.then((response) => response.json())
.then((responseJson) => {
if(responseJson.rates != null) {
let ratesEntries = Object.entries(responseJson.rates);
let exchangeRates = [];
ratesEntries.forEach( (entry,index) => {
exchangeRates.push( { key: entry[0] , value: entry[1] });
});
this.setState({
exchangeRates : exchangeRates ,
loaded : true
});
} else {
this.setState({
loaded : false
})
}
})
.catch((error) => {
console.error(error);
});
}
componentWillReceiveProps(nextProps) {
if(nextProps.baseCurrency !== this.state.value) {
this.setState({ baseCurrency: nextProps.baseCurrency });
}
}
render() {
this.getRates();
if(this.state.loaded)
{
return (
<View style = {styles.container}>
<FlatList
data={this.state.exchangeRates}
renderItem= { ({item}) => <ListItem currency = {item.key} rate = { Number((item.value).toFixed(3))}/>
}
/>
</View>
);
} else {
return(<View/>);
}
}
}
const styles = StyleSheet.create({
container : {
width: '90%',
padding: 2,
} ,
listItemView : {
} ,
listItem : {
fontSize: 18 ,
borderBottomWidth: 1
}
});