BlueWallet/screen/lnd/lndCreateInvoice.js

125 lines
4.2 KiB
JavaScript
Raw Normal View History

/* global alert */
2018-12-25 17:34:51 +01:00
import React, { Component } from 'react';
import { ActivityIndicator, View, TextInput, KeyboardAvoidingView, Keyboard, TouchableWithoutFeedback, Text } from 'react-native';
import { BlueNavigationStyle, BlueButton, BlueBitcoinAmount } from '../../BlueComponents';
2018-12-25 17:34:51 +01:00
import PropTypes from 'prop-types';
import { BitcoinUnit } from '../../models/bitcoinUnits';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
let EV = require('../../events');
2018-12-25 17:34:51 +01:00
let loc = require('../../loc');
export default class LNDCreateInvoice extends Component {
static navigationOptions = ({ navigation }) => ({
...BlueNavigationStyle(navigation, true),
title: loc.receive.header,
});
constructor(props) {
super(props);
// fallback to first wallet if it exists
const fromWallet = props.navigation.getParam('fromWallet');
this.state = {
fromWallet,
description: '',
2018-12-25 17:34:51 +01:00
isLoading: false,
};
}
async createInvoice() {
this.setState({ isLoading: true }, async () => {
try {
const invoiceRequest = await this.state.fromWallet.addInvoice(this.state.amount, this.state.description);
EV(EV.enum.TRANSACTIONS_COUNT_CHANGED);
2018-12-25 17:34:51 +01:00
ReactNativeHapticFeedback.trigger('notificationSuccess', false);
this.props.navigation.navigate('LNDViewInvoice', {
invoice: invoiceRequest,
fromWallet: this.state.fromWallet,
isModal: true,
2018-12-25 17:34:51 +01:00
});
} catch (_error) {
ReactNativeHapticFeedback.trigger('notificationError', false);
this.setState({ isLoading: false });
alert('Error');
2018-12-25 17:34:51 +01:00
}
});
}
renderCreateButton = () => {
return (
<View style={{ marginHorizontal: 56, marginVertical: 16, minHeight: 45, alignContent: 'center', backgroundColor: '#FFFFFF' }}>
2018-12-25 17:34:51 +01:00
{this.state.isLoading ? (
<ActivityIndicator />
) : (
2019-01-24 20:31:30 +01:00
<BlueButton disabled={!this.state.amount > 0} onPress={() => this.createInvoice()} title={loc.send.details.create} />
2018-12-25 17:34:51 +01:00
)}
</View>
);
};
render() {
if (!this.state.fromWallet) {
return (
<View style={{ flex: 1, paddingTop: 20 }}>
<Text>System error: Source wallet not found (this should never happen)</Text>
</View>
);
}
return (
<TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}>
<View style={{ flex: 1, justifyContent: 'space-between' }}>
<View style={{ flex: 1, backgroundColor: '#FFFFFF' }}>
<KeyboardAvoidingView behavior="position">
<BlueBitcoinAmount
isLoading={this.state.isLoading}
amount={this.state.amount}
onChangeText={text => {
this.setState({ amount: text });
}}
disabled={this.state.isLoading}
unit={BitcoinUnit.SATS}
/>
2018-12-25 17:34:51 +01:00
<View
style={{
flexDirection: 'row',
borderColor: '#d2d2d2',
borderBottomColor: '#d2d2d2',
borderWidth: 1.0,
borderBottomWidth: 0.5,
backgroundColor: '#f5f5f5',
minHeight: 44,
height: 44,
marginHorizontal: 20,
alignItems: 'center',
marginVertical: 8,
borderRadius: 4,
}}
>
<TextInput
onChangeText={text => this.setState({ description: text })}
placeholder={loc.receive.details.label}
value={this.state.description}
2018-12-25 17:34:51 +01:00
numberOfLines={1}
style={{ flex: 1, marginHorizontal: 8, minHeight: 33 }}
editable={!this.state.isLoading}
onSubmitEditing={Keyboard.dismiss}
2018-12-25 17:34:51 +01:00
/>
</View>
{this.renderCreateButton()}
</KeyboardAvoidingView>
</View>
</View>
</TouchableWithoutFeedback>
);
}
}
LNDCreateInvoice.propTypes = {
navigation: PropTypes.shape({
goBack: PropTypes.func,
2018-12-25 17:34:51 +01:00
navigate: PropTypes.func,
getParam: PropTypes.func,
}),
};