lbry-desktop/ui/page/wallet/view.jsx

237 lines
7.5 KiB
React
Raw Normal View History

// @flow
2019-06-17 22:32:38 +02:00
import React from 'react';
import { withRouter } from 'react-router';
2019-06-17 22:32:38 +02:00
import WalletBalance from 'component/walletBalance';
2021-07-28 15:24:28 +02:00
import WalletFiatBalance from 'component/walletFiatBalance';
2021-07-29 18:44:32 +02:00
import WalletFiatPaymentBalance from 'component/walletFiatPaymentBalance';
2021-07-29 17:45:02 +02:00
import WalletFiatAccountHistory from 'component/walletFiatAccountHistory';
import WalletFiatPaymentHistory from 'component/walletFiatPaymentHistory';
import TxoList from 'component/txoList';
2019-06-17 22:32:38 +02:00
import Page from 'component/page';
2020-06-01 19:03:19 +02:00
import Spinner from 'component/spinner';
import YrblWalletEmpty from 'component/yrblWalletEmpty';
2021-07-28 19:06:14 +02:00
import { Lbryio } from 'lbryinc';
2019-06-17 22:32:38 +02:00
type Props = {
2021-07-03 20:32:06 +02:00
history: { action: string, push: (string) => void, replace: (string) => void },
location: { search: string, pathname: string },
totalBalance: ?number,
};
2019-06-17 22:32:38 +02:00
const WalletPage = (props: Props) => {
console.log(props);
2021-07-28 23:10:46 +02:00
var stripeEnvironment = 'test';
2021-07-29 19:29:05 +02:00
var environment = 'test';
2021-07-28 19:06:14 +02:00
const tab = new URLSearchParams(props.location.search).get('tab');
2021-07-28 19:06:14 +02:00
const [accountStatusResponse, setAccountStatusResponse] = React.useState();
2021-07-28 23:10:46 +02:00
const [accountTransactionResponse, setAccountTransactionResponse] = React.useState();
2021-07-29 19:29:05 +02:00
const [customerTransactions, setCustomerTransactions] = React.useState();
2021-07-29 19:45:16 +02:00
const [totalTippedAmount, setTotalTippedAmount] = React.useState(0);
2021-07-29 19:29:05 +02:00
function getPaymentHistory() {
return Lbryio.call(
'customer',
'list',
{
environment: stripeEnvironment,
},
'post'
)};
function getCustomerStatus(){
return Lbryio.call(
'customer',
'status',
{
environment: stripeEnvironment,
},
'post'
)
}
2021-07-28 19:06:14 +02:00
function getAccountStatus(){
return Lbryio.call(
'account',
'status',
{
2021-07-28 23:10:46 +02:00
environment: stripeEnvironment,
},
'post'
);
}
function getAccountTransactionsa(){
return Lbryio.call(
'account',
'list',
{
environment: stripeEnvironment,
2021-07-28 19:06:14 +02:00
},
'post'
);
}
2021-07-29 21:58:18 +02:00
// calculate account transactions section
2021-07-28 19:06:14 +02:00
React.useEffect(() => {
(async function(){
2021-07-28 23:10:46 +02:00
try {
const response = await getAccountStatus();
2021-07-29 21:58:18 +02:00
setAccountStatusResponse(response);
// TODO: some weird naming clash hence getAccountTransactionsa
const getAccountTransactions = await getAccountTransactionsa();
setAccountTransactionResponse(getAccountTransactions)
} catch (err){
console.log(err);
}
})();
}, []);
// populate customer payment data
React.useEffect(() => {
(async function(){
try {
2021-07-29 19:45:16 +02:00
// get card payments customer has made
2021-07-29 19:29:05 +02:00
const customerTransactionResponse = await getPaymentHistory();
2021-07-29 19:45:16 +02:00
let totalTippedAmount = 0;
for(const transaction of customerTransactionResponse){
totalTippedAmount = totalTippedAmount + transaction.tipped_amount
}
setTotalTippedAmount(totalTippedAmount / 100);
2021-07-29 19:29:05 +02:00
setCustomerTransactions(customerTransactionResponse)
2021-07-28 23:10:46 +02:00
} catch (err){
2021-07-29 19:45:16 +02:00
console.log(err);
2021-07-28 23:10:46 +02:00
}
2021-07-28 19:06:14 +02:00
})();
}, []);
2021-07-29 14:18:16 +02:00
function focusLBCTab(){
document.getElementsByClassName('lbc-transactions')[0].style.display = 'inline';
document.getElementsByClassName('fiat-transactions')[0].style.display = 'none';
document.getElementsByClassName('payment-history-tab')[0].style.display = 'none';
document.getElementsByClassName('lbc-tab-switcher')[0].style.textDecoration = 'underline';
document.getElementsByClassName('fiat-tab-switcher')[0].style.textDecoration = 'none';
document.getElementsByClassName('fiat-payment-history-switcher')[0].style.textDecoration = 'none';
}
function focusAccountHistoryTab(){
document.getElementsByClassName('lbc-transactions')[0].style.display = 'none';
document.getElementsByClassName('payment-history-tab')[0].style.display = 'none';
document.getElementsByClassName('fiat-transactions')[0].style.display = 'inline';
document.getElementsByClassName('lbc-tab-switcher')[0].style.textDecoration = 'none';
document.getElementsByClassName('fiat-tab-switcher')[0].style.textDecoration = 'underline';
document.getElementsByClassName('fiat-payment-history-switcher')[0].style.textDecoration = 'none';
}
function focusPaymentHistoryTab(){
document.getElementsByClassName('lbc-transactions')[0].style.display = 'none';
document.getElementsByClassName('fiat-transactions')[0].style.display = 'none';
document.getElementsByClassName('payment-history-tab')[0].style.display = 'inline';
document.getElementsByClassName('lbc-tab-switcher')[0].style.textDecoration = 'none';
document.getElementsByClassName('fiat-tab-switcher')[0].style.textDecoration = 'none';
document.getElementsByClassName('fiat-payment-history-switcher')[0].style.textDecoration = 'underline';
}
// select the first tab
2021-07-28 15:18:06 +02:00
React.useEffect(() => {
2021-07-29 14:18:16 +02:00
// if (tab === 'account-history') {
2021-07-29 17:45:02 +02:00
if (1 === 2) {
focusAccountHistoryTab();
// } else if (tab === 'payment-history'){
} else if (1 === 1){
focusPaymentHistoryTab();
}
2021-07-28 15:18:06 +02:00
}, []);
const { location, totalBalance } = props;
const { search } = location;
const showIntro = totalBalance === 0;
const loading = totalBalance === undefined;
return (
<Page>
2021-07-28 13:08:28 +02:00
{/* tabs to switch between fiat and lbc */}
2021-07-29 21:01:21 +02:00
{/* lbc button */}
<h2 className="lbc-tab-switcher"
2021-07-28 15:18:06 +02:00
style={{display: 'inline-block', paddingBottom: '16px', marginRight: '14px', textUnderlineOffset: '4px', textDecoration: 'underline', fontSize: '18px', marginLeft: '3px'}}
2021-07-28 13:08:28 +02:00
onClick={() => {
2021-07-29 14:18:16 +02:00
focusLBCTab();
2021-07-28 13:08:28 +02:00
}}
2021-07-29 13:48:33 +02:00
>LBC Wallet</h2>
2021-07-29 21:01:21 +02:00
{/* account history button */}
<h2 className="fiat-tab-switcher"
2021-07-29 14:18:16 +02:00
style={{display: 'inline-block', textUnderlineOffset: '4px', fontSize: '18px', marginRight: '14px'}}
2021-07-28 13:08:28 +02:00
onClick={() => {
2021-07-29 14:18:16 +02:00
focusAccountHistoryTab();
2021-07-28 13:08:28 +02:00
}}
2021-07-29 13:48:33 +02:00
>Account History</h2>
2021-07-29 21:01:21 +02:00
{/* payment history button */}
2021-07-29 14:18:16 +02:00
<h2 className="fiat-payment-history-switcher"
style={{display: 'inline-block', textUnderlineOffset: '4px', fontSize: '18px'}}
onClick={() => {
focusPaymentHistoryTab();
}}
>Payment History</h2>
2021-07-29 21:01:21 +02:00
{/* lbc wallet section */}
2021-07-28 13:08:28 +02:00
<div className="lbc-transactions">
{/* if the transactions are loading */}
{ loading && (
<div className="main--empty">
<Spinner delayed />
</div>
)}
{/* when the transactions are finished loading */}
{ !loading && (
<>
{showIntro ? (
<YrblWalletEmpty includeWalletLink />
) : (
<div className="card-stack">
<WalletBalance />
<TxoList search={search} />
</div>
)}
</>
)}
</div>
2021-07-29 14:18:16 +02:00
2021-07-29 21:01:21 +02:00
{/* account received transactions section */}
<div className="fiat-transactions" style={{display: 'none'}}>
<WalletFiatBalance accountDetails={accountStatusResponse} />
2021-07-29 18:32:50 +02:00
<div style={{paddingTop: '25px'}}></div>
2021-07-29 21:01:21 +02:00
<WalletFiatAccountHistory transactions={accountTransactionResponse}/>
</div>
{/* fiat payment history for tips made by user */}
<div className="payment-history-tab" style={{display: 'none'}}>
<WalletFiatPaymentBalance totalTippedAmount={totalTippedAmount} accountDetails={accountStatusResponse} />
<div style={{paddingTop: '25px'}}></div>
<WalletFiatPaymentHistory transactions={customerTransactions}/>
</div>
2021-07-29 14:18:16 +02:00
</Page>
);
};
export default withRouter(WalletPage);