BlueWallet/screen/lnd/lnurlPaySuccess.js

184 lines
5.2 KiB
JavaScript
Raw Normal View History

2020-07-23 20:06:13 +02:00
import React, { Component } from 'react';
2020-12-25 17:09:53 +01:00
import PropTypes from 'prop-types';
2020-07-23 20:06:13 +02:00
import { View, Text, Linking, StyleSheet, Image, ScrollView } from 'react-native';
2020-12-16 04:15:57 +01:00
import AsyncStorage from '@react-native-async-storage/async-storage';
2023-11-15 14:05:51 +01:00
import { BlueButtonLink, BlueCard, BlueLoading, BlueSpacing20, BlueSpacing40, BlueText, SafeBlueArea } from '../../BlueComponents';
2020-12-25 17:09:53 +01:00
import navigationStyle from '../../components/navigationStyle';
2020-07-23 20:06:13 +02:00
import Lnurl from '../../class/lnurl';
import loc from '../../loc';
2022-02-23 17:13:20 +01:00
import { SuccessView } from '../send/success';
2023-11-15 13:54:04 +01:00
import Button from '../../components/Button';
2020-07-23 20:06:13 +02:00
export default class LnurlPaySuccess extends Component {
constructor(props) {
super(props);
const paymentHash = props.route.params.paymentHash;
const fromWalletID = props.route.params.fromWalletID;
const justPaid = !!props.route.params.justPaid;
this.state = {
paymentHash,
isLoading: true,
fromWalletID,
justPaid,
};
}
async componentDidMount() {
const LN = new Lnurl(false, AsyncStorage);
await LN.loadSuccessfulPayment(this.state.paymentHash);
const successAction = LN.getSuccessAction();
if (!successAction) {
this.setState({ isLoading: false, LN });
return;
}
const newState = { LN, isLoading: false };
switch (successAction.tag) {
case 'aes': {
const preimage = LN.getPreimage();
newState.message = Lnurl.decipherAES(successAction.ciphertext, preimage, successAction.iv);
newState.preamble = successAction.description;
break;
}
case 'url':
newState.url = successAction.url;
newState.preamble = successAction.description;
break;
case 'message':
this.setState({ message: successAction.message });
newState.message = successAction.message;
break;
}
this.setState(newState);
}
render() {
if (this.state.isLoading || !this.state.LN) {
return <BlueLoading />;
}
/** @type {Lnurl} */
const LN = this.state.LN;
const domain = LN.getDomain();
const repeatable = !LN.getDisposable();
const lnurl = LN.getLnurl();
const description = LN.getDescription();
const image = LN.getImage();
const { preamble, message, url, justPaid } = this.state;
return (
<SafeBlueArea style={styles.root}>
<ScrollView style={styles.container}>
2022-04-09 19:30:31 +02:00
{justPaid && <SuccessView />}
2020-07-23 20:06:13 +02:00
2022-04-09 19:30:31 +02:00
<BlueSpacing40 />
2020-07-23 20:06:13 +02:00
<BlueText style={styles.alignSelfCenter}>{domain}</BlueText>
<BlueText style={[styles.alignSelfCenter, styles.description]}>{description}</BlueText>
2020-07-23 20:06:13 +02:00
{image && <Image style={styles.img} source={{ uri: image }} />}
<BlueSpacing20 />
{(preamble || url || message) && (
<BlueCard>
<View style={styles.successContainer}>
<Text style={styles.successText}>{preamble}</Text>
{url ? (
<BlueButtonLink
title={url}
onPress={() => {
Linking.openURL(url);
}}
/>
) : (
<Text selectable style={{ ...styles.successText, ...styles.successValue }}>
{message}
</Text>
)}
</View>
</BlueCard>
)}
<BlueCard>
{repeatable ? (
2023-11-15 13:54:04 +01:00
<Button
2020-07-23 20:06:13 +02:00
onPress={() => {
this.props.navigation.navigate('ScanLndInvoiceRoot', {
screen: 'LnurlPay',
params: {
2022-10-31 13:25:26 +01:00
lnurl,
2021-08-25 07:55:22 +02:00
walletID: this.state.fromWalletID,
2020-07-23 20:06:13 +02:00
},
});
}}
title="repeat" // TODO: translate this
2020-07-23 20:06:13 +02:00
icon={{ name: 'refresh', type: 'font-awesome', color: '#9aa0aa' }}
/>
) : (
2023-11-15 13:54:04 +01:00
<Button
2020-07-23 20:06:13 +02:00
onPress={() => {
2023-11-11 12:33:50 +01:00
this.props.navigation.getParent().popToTop();
2020-07-23 20:06:13 +02:00
}}
title={loc.send.success_done}
/>
)}
</BlueCard>
</ScrollView>
</SafeBlueArea>
);
}
}
LnurlPaySuccess.propTypes = {
navigation: PropTypes.shape({
navigate: PropTypes.func,
pop: PropTypes.func,
2023-11-11 12:33:50 +01:00
getParent: PropTypes.func,
2020-07-23 20:06:13 +02:00
}),
route: PropTypes.shape({
name: PropTypes.string,
params: PropTypes.shape({
paymentHash: PropTypes.string.isRequired,
fromWalletID: PropTypes.string.isRequired,
justPaid: PropTypes.bool.isRequired,
}),
}),
};
const styles = StyleSheet.create({
img: { width: 200, height: 200, alignSelf: 'center' },
alignSelfCenter: {
alignSelf: 'center',
},
root: {
padding: 0,
2020-07-23 20:06:13 +02:00
},
container: {
paddingHorizontal: 16,
},
2020-07-23 20:06:13 +02:00
successContainer: {
marginTop: 10,
},
successText: {
textAlign: 'center',
margin: 4,
},
successValue: {
fontWeight: 'bold',
},
description: {
marginTop: 20,
},
2020-07-23 20:06:13 +02:00
});
2020-12-25 17:09:53 +01:00
LnurlPaySuccess.navigationOptions = navigationStyle({
title: '',
closeButton: true,
2023-11-11 12:33:50 +01:00
headerBackVisible: false,
2022-04-09 19:30:31 +02:00
gestureEnabled: false,
2023-11-11 12:33:50 +01:00
closeButtonFunc: ({ navigation }) => navigation.getParent().popToTop(),
2020-12-25 17:09:53 +01:00
});