2016-09-01 08:40:23 +02:00
var addressRefreshButtonStyle = {
fontSize : '11pt' ,
} ;
var AddressSection = React . createClass ( {
_refreshAddress : function ( ) {
lbry . getNewAddress ( ( address ) => {
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 ,
} ) ;
2016-07-19 00:40:15 +02:00
} ) ;
} ,
getInitialState : function ( ) {
return {
2016-09-01 08:40:23 +02:00
address : null ,
}
} ,
componentWillMount : function ( ) {
var address = localStorage . getItem ( 'wallet_address' ) ;
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" >
2016-09-01 08:40:23 +02:00
< h3 > Wallet Address < / h 3 >
< p > < Address address = { this . state . address } / > < Link text = "Get new address" icon = 'icon-refresh' onClick = { this . _refreshAddress } style = { addressRefreshButtonStyle } / > < / p >
< div className = "help" >
< p > Other LBRY users may send credits to you by entering this address on the "Send" page . < / 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 .
< / d i v >
2016-08-03 10:07:36 +02:00
< / s e c t i o n >
2016-07-19 00:40:15 +02:00
) ;
}
} ) ;
var SendToAddressSection = React . createClass ( {
sendToAddress : function ( ) {
2016-08-02 15:35:18 +02:00
if ( ( this . state . balance - this . state . amount ) < 1 )
2016-08-02 11:34:30 +02:00
{
alert ( "Insufficient balance: after this transaction you would have less than 1 LBC in your wallet." )
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 ( {
results : "Something went wrong: " + error . faultString + " " + error . faultCode
} )
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" >
< h3 > Send Credits < / h 3 >
2016-08-08 05:31:21 +02:00
< div className = "form-row" >
2016-08-08 05:36:23 +02:00
< label htmlFor = "amount" > Amount < / l a b e l >
< input id = "amount" type = "text" size = "10" onChange = { this . setAmount } > < / i n p u t >
2016-08-08 05:31:21 +02:00
< / d i v >
< div className = "form-row" >
2016-08-08 05:36:23 +02:00
< label htmlFor = "address" > Recipient address < / l a b e l >
< input id = "address" type = "text" size = "60" onChange = { this . setAddress } > < / i n p u t >
2016-08-08 05:31:21 +02:00
< / d i v >
2016-08-08 05:36:23 +02:00
< div className = "form-row form-row-submit" >
2016-08-03 10:07:36 +02:00
< Link button = "primary" label = "Send" onClick = { this . sendToAddress } disabled = { ! ( parseFloat ( this . state . amount ) > 0.0 ) || this . state . address == "" } / >
2016-08-08 05:31:21 +02:00
< / d i v >
2016-08-08 02:20:14 +02:00
{
this . state . results ?
2016-08-08 05:31:21 +02:00
< div className = "form-row" >
2016-08-08 02:20:14 +02:00
< h4 > Results < / h 4 >
{ this . state . results }
2016-08-08 05:31:21 +02:00
< / d i v >
2016-08-21 16:57:11 +02:00
: ''
2016-08-08 02:20:14 +02:00
}
2016-08-08 05:31:21 +02:00
< / s e c t i o n >
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-08-27 16:12:56 +02:00
condensedTransactions [ txid ] += parseFloat ( tx [ "value" ] ) ;
2016-08-20 21:52:03 +02:00
} ) ;
2016-08-27 16:12:56 +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-08-27 16:12:56 +02:00
if ( condensedTransactions [ txid ] && condensedTransactions [ txid ] > 0 )
{
transactionItems . push ( {
id : txid ,
date : new Date ( parseInt ( tx [ "timestamp" ] ) * 1000 ) ,
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 } < / t d >
< td > { item . date . toLocaleDateString ( ) } < / t d >
< td > { item . date . toLocaleTimeString ( ) } < / t d >
< td >
< a className = "button-text" href = { "https://explorer.lbry.io/tx/" + item . id } target = "_blank" > { item . id . substr ( 0 , 7 ) } < / a >
< / t d >
< / t r >
) ;
} ) ;
}
return (
< section className = "card" >
< h3 > Transaction History < / h 3 >
{ this . state . transactionItems === null ? < BusyMessage message = "Loading transactions" / > : '' }
{ this . state . transactionItems && rows . length === 0 ? < div className = "empty" > You have no transactions . < / d i v > : ' ' }
{ this . state . transactionItems && rows . length > 0 ?
< table className = "table-standard table-stretch" >
< thead >
< tr >
< th > Amount < / t h >
< th > Date < / t h >
< th > Time < / t h >
< th > Transaction < / t h >
< / t r >
< / t h e a d >
< tbody >
{ rows }
< / t b o d y >
< / t a b l e >
: ''
}
< / s e c t i o n >
) ;
}
} ) ;
var WalletPage = React . createClass ( {
propTypes : {
viewingPage : React . PropTypes . string ,
} ,
componentDidMount : function ( ) {
document . title = "My Wallet" ;
} ,
/ *
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 ( ) {
lbry . getBalance ( ( 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
} ) ;
} ,
2016-07-19 00:40:15 +02:00
render : function ( ) {
return (
< main className = "page" >
2016-08-08 02:20:14 +02:00
< section className = "card" >
< h3 > Balance < / h 3 >
2016-08-27 16:12:56 +02:00
{ this . state . balance === null ? < BusyMessage message = "Checking balance" / > : '' }
{ this . state . balance !== null ? < CreditAmount amount = { this . state . balance } precision = { 8 } / > : '' }
2016-08-20 21:52:03 +02:00
< / s e c t i o n >
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
< / m a i n >
) ;
}
2016-08-22 18:16:43 +02:00
} ) ;