BlueWallet/screen/settings/currency.js

92 lines
2.9 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect, useContext } from 'react';
2020-11-21 03:47:13 +01:00
import { FlatList, ActivityIndicator, View, StyleSheet } from 'react-native';
2020-12-25 17:09:53 +01:00
import { useTheme } from '@react-navigation/native';
import navigationStyle from '../../components/navigationStyle';
import { SafeBlueArea, BlueListItem, BlueText, BlueCard } from '../../BlueComponents';
2020-11-22 01:16:20 +01:00
import { FiatUnit, FiatUnitSource } from '../../models/fiatUnit';
2020-07-20 15:38:46 +02:00
import loc from '../../loc';
import { BlueStorageContext } from '../../blue_modules/storage-context';
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 { setPreferredFiatCurrency } = useContext(BlueStorageContext);
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>
<FlatList
style={styles.flex}
keyExtractor={(_item, index) => `${index}`}
data={data}
2021-09-13 19:43:26 +02:00
initialNumToRender={50}
extraData={data}
renderItem={({ item }) => {
return (
2020-09-22 03:53:28 +02:00
<BlueListItem
disabled={isSavingNewPreferredCurrency}
2020-05-03 20:17:49 +02:00
title={`${item.endPointKey} (${item.symbol})`}
2020-11-21 03:47:13 +01:00
checkmark={selectedCurrency.endPointKey === item.endPointKey}
onPress={async () => {
setIsSavingNewPreferredCurrency(true);
setSelectedCurrency(item);
await currency.setPrefferedCurrency(item);
2021-09-09 21:36:35 +02:00
await currency.init(true);
setIsSavingNewPreferredCurrency(false);
setPreferredFiatCurrency();
}}
2020-05-03 20:17:49 +02:00
/>
);
}}
/>
<BlueCard>
<BlueText>
2020-11-22 01:16:20 +01:00
{loc.settings.currency_source} {selectedCurrency.source ?? FiatUnitSource.CoinDesk}
</BlueText>
</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
2021-02-15 09:03:54 +01:00
Currency.navigationOptions = navigationStyle({}, opts => ({ ...opts, title: loc.settings.currency }));
2020-12-25 17:09:53 +01:00
export default Currency;