2016-11-22 21:19:08 +01:00
|
|
|
import React from 'react';
|
|
|
|
import lbry from '../lbry.js';
|
2017-04-07 07:15:22 +02:00
|
|
|
import Link from 'component/link';
|
2016-11-22 21:19:08 +01:00
|
|
|
import Modal from '../component/modal.js';
|
2017-04-27 15:17:18 +02:00
|
|
|
import {SubHeader} from '../component/header.js';
|
2017-04-10 14:32:40 +02:00
|
|
|
import {FormField, FormRow} from '../component/form.js';
|
2016-11-22 21:19:08 +01:00
|
|
|
import {Address, BusyMessage, CreditAmount} from '../component/common.js';
|
|
|
|
|
2016-09-01 08:40:23 +02:00
|
|
|
var AddressSection = React.createClass({
|
2016-10-19 09:13:39 +02:00
|
|
|
_refreshAddress: function(event) {
|
|
|
|
if (typeof event !== 'undefined') {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2017-04-10 19:44:54 +02:00
|
|
|
lbry.getUnusedAddress((address) => {
|
|
|
|
window.localStorage.setItem('wallet_address', address);
|
|
|
|
this.setState({
|
|
|
|
address: address,
|
|
|
|
});
|
2017-04-30 03:56:55 +02:00
|
|
|
});
|
2017-04-10 19:44:54 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
_getNewAddress: function(event) {
|
|
|
|
if (typeof event !== 'undefined') {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2017-04-27 20:18:59 +02:00
|
|
|
lbry.getNewAddress((address) => {
|
2016-11-22 21:28:16 +01:00
|
|
|
window.localStorage.setItem('wallet_address', address);
|
2016-07-19 00:40:15 +02:00
|
|
|
this.setState({
|
2016-09-01 08:40:23 +02:00
|
|
|
address: address,
|
|
|
|
});
|
2017-04-30 03:56:55 +02:00
|
|
|
});
|
2016-07-19 00:40:15 +02:00
|
|
|
},
|
2017-04-10 19:44:54 +02:00
|
|
|
|
2016-07-19 00:40:15 +02:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2016-09-01 08:40:23 +02:00
|
|
|
address: null,
|
2016-10-21 09:56:37 +02:00
|
|
|
modal: null,
|
2016-09-01 08:40:23 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
componentWillMount: function() {
|
2016-11-22 21:28:16 +01:00
|
|
|
var address = window.localStorage.getItem('wallet_address');
|
2016-09-01 08:40:23 +02:00
|
|
|
if (address === null) {
|
2016-09-02 07:46:27 +02:00
|
|
|
this._refreshAddress();
|
2016-09-01 08:40:23 +02:00
|
|
|
} else {
|
2016-09-02 07:46:27 +02:00
|
|
|
lbry.checkAddressIsMine(address, (isMine) => {
|
|
|
|
if (isMine) {
|
|
|
|
this.setState({
|
|
|
|
address: address,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this._refreshAddress();
|
|
|
|
}
|
2016-09-01 08:40:23 +02:00
|
|
|
});
|
2016-07-19 00:40:15 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
render: function() {
|
|
|
|
return (
|
2016-08-08 02:20:14 +02:00
|
|
|
<section className="card">
|
2017-04-10 14:32:40 +02:00
|
|
|
<div className="card__title-primary">
|
|
|
|
<h3>Wallet Address</h3>
|
|
|
|
</div>
|
|
|
|
<div className="card__content">
|
|
|
|
<Address address={this.state.address} />
|
|
|
|
</div>
|
|
|
|
<div className="card__actions">
|
|
|
|
<Link label="Get New Address" button="primary" icon='icon-refresh' onClick={this._getNewAddress} />
|
|
|
|
</div>
|
|
|
|
<div className="card__content">
|
|
|
|
<div className="help">
|
|
|
|
<p>Other LBRY users may send credits to you by entering this address on the "Send" page.</p>
|
|
|
|
<p>You can generate a new address at any time, and any previous addresses will continue to work. Using multiple addresses can be helpful for keeping track of incoming payments from multiple sources.</p>
|
|
|
|
</div>
|
2016-09-01 08:40:23 +02:00
|
|
|
</div>
|
2016-08-03 10:07:36 +02:00
|
|
|
</section>
|
2016-07-19 00:40:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var SendToAddressSection = React.createClass({
|
2016-10-19 09:13:39 +02:00
|
|
|
handleSubmit: function(event) {
|
|
|
|
if (typeof event !== 'undefined') {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2016-08-02 15:35:18 +02:00
|
|
|
if ((this.state.balance - this.state.amount) < 1)
|
2016-08-02 11:34:30 +02:00
|
|
|
{
|
2016-10-21 09:56:37 +02:00
|
|
|
this.setState({
|
|
|
|
modal: 'insufficientBalance',
|
|
|
|
});
|
2016-08-02 11:34:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-19 00:40:15 +02:00
|
|
|
this.setState({
|
|
|
|
results: "",
|
|
|
|
});
|
|
|
|
|
2016-07-19 01:18:16 +02:00
|
|
|
lbry.sendToAddress(this.state.amount, this.state.address, (results) => {
|
2016-07-19 00:40:15 +02:00
|
|
|
if(results === true)
|
|
|
|
{
|
|
|
|
this.setState({
|
2016-07-22 17:59:20 +02:00
|
|
|
results: "Your transaction was successfully placed in the queue.",
|
2016-07-19 00:40:15 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.setState({
|
|
|
|
results: "Something went wrong: " + results
|
|
|
|
});
|
|
|
|
}
|
2016-07-22 17:33:05 +02:00
|
|
|
}, (error) => {
|
|
|
|
this.setState({
|
2017-03-23 20:42:40 +01:00
|
|
|
results: "Something went wrong: " + error.message
|
2016-07-22 17:33:05 +02:00
|
|
|
})
|
2016-07-19 00:40:15 +02:00
|
|
|
});
|
|
|
|
},
|
2016-10-21 09:56:37 +02:00
|
|
|
closeModal: function() {
|
|
|
|
this.setState({
|
|
|
|
modal: null,
|
|
|
|
});
|
|
|
|
},
|
2016-07-19 00:40:15 +02:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
address: "",
|
|
|
|
amount: 0.0,
|
2016-08-22 18:16:43 +02:00
|
|
|
balance: <BusyMessage message="Checking balance" />,
|
2016-07-19 00:40:15 +02:00
|
|
|
results: "",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
componentWillMount: function() {
|
|
|
|
lbry.getBalance((results) => {
|
|
|
|
this.setState({
|
|
|
|
balance: results,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
setAmount: function(event) {
|
|
|
|
this.setState({
|
2016-07-22 17:33:05 +02:00
|
|
|
amount: parseFloat(event.target.value),
|
2016-07-19 00:40:15 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
setAddress: function(event) {
|
|
|
|
this.setState({
|
|
|
|
address: event.target.value,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
render: function() {
|
|
|
|
return (
|
2016-08-08 02:20:14 +02:00
|
|
|
<section className="card">
|
2016-10-19 09:13:39 +02:00
|
|
|
<form onSubmit={this.handleSubmit}>
|
2017-04-10 14:32:40 +02:00
|
|
|
<div className="card__title-primary">
|
|
|
|
<h3>Send Credits</h3>
|
2016-08-08 05:31:21 +02:00
|
|
|
</div>
|
2017-04-10 14:32:40 +02:00
|
|
|
<div className="card__content">
|
2017-04-12 16:55:19 +02:00
|
|
|
<FormRow label="Amount" postfix="LBC" step="0.01" type="number" placeholder="1.23" size="10" onChange={this.setAmount} />
|
2016-10-19 09:13:39 +02:00
|
|
|
</div>
|
2017-04-10 14:32:40 +02:00
|
|
|
<div className="card__content">
|
|
|
|
<FormRow label="Recipient Address" placeholder="bbFxRyXXXXXXXXXXXZD8nE7XTLUxYnddTs" type="text" size="60" onChange={this.setAddress} />
|
|
|
|
</div>
|
|
|
|
<div className="card__actions card__actions--form-submit">
|
2016-10-19 09:13:39 +02:00
|
|
|
<Link button="primary" label="Send" onClick={this.handleSubmit} disabled={!(parseFloat(this.state.amount) > 0.0) || this.state.address == ""} />
|
|
|
|
<input type='submit' className='hidden' />
|
|
|
|
</div>
|
2017-04-10 14:32:40 +02:00
|
|
|
{
|
|
|
|
this.state.results ?
|
|
|
|
<div className="card__content">
|
|
|
|
<h4>Results</h4>
|
|
|
|
{this.state.results}
|
|
|
|
</div> : ''
|
|
|
|
}
|
2016-10-19 09:13:39 +02:00
|
|
|
</form>
|
2017-01-13 23:05:09 +01:00
|
|
|
<Modal isOpen={this.state.modal === 'insufficientBalance'} contentLabel="Insufficient balance"
|
|
|
|
onConfirmed={this.closeModal}>
|
2016-10-21 09:56:37 +02:00
|
|
|
Insufficient balance: after this transaction you would have less than 1 LBC in your wallet.
|
|
|
|
</Modal>
|
2016-08-08 05:31:21 +02:00
|
|
|
</section>
|
2016-07-19 00:40:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-08-27 16:12:56 +02:00
|
|
|
|
|
|
|
var TransactionList = React.createClass({
|
2016-08-08 02:20:14 +02:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2016-08-27 16:12:56 +02:00
|
|
|
transactionItems: null,
|
2016-08-08 02:20:14 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
componentWillMount: function() {
|
2016-08-20 21:52:03 +02:00
|
|
|
lbry.call('get_transaction_history', {}, (results) => {
|
|
|
|
if (results.length == 0) {
|
2016-08-27 16:12:56 +02:00
|
|
|
this.setState({ transactionItems: [] })
|
2016-08-20 21:52:03 +02:00
|
|
|
} else {
|
2016-08-27 16:12:56 +02:00
|
|
|
var transactionItems = [],
|
|
|
|
condensedTransactions = {};
|
2016-08-20 21:52:03 +02:00
|
|
|
results.forEach(function(tx) {
|
2016-08-20 23:52:31 +02:00
|
|
|
var txid = tx["txid"];
|
|
|
|
if (!(txid in condensedTransactions)) {
|
|
|
|
condensedTransactions[txid] = 0;
|
|
|
|
}
|
2016-09-05 19:20:34 +02:00
|
|
|
condensedTransactions[txid] += parseFloat(tx["value"]);
|
2016-08-20 21:52:03 +02:00
|
|
|
});
|
2016-08-24 07:08:58 +02:00
|
|
|
results.reverse().forEach(function(tx) {
|
2016-08-20 23:52:31 +02:00
|
|
|
var txid = tx["txid"];
|
2016-09-03 22:28:05 +02:00
|
|
|
if (condensedTransactions[txid] && condensedTransactions[txid] != 0)
|
2016-08-27 16:12:56 +02:00
|
|
|
{
|
|
|
|
transactionItems.push({
|
|
|
|
id: txid,
|
2016-11-11 11:09:43 +01:00
|
|
|
date: tx["timestamp"] ? (new Date(parseInt(tx["timestamp"]) * 1000)) : null,
|
2016-08-27 16:12:56 +02:00
|
|
|
amount: condensedTransactions[txid]
|
|
|
|
});
|
|
|
|
delete condensedTransactions[txid];
|
2016-08-20 23:52:31 +02:00
|
|
|
}
|
|
|
|
});
|
2016-08-27 16:12:56 +02:00
|
|
|
|
|
|
|
this.setState({ transactionItems: transactionItems });
|
2016-08-20 21:52:03 +02:00
|
|
|
}
|
2016-08-27 16:12:56 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
render: function() {
|
|
|
|
var rows = [];
|
|
|
|
if (this.state.transactionItems && this.state.transactionItems.length > 0)
|
|
|
|
{
|
|
|
|
this.state.transactionItems.forEach(function(item) {
|
|
|
|
rows.push(
|
|
|
|
<tr key={item.id}>
|
|
|
|
<td>{ (item.amount > 0 ? '+' : '' ) + item.amount }</td>
|
2016-11-11 11:09:43 +01:00
|
|
|
<td>{ item.date ? item.date.toLocaleDateString() : <span className="empty">(Transaction pending)</span> }</td>
|
|
|
|
<td>{ item.date ? item.date.toLocaleTimeString() : <span className="empty">(Transaction pending)</span> }</td>
|
2016-08-27 16:12:56 +02:00
|
|
|
<td>
|
|
|
|
<a className="button-text" href={"https://explorer.lbry.io/tx/"+item.id} target="_blank">{item.id.substr(0, 7)}</a>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<section className="card">
|
2017-04-10 14:32:40 +02:00
|
|
|
<div className="card__title-primary">
|
|
|
|
<h3>Transaction History</h3>
|
|
|
|
</div>
|
|
|
|
<div className="card__content">
|
|
|
|
{ this.state.transactionItems === null ? <BusyMessage message="Loading transactions" /> : '' }
|
|
|
|
{ this.state.transactionItems && rows.length === 0 ? <div className="empty">You have no transactions.</div> : '' }
|
|
|
|
{ this.state.transactionItems && rows.length > 0 ?
|
|
|
|
<table className="table-standard table-stretch">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Amount</th>
|
|
|
|
<th>Date</th>
|
|
|
|
<th>Time</th>
|
|
|
|
<th>Transaction</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{rows}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2016-08-27 16:12:56 +02:00
|
|
|
: ''
|
2017-04-10 14:32:40 +02:00
|
|
|
}
|
|
|
|
</div>
|
2016-08-27 16:12:56 +02:00
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-04-27 15:17:18 +02:00
|
|
|
export let WalletNav = React.createClass({
|
|
|
|
render: function() {
|
|
|
|
return <SubHeader modifier="constrained" viewingPage={this.props.viewingPage} links={{
|
|
|
|
'?wallet': 'Overview',
|
|
|
|
'?send': 'Send',
|
|
|
|
'?receive': 'Receive',
|
|
|
|
'?rewards': 'Rewards'
|
|
|
|
}} />;
|
|
|
|
}
|
|
|
|
});
|
2016-08-27 16:12:56 +02:00
|
|
|
|
|
|
|
var WalletPage = React.createClass({
|
2017-03-26 20:30:18 +02:00
|
|
|
_balanceSubscribeId: null,
|
|
|
|
|
2016-08-27 16:12:56 +02:00
|
|
|
propTypes: {
|
|
|
|
viewingPage: React.PropTypes.string,
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
Below should be refactored so that balance is shared all of wallet page. Or even broader?
|
|
|
|
What is the proper React pattern for sharing a global state like balance?
|
|
|
|
*/
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
balance: null,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
componentWillMount: function() {
|
2017-03-26 20:30:18 +02:00
|
|
|
this._balanceSubscribeId = lbry.balanceSubscribe((results) => {
|
2016-08-20 21:52:03 +02:00
|
|
|
this.setState({
|
2016-08-27 16:12:56 +02:00
|
|
|
balance: results,
|
2016-08-20 21:52:03 +02:00
|
|
|
})
|
2016-08-08 02:20:14 +02:00
|
|
|
});
|
|
|
|
},
|
2017-03-26 20:30:18 +02:00
|
|
|
componentWillUnmount: function() {
|
|
|
|
if (this._balanceSubscribeId) {
|
|
|
|
lbry.balanceUnsubscribe(this._balanceSubscribeId);
|
|
|
|
}
|
|
|
|
},
|
2016-07-19 00:40:15 +02:00
|
|
|
render: function() {
|
|
|
|
return (
|
2017-05-01 02:15:21 +02:00
|
|
|
<main className="main--single-column">
|
2017-04-27 15:17:18 +02:00
|
|
|
<WalletNav viewingPage={this.props.viewingPage} />
|
2016-08-08 02:20:14 +02:00
|
|
|
<section className="card">
|
2017-04-10 14:32:40 +02:00
|
|
|
<div className="card__title-primary">
|
|
|
|
<h3>Balance</h3>
|
|
|
|
</div>
|
|
|
|
<div className="card__content">
|
|
|
|
{ this.state.balance === null ? <BusyMessage message="Checking balance" /> : ''}
|
|
|
|
{ this.state.balance !== null ? <CreditAmount amount={this.state.balance} precision={8} /> : '' }
|
|
|
|
</div>
|
2016-08-20 21:52:03 +02:00
|
|
|
</section>
|
2016-08-27 16:12:56 +02:00
|
|
|
{ this.props.viewingPage === 'wallet' ? <TransactionList /> : '' }
|
|
|
|
{ this.props.viewingPage === 'send' ? <SendToAddressSection /> : '' }
|
2016-09-01 08:40:23 +02:00
|
|
|
{ this.props.viewingPage === 'receive' ? <AddressSection /> : '' }
|
2016-07-19 00:40:15 +02:00
|
|
|
</main>
|
|
|
|
);
|
|
|
|
}
|
2016-08-22 18:16:43 +02:00
|
|
|
});
|
2016-11-22 21:19:08 +01:00
|
|
|
|
|
|
|
export default WalletPage;
|