lbry-desktop/ui/component/walletSend/view.jsx

224 lines
7.8 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
import * as MODALS from 'constants/modal_types';
2019-06-17 22:32:38 +02:00
import React from 'react';
2018-03-26 23:32:43 +02:00
import Button from 'component/button';
2019-02-13 17:27:20 +01:00
import { Form, FormField } from 'component/common/form';
2018-03-26 23:32:43 +02:00
import { Formik } from 'formik';
import validateSendTx from 'util/form-validation';
2019-09-26 18:07:11 +02:00
import Card from 'component/common/card';
2020-09-02 22:08:37 +02:00
import I18nMessage from 'component/i18nMessage';
import LbcSymbol from 'component/common/lbc-symbol';
import WalletSpendableBalanceHelp from 'component/walletSpendableBalanceHelp';
import classnames from 'classnames';
import ChannelSelector from 'component/channelSelector';
import ClaimPreview from 'component/claimPreview';
2018-03-26 23:32:43 +02:00
type Props = {
openModal: (id: string, { destination: string, amount: string, isAddress: boolean }) => void,
draftTransaction: { address: string, amount: string },
setDraftTransaction: ({ address: string, amount: string }) => void,
2018-03-26 23:32:43 +02:00
balance: number,
isAddress: boolean,
setIsAddress: (boolean) => void,
contentUri: string,
contentError: string,
contentClaim?: StreamClaim,
setEnteredContentUri: (string) => void,
confirmed: boolean,
setConfirmed: (boolean) => void,
sendLabel: string,
setSendLabel: (string) => void,
snack: ?{
linkTarget: ?string,
linkText: ?string,
message: string,
isError: boolean,
},
2018-03-26 23:32:43 +02:00
};
class WalletSend extends React.PureComponent<Props> {
constructor() {
super();
(this: any).handleSubmit = this.handleSubmit.bind(this);
(this: any).handleClear = this.handleClear.bind(this);
2018-03-26 23:32:43 +02:00
}
handleSubmit() {
const { draftTransaction, openModal, isAddress, contentUri, setConfirmed } = this.props;
const destination = isAddress ? draftTransaction.address : contentUri;
const amount = draftTransaction.amount;
const modalProps = { destination, amount, isAddress, setConfirmed };
openModal(MODALS.CONFIRM_TRANSACTION, modalProps);
}
handleClear() {
const { setDraftTransaction, setConfirmed } = this.props;
setDraftTransaction({
address: '',
amount: '',
});
setConfirmed(false);
}
render() {
const {
draftTransaction,
setDraftTransaction,
balance,
isAddress,
setIsAddress,
contentUri,
contentClaim,
setEnteredContentUri,
contentError,
confirmed,
sendLabel,
setSendLabel,
snack,
} = this.props;
if (confirmed) {
this.handleClear();
setSendLabel('Sending...');
}
if (snack) setSendLabel('Send');
return (
2019-09-26 18:07:11 +02:00
<Card
2021-03-10 07:36:02 +01:00
title={__('Send Credits')}
2020-09-02 22:08:37 +02:00
subtitle={
<I18nMessage tokens={{ lbc: <LbcSymbol /> }}>
Send LBRY Credits to your friends or favorite creators.
</I18nMessage>
}
2019-09-26 18:07:11 +02:00
actions={
<Formik
initialValues={{
address: '',
amount: '',
}}
onSubmit={this.handleSubmit}
render={({ values, errors, touched, handleBlur, handleSubmit }) => (
<div>
<div className="section">
<Button
key="Address"
label={__('Address')}
button="alt"
onClick={() => setIsAddress(true)}
className={classnames('button-toggle', { 'button-toggle--active': isAddress })}
2019-09-26 18:07:11 +02:00
/>
<Button
key="Search"
label={__('Search')}
button="alt"
onClick={() => setIsAddress(false)}
className={classnames('button-toggle', { 'button-toggle--active': !isAddress })}
2019-09-26 18:07:11 +02:00
/>
</div>
<div className="section">
{!isAddress && <ChannelSelector />}
<Form onSubmit={handleSubmit}>
{!isAddress && (
<FormField
type="text"
name="search"
error={contentError}
placeholder={__('Enter a name, @username or URL')}
className="form-field--address"
label={__('Recipient search')}
onChange={(event) => setEnteredContentUri(event.target.value)}
onBlur={handleBlur}
value={values.search}
/>
)}
{!isAddress && (
<fieldset-section>
<ClaimPreview
key={contentUri}
uri={contentUri}
actions={''}
type={'small'}
showNullPlaceholder
hideMenu
hideRepostLabel
/>
</fieldset-section>
)}
<fieldset-group class="fieldset-group--smushed">
<FormField
autoFocus
type="number"
name="amount"
label={__('Amount')}
className="form-field--price-amount"
affixClass="form-field--fix-no-height"
min="0"
step="any"
placeholder="12.34"
2021-06-01 13:48:55 +02:00
onChange={(event) =>
setDraftTransaction({ address: draftTransaction.address, amount: event.target.value })
}
onBlur={handleBlur}
value={draftTransaction.amount}
/>
{isAddress && (
<FormField
type="text"
name="address"
placeholder={'bbFxRyXXXXXXXXXXXZD8nE7XTLUxYnddTs'}
className="form-field--address"
2021-06-01 13:48:55 +02:00
label={__('Recipient address')}
onChange={(event) =>
setDraftTransaction({ address: event.target.value, amount: draftTransaction.amount })
}
onBlur={handleBlur}
value={draftTransaction.address}
/>
)}
</fieldset-group>
<div className="card__actions">
<Button
button="primary"
type="submit"
label={__(sendLabel)}
disabled={
!(parseFloat(draftTransaction.amount) > 0.0) ||
parseFloat(draftTransaction.amount) >= balance ||
sendLabel === 'Sending...' ||
2021-06-01 13:48:55 +02:00
(isAddress
? !draftTransaction.address || validateSendTx(draftTransaction.address).address !== ''
: !contentClaim)
}
/>
{!!Object.keys(errors).length || (
<span className="error__text">
{(!!draftTransaction.address && touched.address && errors.address) ||
(!!draftTransaction.amount && touched.amount && errors.amount) ||
(parseFloat(draftTransaction.amount) === balance &&
__('Decrease amount to account for transaction fee')) ||
(parseFloat(draftTransaction.amount) > balance && __('Not enough Credits'))}
</span>
)}
</div>
<WalletSpendableBalanceHelp />
</Form>
</div>
</div>
2019-09-26 18:07:11 +02:00
)}
/>
}
/>
);
}
}
2017-06-06 06:21:55 +02:00
export default WalletSend;