BlueWallet/screen/settings/currency.js

98 lines
3.0 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect } from 'react';
import { FlatList, TouchableOpacity, ActivityIndicator, View, StyleSheet } from 'react-native';
2020-07-15 19:32:59 +02:00
import { SafeBlueArea, BlueListItemHooks, BlueTextHooks, BlueCard, BlueNavigationStyle } from '../../BlueComponents';
2018-12-24 07:14:53 +01:00
import PropTypes from 'prop-types';
import { Icon } from 'react-native-elements';
2018-12-24 07:14:53 +01:00
import { FiatUnit } from '../../models/fiatUnit';
2020-07-15 19:32:59 +02:00
import { useTheme } from '@react-navigation/native';
const loc = require('../../loc');
2020-06-09 16:08:18 +02:00
const currency = require('../../blue_modules/currency');
2018-12-24 07:14:53 +01:00
const data = Object.values(FiatUnit);
2018-12-24 07:14:53 +01:00
const Currency = () => {
const [isSavingNewPreferredCurrency, setIsSavingNewPreferredCurrency] = useState(false);
const [selectedCurrency, setSelectedCurrency] = useState(null);
2020-07-15 19:32:59 +02:00
const { colors } = useTheme();
const styles = StyleSheet.create({
flex: {
flex: 1,
backgroundColor: colors.background,
},
activity: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: colors.background,
},
});
2018-12-24 07:14:53 +01:00
useEffect(() => {
const fetchCurrency = async () => {
try {
const preferredCurrency = await currency.getPreferredCurrency();
if (preferredCurrency === null) {
throw Error();
}
setSelectedCurrency(preferredCurrency);
} catch (_error) {
setSelectedCurrency(FiatUnit.USD);
}
};
fetchCurrency();
}, []);
2018-12-24 07:14:53 +01:00
if (selectedCurrency !== null && selectedCurrency !== undefined) {
2018-12-24 07:14:53 +01:00
return (
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={styles.flex}>
<FlatList
style={styles.flex}
keyExtractor={(_item, index) => `${index}`}
data={data}
extraData={data}
renderItem={({ item }) => {
return (
2020-07-15 19:32:59 +02:00
<BlueListItemHooks
disabled={isSavingNewPreferredCurrency}
2020-05-03 20:17:49 +02:00
title={`${item.endPointKey} (${item.symbol})`}
{...(selectedCurrency.endPointKey === item.endPointKey
? { rightIcon: <Icon name="check" type="octaicon" color="#0070FF" /> }
2020-05-03 20:17:49 +02:00
: { hideChevron: true })}
Component={TouchableOpacity}
onPress={async () => {
setIsSavingNewPreferredCurrency(true);
setSelectedCurrency(item);
await currency.setPrefferedCurrency(item);
await currency.startUpdater();
setIsSavingNewPreferredCurrency(false);
}}
2020-05-03 20:17:49 +02:00
/>
);
}}
/>
<BlueCard>
2020-07-15 19:32:59 +02:00
<BlueTextHooks>Prices are obtained from CoinDesk</BlueTextHooks>
</BlueCard>
</SafeBlueArea>
2018-12-24 07:14:53 +01:00
);
}
return (
<View style={styles.activity}>
<ActivityIndicator />
</View>
);
};
2018-12-24 07:14:53 +01:00
Currency.propTypes = {
navigation: PropTypes.shape({
navigate: PropTypes.func,
goBack: PropTypes.func,
}),
};
Currency.navigationOptions = () => ({
...BlueNavigationStyle(),
title: loc.settings.currency,
});
export default Currency;