You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
7.3 KiB

7 years ago
import React, { Component } from 'react';
import { ActivityIndicator, View, Dimensions } from 'react-native';
7 years ago
import { Text, FormValidationMessage } from 'react-native-elements';
7 years ago
import {
BlueSpacing20,
BlueSpacing40,
7 years ago
BlueButton,
SafeBlueArea,
BlueCard,
BlueText,
BlueFormInput,
BlueSpacing,
BlueFormInputAddress,
7 years ago
} from '../../BlueComponents';
7 years ago
import PropTypes from 'prop-types';
7 years ago
const bip21 = require('bip21');
7 years ago
let EV = require('../../events');
7 years ago
let BigNumber = require('bignumber.js');
7 years ago
let BlueApp = require('../../BlueApp');
let loc = require('../../loc');
const { height, width } = Dimensions.get('window');
const aspectRatio = height / width;
let isIpad;
if (aspectRatio > 1.6) {
isIpad = false;
} else {
isIpad = true;
}
7 years ago
7 years ago
const btcAddressRx = /^[a-zA-Z0-9]{26,35}$/;
7 years ago
7 years ago
export default class SendDetails extends Component {
static navigationOptions = {
6 years ago
tabBarVisible: false,
7 years ago
};
7 years ago
constructor(props) {
super(props);
7 years ago
let startTime = Date.now();
let address;
if (props.navigation.state.params)
address = props.navigation.state.params.address;
let fromAddress;
if (props.navigation.state.params)
fromAddress = props.navigation.state.params.fromAddress;
let fromWallet = {};
let startTime2 = Date.now();
7 years ago
for (let w of BlueApp.getWallets()) {
if (w.getAddress() === fromAddress) {
7 years ago
fromWallet = w;
7 years ago
}
}
7 years ago
let endTime2 = Date.now();
console.log('getAddress() took', (endTime2 - startTime2) / 1000, 'sec');
7 years ago
this.state = {
errorMessage: false,
7 years ago
fromAddress: fromAddress,
fromWallet: fromWallet,
isLoading: true,
7 years ago
address: address,
7 years ago
amount: '',
fee: '',
7 years ago
};
7 years ago
7 years ago
EV(EV.enum.CREATE_TRANSACTION_NEW_DESTINATION_ADDRESS, data => {
console.log('received event with ', data);
7 years ago
if (btcAddressRx.test(data)) {
this.setState({
address: data,
7 years ago
});
7 years ago
} else {
7 years ago
const { address, options } = bip21.decode(data);
7 years ago
if (btcAddressRx.test(address)) {
this.setState({
address,
amount: options.amount,
7 years ago
memo: options.label,
});
7 years ago
}
}
7 years ago
});
let endTime = Date.now();
console.log('constructor took', (endTime - startTime) / 1000, 'sec');
7 years ago
}
async componentDidMount() {
7 years ago
let startTime = Date.now();
console.log('send/details - componentDidMount');
7 years ago
this.setState({
isLoading: false,
7 years ago
});
let endTime = Date.now();
console.log('componentDidMount took', (endTime - startTime) / 1000, 'sec');
7 years ago
}
recalculateAvailableBalance(balance, amount, fee) {
7 years ago
if (!amount) amount = 0;
if (!fee) fee = 0;
let availableBalance;
7 years ago
try {
7 years ago
availableBalance = new BigNumber(balance);
availableBalance = availableBalance.sub(amount);
availableBalance = availableBalance.sub(fee);
availableBalance = availableBalance.toString(10);
7 years ago
} catch (err) {
7 years ago
return balance;
7 years ago
}
7 years ago
console.log(typeof availableBalance, availableBalance);
return (availableBalance === 'NaN' && balance) || availableBalance;
7 years ago
}
createTransaction() {
if (!this.state.amount || this.state.amount === '0') {
this.setState({
errorMessage: loc.send.details.amount_fiels_is_not_valid,
7 years ago
});
console.log('validation error');
return;
}
if (!this.state.fee) {
this.setState({
errorMessage: loc.send.details.fee_fiels_is_not_valid,
7 years ago
});
console.log('validation error');
return;
}
if (!this.state.address) {
this.setState({
errorMessage: loc.send.details.address_fiels_is_not_valid,
7 years ago
});
console.log('validation error');
return;
}
this.setState({
7 years ago
errorMessage: '',
});
7 years ago
this.props.navigation.navigate('CreateTransaction', {
7 years ago
amount: this.state.amount,
7 years ago
fee: this.state.fee,
address: this.state.address,
memo: this.state.memo,
7 years ago
fromAddress: this.state.fromAddress,
});
7 years ago
}
render() {
if (this.state.isLoading) {
return (
7 years ago
<View style={{ flex: 1, paddingTop: 20 }}>
7 years ago
<ActivityIndicator />
</View>
);
}
if (!this.state.fromWallet.getAddress) {
return (
7 years ago
<View style={{ flex: 1, paddingTop: 20 }}>
<Text>
System error: Source wallet not found (this should never happen)
</Text>
7 years ago
</View>
);
}
return (
7 years ago
<SafeBlueArea style={{ flex: 1, paddingTop: 20 }}>
{(() => {
if (isIpad) {
return <BlueSpacing40 />;
} else {
return <BlueSpacing />;
}
})()}
7 years ago
<BlueCard
title={loc.send.details.title}
7 years ago
style={{ alignItems: 'center', flex: 1 }}
>
<BlueFormInputAddress
7 years ago
style={{ width: 250 }}
onChangeText={text => this.setState({ address: text })}
placeholder={loc.send.details.receiver_placeholder}
7 years ago
value={this.state.address}
7 years ago
/>
7 years ago
<BlueFormInput
onChangeText={text =>
this.setState({ amount: text.replace(',', '.') })
}
7 years ago
keyboardType={'numeric'}
placeholder={loc.send.details.amount_placeholder}
7 years ago
value={this.state.amount + ''}
7 years ago
/>
7 years ago
<BlueFormInput
onChangeText={text =>
this.setState({ fee: text.replace(',', '.') })
}
7 years ago
keyboardType={'numeric'}
placeholder={loc.send.details.fee_placeholder}
7 years ago
value={this.state.fee + ''}
7 years ago
/>
7 years ago
<BlueFormInput
onChangeText={text => this.setState({ memo: text })}
placeholder={loc.send.details.memo_placeholder}
7 years ago
value={this.state.memo}
7 years ago
/>
7 years ago
<BlueSpacing20 />
<BlueText>
{loc.send.details.remaining_balance}:{' '}
7 years ago
{this.recalculateAvailableBalance(
this.state.fromWallet.getBalance(),
this.state.amount,
this.state.fee,
)}{' '}
BTC
</BlueText>
7 years ago
</BlueCard>
<FormValidationMessage>{this.state.errorMessage}</FormValidationMessage>
<View style={{ flex: 1, flexDirection: 'row' }}>
7 years ago
<View style={{ flex: 0.33 }}>
7 years ago
<BlueButton
6 years ago
onPress={() => this.props.navigation.goBack()}
title={loc.send.details.cancel}
7 years ago
/>
</View>
7 years ago
<View style={{ flex: 0.33 }}>
<BlueButton
6 years ago
icon={{
name: 'qrcode',
type: 'font-awesome',
color: BlueApp.settings.buttonTextColor,
}}
7 years ago
style={{}}
title={loc.send.details.scan}
7 years ago
onPress={() => this.props.navigation.navigate('ScanQrAddress')}
7 years ago
/>
</View>
7 years ago
<View style={{ flex: 0.33 }}>
7 years ago
<BlueButton
7 years ago
onPress={() => this.createTransaction()}
title={loc.send.details.create}
7 years ago
/>
</View>
</View>
</SafeBlueArea>
);
}
}
7 years ago
SendDetails.propTypes = {
navigation: PropTypes.shape({
goBack: PropTypes.function,
navigate: PropTypes.func,
state: PropTypes.shape({
params: PropTypes.shape({
address: PropTypes.string,
fromAddress: PropTypes.string,
}),
}),
}),
};