BlueWallet/screen/settings/currency.js

109 lines
3.8 KiB
JavaScript
Raw Normal View History

2023-04-11 22:29:36 +02:00
import React, { useState, useContext, useLayoutEffect } from 'react';
import { FlatList, StyleSheet } from 'react-native';
import { useNavigation, useTheme } from '@react-navigation/native';
2020-12-25 17:09:53 +01:00
import navigationStyle from '../../components/navigationStyle';
2021-10-13 20:00:58 +02:00
import { SafeBlueArea, BlueListItem, BlueText, BlueCard, BlueSpacing10 } from '../../BlueComponents';
2021-10-27 06:19:06 +02:00
import { FiatUnit, FiatUnitSource, getFiatRate } from '../../models/fiatUnit';
2020-07-20 15:38:46 +02:00
import loc from '../../loc';
import { BlueStorageContext } from '../../blue_modules/storage-context';
2021-10-13 20:00:58 +02:00
import dayjs from 'dayjs';
2021-10-27 06:19:06 +02:00
import alert from '../../components/Alert';
2021-10-13 20:00:58 +02:00
dayjs.extend(require('dayjs/plugin/calendar'));
2020-06-09 16:08:18 +02:00
const currency = require('../../blue_modules/currency');
2018-12-24 07:14:53 +01:00
const Currency = () => {
const { setPreferredFiatCurrency } = useContext(BlueStorageContext);
const [isSavingNewPreferredCurrency, setIsSavingNewPreferredCurrency] = useState(false);
2023-04-11 22:29:36 +02:00
const [selectedCurrency, setSelectedCurrency] = useState(FiatUnit.USD);
2021-10-13 20:00:58 +02:00
const [currencyRate, setCurrencyRate] = useState({ LastUpdated: null, Rate: null });
2020-07-15 19:32:59 +02:00
const { colors } = useTheme();
2023-04-11 22:29:36 +02:00
const { setOptions } = useNavigation();
const [search, setSearch] = useState('');
const data = Object.values(FiatUnit).filter(item => item.endPointKey.toLowerCase().includes(search.toLowerCase()));
2020-07-15 19:32:59 +02:00
const styles = StyleSheet.create({
flex: {
flex: 1,
backgroundColor: colors.background,
},
});
2018-12-24 07:14:53 +01:00
2021-10-13 20:00:58 +02:00
const fetchCurrency = async () => {
let preferredCurrency = FiatUnit.USD;
try {
preferredCurrency = await currency.getPreferredCurrency();
if (preferredCurrency === null) {
throw Error();
}
2021-10-13 20:00:58 +02:00
setSelectedCurrency(preferredCurrency);
} catch (_error) {
setSelectedCurrency(preferredCurrency);
}
2021-10-13 21:46:47 +02:00
const mostRecentFetchedRate = await currency.mostRecentFetchedRate();
setCurrencyRate(mostRecentFetchedRate);
2021-10-13 20:00:58 +02:00
};
2023-04-11 22:29:36 +02:00
useLayoutEffect(() => {
setOptions({
searchBar: {
onChangeText: event => setSearch(event.nativeEvent.text),
},
});
fetchCurrency();
2023-04-11 22:29:36 +02:00
}, [setOptions]);
2018-12-24 07:14:53 +01:00
return (
2023-04-11 22:29:36 +02:00
<SafeBlueArea>
<FlatList
style={styles.flex}
keyExtractor={(_item, index) => `${index}`}
data={data}
initialNumToRender={50}
extraData={data}
renderItem={({ item }) => {
return (
<BlueListItem
disabled={isSavingNewPreferredCurrency}
title={`${item.endPointKey} (${item.symbol})`}
checkmark={selectedCurrency.endPointKey === item.endPointKey}
onPress={async () => {
setIsSavingNewPreferredCurrency(true);
try {
await getFiatRate(item.endPointKey);
await currency.setPrefferedCurrency(item);
await currency.init(true);
await fetchCurrency();
setSelectedCurrency(item);
setPreferredFiatCurrency();
} catch (error) {
console.log(error);
alert(loc.settings.currency_fetch_error);
} finally {
setIsSavingNewPreferredCurrency(false);
}
}}
/>
);
}}
/>
<BlueCard>
<BlueText>
{loc.settings.currency_source} {selectedCurrency?.source ?? FiatUnitSource.CoinDesk}
</BlueText>
<BlueSpacing10 />
<BlueText>
{loc.settings.rate}: {currencyRate.Rate ?? loc._.never}
</BlueText>
<BlueSpacing10 />
<BlueText>
{loc.settings.last_updated}: {dayjs(currencyRate.LastUpdated).calendar() ?? loc._.never}
</BlueText>
</BlueCard>
</SafeBlueArea>
);
};
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;