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

236 lines
6.4 KiB
React
Raw Normal View History

// @flow
2019-06-17 22:32:38 +02:00
import React from 'react';
2021-08-13 19:04:57 +02:00
import { useHistory } from 'react-router';
2019-06-17 22:32:38 +02:00
import WalletBalance from 'component/walletBalance';
import WalletFiatBalance from 'component/walletFiatBalance';
import WalletFiatPaymentBalance from 'component/walletFiatPaymentBalance';
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';
2021-08-13 17:37:49 +02:00
import * as PAGES from 'constants/pages';
2020-06-01 19:03:19 +02:00
import Spinner from 'component/spinner';
import YrblWalletEmpty from 'component/yrblWalletEmpty';
import { Lbryio } from 'lbryinc';
2021-08-13 19:04:57 +02:00
import { STRIPE_PUBLIC_KEY } from 'config';
2021-08-13 17:37:49 +02:00
import { Tabs, TabList, Tab, TabPanels, TabPanel } from 'component/common/tabs';
const TAB_QUERY = 'tab';
const TABS = {
LBRY_CREDITS_TAB: 'credits',
ACCOUNT_HISTORY: 'fiat-account-history',
PAYMENT_HISTORY: 'fiat-payment-history',
};
2021-08-13 15:55:46 +02:00
let stripeEnvironment = 'test';
// if the key contains pk_live it's a live key
// update the environment for the calls to the backend to indicate which environment to hit
if (STRIPE_PUBLIC_KEY.indexOf('pk_live') > -1) {
stripeEnvironment = 'live';
}
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) => {
2021-08-13 17:37:49 +02:00
const {
location: { search },
push,
} = useHistory();
2021-08-13 17:59:50 +02:00
// @if TARGET='web'
2021-08-13 17:37:49 +02:00
const urlParams = new URLSearchParams(search);
const currentView = urlParams.get(TAB_QUERY) || TABS.LBRY_CREDITS_TAB;
let tabIndex;
switch (currentView) {
case TABS.LBRY_CREDITS_TAB:
tabIndex = 0;
break;
case TABS.PAYMENT_HISTORY:
tabIndex = 1;
break;
case TABS.ACCOUNT_HISTORY:
tabIndex = 2;
break;
default:
tabIndex = 0;
break;
}
function onTabChange(newTabIndex) {
let url = `/$/${PAGES.WALLET}?`;
if (newTabIndex === 0) {
url += `${TAB_QUERY}=${TABS.LBRY_CREDITS_TAB}`;
} else if (newTabIndex === 1) {
url += `${TAB_QUERY}=${TABS.PAYMENT_HISTORY}`;
} else if (newTabIndex === 2) {
url += `${TAB_QUERY}=${TABS.ACCOUNT_HISTORY}`;
} else {
url += `${TAB_QUERY}=${TABS.LBRY_CREDITS_TAB}`;
}
push(url);
}
const [accountStatusResponse, setAccountStatusResponse] = React.useState();
2021-08-13 16:46:45 +02:00
const [accountTransactionResponse, setAccountTransactionResponse] = React.useState([]);
const [customerTransactions, setCustomerTransactions] = React.useState([]);
function getPaymentHistory() {
return Lbryio.call(
'customer',
'list',
{
environment: stripeEnvironment,
},
'post'
2021-08-13 16:07:12 +02:00
);
}
function getAccountStatus() {
return Lbryio.call(
'account',
'status',
{
environment: stripeEnvironment,
},
'post'
);
}
function getAccountTransactionsa() {
return Lbryio.call(
'account',
'list',
{
environment: stripeEnvironment,
},
'post'
);
}
// calculate account transactions section
React.useEffect(() => {
(async function() {
try {
const response = await getAccountStatus();
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 {
// get card payments customer has made
const customerTransactionResponse = await getPaymentHistory();
setCustomerTransactions(customerTransactionResponse);
} catch (err) {
console.log(err);
}
})();
}, []);
2021-08-13 17:59:50 +02:00
// @endif
const { totalBalance } = props;
const showIntro = totalBalance === 0;
const loading = totalBalance === undefined;
2021-08-13 17:59:50 +02:00
// @if TARGET='web'
return (
<Page>
2021-08-13 17:44:56 +02:00
<Tabs onChange={onTabChange} index={tabIndex}>
2021-08-13 17:37:49 +02:00
<TabList className="tabs__list--collection-edit-page">
<Tab>{__('LBRY Credits')}</Tab>
<Tab>{__('Account History')}</Tab>
<Tab>{__('Payment History')}</Tab>
</TabList>
<TabPanels>
<TabPanel>
2021-08-13 17:44:56 +02:00
<div className="section card-stack">
<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>
</div>
2021-08-13 17:37:49 +02:00
</TabPanel>
<TabPanel>
2021-08-13 17:44:56 +02:00
<div className="section card-stack">
<WalletFiatBalance accountDetails={accountStatusResponse} />
<WalletFiatAccountHistory transactions={accountTransactionResponse} />
</div>
2021-08-13 17:37:49 +02:00
</TabPanel>
<TabPanel>
2021-08-13 17:44:56 +02:00
<div className="section card-stack">
2021-08-13 17:59:50 +02:00
<WalletFiatPaymentBalance transactions={customerTransactions} accountDetails={accountStatusResponse} />
2021-08-13 17:44:56 +02:00
<WalletFiatPaymentHistory transactions={customerTransactions} />
</div>
2021-08-13 17:37:49 +02:00
</TabPanel>
</TabPanels>
</Tabs>
</Page>
);
2021-08-13 17:59:50 +02:00
// @endif
// @if TARGET='app'
2021-08-13 19:04:57 +02:00
// $FlowFixMe
2021-08-13 17:59:50 +02:00
// eslint-disable-next-line no-unreachable
return (
<Page>
{loading && (
<div className="main--empty">
<Spinner delayed />
</div>
)}
{!loading && (
<>
{showIntro ? (
<YrblWalletEmpty includeWalletLink />
) : (
<div className="card-stack">
<WalletBalance />
<TxoList search={search} />
</div>
)}
</>
)}
</Page>
);
// @endif
};
2021-08-13 17:37:49 +02:00
export default WalletPage;