2018-05-03 20:58:31 +02:00
|
|
|
// @flow
|
|
|
|
import React from 'react';
|
|
|
|
import { regexAddress } from 'lbry-redux';
|
|
|
|
import { TextInput, Text, View } from 'react-native';
|
|
|
|
import Button from '../button';
|
|
|
|
import walletStyle from '../../styles/wallet';
|
|
|
|
|
|
|
|
type DraftTransaction = {
|
|
|
|
address: string,
|
|
|
|
amount: ?number, // So we can use a placeholder in the input
|
|
|
|
};
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
sendToAddress: (string, number) => void,
|
|
|
|
balance: number,
|
|
|
|
};
|
|
|
|
|
|
|
|
class WalletSend extends React.PureComponent<Props> {
|
2018-06-04 18:34:03 +02:00
|
|
|
amountInput = null;
|
2018-08-08 17:22:56 +02:00
|
|
|
|
2018-05-03 20:58:31 +02:00
|
|
|
state = {
|
|
|
|
amount: null,
|
2018-06-04 18:34:03 +02:00
|
|
|
address: null,
|
|
|
|
addressChanged: false,
|
|
|
|
addressValid: false
|
2018-05-03 20:58:31 +02:00
|
|
|
};
|
2018-08-08 17:22:56 +02:00
|
|
|
|
2018-05-03 20:58:31 +02:00
|
|
|
componentWillUpdate(nextProps) {
|
|
|
|
const { draftTransaction, transactionError } = nextProps;
|
|
|
|
if (transactionError && transactionError.trim().length > 0) {
|
|
|
|
this.setState({ address: draftTransaction.address, amount: draftTransaction.amount });
|
|
|
|
}
|
|
|
|
}
|
2018-08-08 17:22:56 +02:00
|
|
|
|
2018-05-03 20:58:31 +02:00
|
|
|
handleSend = () => {
|
|
|
|
const { balance, sendToAddress, notify } = this.props;
|
|
|
|
const { address, amount } = this.state;
|
|
|
|
if (address && !regexAddress.test(address)) {
|
|
|
|
notify({
|
|
|
|
message: 'The recipient address is not a valid LBRY address.',
|
|
|
|
displayType: ['toast']
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2018-08-08 17:22:56 +02:00
|
|
|
|
2018-05-03 20:58:31 +02:00
|
|
|
if (amount > balance) {
|
|
|
|
notify({
|
|
|
|
message: 'Insufficient credits',
|
|
|
|
displayType: ['toast']
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2018-08-08 17:22:56 +02:00
|
|
|
|
2018-05-03 20:58:31 +02:00
|
|
|
if (amount && address) {
|
|
|
|
sendToAddress(address, parseFloat(amount));
|
|
|
|
this.setState({ address: null, amount: null });
|
|
|
|
}
|
|
|
|
}
|
2018-08-08 17:22:56 +02:00
|
|
|
|
2018-06-04 18:34:03 +02:00
|
|
|
handleAddressInputBlur = () => {
|
|
|
|
if (this.state.addressChanged && !this.state.addressValid) {
|
|
|
|
const { notify } = this.props;
|
|
|
|
notify({
|
|
|
|
message: 'The recipient address is not a valid LBRY address.',
|
|
|
|
displayType: ['toast']
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-08-08 17:22:56 +02:00
|
|
|
|
2018-06-04 18:34:03 +02:00
|
|
|
handleAddressInputSubmit = () => {
|
|
|
|
if (this.amountInput) {
|
|
|
|
this.amountInput.focus();
|
|
|
|
}
|
|
|
|
}
|
2018-05-03 20:58:31 +02:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { balance } = this.props;
|
|
|
|
const canSend = this.state.address &&
|
|
|
|
this.state.amount > 0 &&
|
|
|
|
this.state.address.trim().length > 0;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={walletStyle.card}>
|
|
|
|
<Text style={walletStyle.title}>Send Credits</Text>
|
|
|
|
<Text style={walletStyle.text}>Recipient address</Text>
|
2018-06-04 18:34:03 +02:00
|
|
|
<View style={[walletStyle.row, walletStyle.bottomMarginMedium]}>
|
|
|
|
<TextInput onChangeText={value => this.setState({
|
|
|
|
address: value,
|
|
|
|
addressChanged: true,
|
|
|
|
addressValid: (value.trim().length == 0 || regexAddress.test(value))
|
|
|
|
})}
|
|
|
|
onBlur={this.handleAddressInputBlur}
|
|
|
|
onSubmitEditing={this.handleAddressInputSubmit}
|
2018-05-03 20:58:31 +02:00
|
|
|
placeholder={'bbFxRyXXXXXXXXXXXZD8nE7XTLUxYnddTs'}
|
|
|
|
value={this.state.address}
|
2018-06-04 18:34:03 +02:00
|
|
|
returnKeyType={'next'}
|
2018-05-03 20:58:31 +02:00
|
|
|
style={[walletStyle.input, walletStyle.addressInput, walletStyle.bottomMarginMedium]} />
|
2018-06-04 18:34:03 +02:00
|
|
|
</View>
|
|
|
|
<Text style={walletStyle.text}>Amount</Text>
|
|
|
|
<View style={walletStyle.row}>
|
|
|
|
<View style={walletStyle.amountRow}>
|
|
|
|
<TextInput ref={ref => this.amountInput = ref}
|
|
|
|
onChangeText={value => this.setState({amount: value})}
|
|
|
|
keyboardType={'numeric'}
|
|
|
|
value={this.state.amount}
|
|
|
|
style={[walletStyle.input, walletStyle.amountInput]} />
|
|
|
|
<Text style={[walletStyle.text, walletStyle.currency]}>LBC</Text>
|
|
|
|
</View>
|
2018-05-03 20:58:31 +02:00
|
|
|
<Button text={'Send'}
|
|
|
|
style={[walletStyle.button, walletStyle.sendButton]}
|
|
|
|
disabled={!canSend}
|
|
|
|
onPress={this.handleSend} />
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default WalletSend;
|