BlueWallet/screen/settings/currency.js

91 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-12-24 01:14:53 -05:00
import React, { Component } from 'react';
2018-12-31 21:27:03 +00:00
import { FlatList, TouchableOpacity, ActivityIndicator, View } from 'react-native';
import { SafeBlueArea, BlueNavigationStyle, BlueListItem, BlueText, BlueCard } from '../../BlueComponents';
2018-12-24 01:14:53 -05:00
import PropTypes from 'prop-types';
import { Icon } from 'react-native-elements';
2018-12-24 01:14:53 -05:00
import { FiatUnit } from '../../models/fiatUnit';
let loc = require('../../loc');
let currency = require('../../currency');
export default class Currency extends Component {
static navigationOptions = () => ({
...BlueNavigationStyle(),
title: loc.settings.currency,
});
constructor(props) {
super(props);
this.state = { data: Object.values(FiatUnit), isSavingNewPreferredCurrency: false };
2018-12-24 01:14:53 -05:00
}
async componentDidMount() {
try {
2018-12-31 21:27:03 +00:00
const preferredCurrency = await currency.getPreferredCurrency();
if (preferredCurrency === null) {
throw Error();
}
this.setState({ selectedCurrency: preferredCurrency });
} catch (_error) {
this.setState({ selectedCurrency: FiatUnit.USD });
}
2018-12-24 01:14:53 -05:00
}
renderItem = ({ item }) => {
return (
<TouchableOpacity
onPress={() => {
this.setState({ isSavingNewPreferredCurrency: true, selectedCurrency: item }, async () => {
2018-12-31 21:27:03 +00:00
await currency.setPrefferedCurrency(item);
await currency.startUpdater();
this.setState({ isSavingNewPreferredCurrency: false });
});
2018-12-24 01:14:53 -05:00
}}
>
<BlueListItem
title={item.endPointKey + ' (' + item.symbol + ')'}
2018-12-25 14:42:04 -05:00
{...(this.state.selectedCurrency.endPointKey === item.endPointKey
? {
rightIcon: this.state.selectedNewCurrency ? (
<ActivityIndicator />
) : (
<Icon name="check" type="font-awesome" color="#0c2550" />
),
}
: { hideChevron: true })}
/>
2018-12-24 01:14:53 -05:00
</TouchableOpacity>
);
};
render() {
if (this.state.selectedCurrency !== null && this.state.selectedCurrency !== undefined) {
return (
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={{ flex: 1 }}>
<FlatList
style={{ flex: 1 }}
keyExtractor={(_item, index) => `${index}`}
data={this.state.data}
extraData={this.state.data}
renderItem={this.renderItem}
/>
<BlueCard>
<BlueText>Prices are obtained from CoinDesk</BlueText>
</BlueCard>
</SafeBlueArea>
);
}
2018-12-24 01:14:53 -05:00
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<ActivityIndicator />
</View>
2018-12-24 01:14:53 -05:00
);
}
}
Currency.propTypes = {
navigation: PropTypes.shape({
navigate: PropTypes.func,
goBack: PropTypes.func,
}),
};