BlueWallet/screen/transactions/list.js

156 lines
4.6 KiB
JavaScript
Raw Normal View History

2018-01-30 22:42:38 +00:00
import React, { Component } from 'react';
2018-09-16 02:24:45 -04:00
import { FlatList } from 'react-native';
2018-01-30 22:42:38 +00:00
import Ionicons from 'react-native-vector-icons/Ionicons';
2018-03-18 02:48:23 +00:00
import { Header, Icon } from 'react-native-elements';
2018-07-07 14:04:32 +01:00
import { BlueLoading, BlueList, SafeBlueArea, BlueCard, BlueText, BlueListItem } from '../../BlueComponents';
2018-03-18 02:48:23 +00:00
import PropTypes from 'prop-types';
2018-05-28 20:18:11 +01:00
let loc = require('../../loc');
2018-03-17 22:39:21 +02:00
let EV = require('../../events');
2018-03-18 02:48:23 +00:00
/** @type {AppStorage} */
let BlueApp = require('../../BlueApp');
2018-03-17 22:39:21 +02:00
export default class TransactionsList extends Component {
2018-01-30 22:42:38 +00:00
static navigationOptions = {
2018-05-28 20:18:11 +01:00
tabBarLabel: loc.transactions.list.tabBarLabel,
2018-01-30 22:42:38 +00:00
tabBarIcon: ({ tintColor, focused }) => (
2018-07-07 14:04:32 +01:00
<Ionicons name={focused ? 'ios-list-box' : 'ios-list-box-outline'} size={26} style={{ color: tintColor }} />
2018-01-30 22:42:38 +00:00
),
2018-03-17 22:39:21 +02:00
};
2018-01-30 22:42:38 +00:00
constructor(props) {
super(props);
this.state = {
isLoading: true,
2018-03-17 22:39:21 +02:00
};
2018-01-30 22:42:38 +00:00
2018-03-17 22:39:21 +02:00
EV(EV.enum.TRANSACTIONS_COUNT_CHANGED, this.refreshFunction.bind(this));
2018-01-30 22:42:38 +00:00
}
async componentDidMount() {
2018-03-17 22:39:21 +02:00
console.log('transaction/list- componentDidMount');
this.refreshFunction();
2018-01-30 22:42:38 +00:00
} // end
2018-03-17 22:39:21 +02:00
refreshFunction() {
this.setState(
{
isLoading: true,
},
() => {
setTimeout(() => {
this.setState({
isLoading: false,
final_balance: BlueApp.getBalance(),
2018-09-16 02:24:45 -04:00
dataSource: BlueApp.getTransactions(),
2018-03-17 22:39:21 +02:00
});
}, 1);
},
);
2018-01-30 22:42:38 +00:00
}
2018-03-17 22:39:21 +02:00
txMemo(hash) {
if (BlueApp.tx_metadata[hash] && BlueApp.tx_metadata[hash]['memo']) {
2018-02-24 15:15:03 +00:00
return ' | ' + BlueApp.tx_metadata[hash]['memo'];
}
return '';
}
2018-01-30 22:42:38 +00:00
refresh() {
2018-03-17 22:39:21 +02:00
this.setState(
{
isLoading: true,
},
async function() {
let that = this;
setTimeout(async function() {
// more responsive
let noErr = true;
try {
await BlueApp.fetchWalletTransactions();
await BlueApp.fetchWalletBalances();
} catch (err) {
noErr = false;
console.warn(err);
}
if (noErr) await BlueApp.saveToDisk(); // caching
EV(EV.enum.WALLETS_COUNT_CHANGED); // TODO: some other event type?
that.setState({
isLoading: false,
final_balance: BlueApp.getBalance(),
2018-09-16 02:24:45 -04:00
dataSource: BlueApp.getTransactions(),
2018-03-17 22:39:21 +02:00
});
}, 10);
},
);
2018-01-30 22:42:38 +00:00
}
render() {
2018-03-17 22:39:21 +02:00
const { navigate } = this.props.navigation;
2018-01-30 22:42:38 +00:00
if (this.state.isLoading) {
2018-03-17 22:39:21 +02:00
return <BlueLoading />;
2018-01-30 22:42:38 +00:00
}
return (
2018-03-17 22:39:21 +02:00
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={{ flex: 1 }}>
2018-01-30 22:42:38 +00:00
<Header
backgroundColor={BlueApp.settings.brandingColor}
2018-03-17 22:39:21 +02:00
centerComponent={{
text: this.state.final_balance + ' BTC',
2018-05-22 18:10:53 +01:00
style: { color: BlueApp.settings.foregroundColor, fontSize: 25 },
2018-03-17 22:39:21 +02:00
}}
rightComponent={<Icon name="refresh" color={BlueApp.settings.foregroundColor} onPress={() => this.refresh()} />}
2018-01-30 22:42:38 +00:00
/>
2018-05-28 20:18:11 +01:00
<BlueCard title={loc.transactions.list.title}>
2018-07-07 14:04:32 +01:00
<BlueText style={{ marginBottom: 10 }}>{loc.transactions.list.description}</BlueText>
2018-01-30 22:42:38 +00:00
2018-03-17 22:39:21 +02:00
<BlueList>
2018-09-16 02:24:45 -04:00
<FlatList
data={this.state.dataSource}
extraData={this.state.dataSource}
renderItem={rowData => {
2018-03-17 22:39:21 +02:00
return (
<BlueListItem
avatar={
<Icon
color={(() => {
2018-07-07 14:04:32 +01:00
return (rowData.confirmations && ((rowData.value < 0 && '#900') || '#080')) || '#ebebeb';
2018-03-17 22:39:21 +02:00
})()}
name={(() => {
2018-07-07 14:04:32 +01:00
return (rowData.value < 0 && 'call-made') || 'call-received';
2018-03-17 22:39:21 +02:00
})()}
/>
}
2018-07-07 14:04:32 +01:00
title={rowData.value / 100000000 + ' BTC' + this.txMemo(rowData.hash)}
2018-03-17 22:39:21 +02:00
subtitle={
rowData.received
.replace(['T'], ' ')
.replace(['Z'], ' ')
.split('.')[0] +
2018-05-28 20:18:11 +01:00
' | ' +
loc.transactions.list.conf +
': ' +
2018-03-17 22:39:21 +02:00
rowData.confirmations +
'\nYOLO'
}
onPress={() => {
navigate('TransactionDetails', { hash: rowData.hash });
}}
/>
);
}}
/>
</BlueList>
2018-01-30 22:42:38 +00:00
</BlueCard>
</SafeBlueArea>
);
}
}
2018-03-18 02:48:23 +00:00
TransactionsList.propTypes = {
navigation: PropTypes.shape({
navigate: PropTypes.func,
}),
};