Skip to content
This repository has been archived by the owner on Jul 22, 2020. It is now read-only.

Commit

Permalink
fix: minor accounts details component and store refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-calavera authored and sunnygleason committed Dec 23, 2019
1 parent cb219e3 commit 1560679
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 45 deletions.
74 changes: 38 additions & 36 deletions src/v2/components/Accounts/Detail/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
import {observer} from 'mobx-react-lite';
import {Container, Tabs, useTheme} from '@material-ui/core';
import useMediaQuery from '@material-ui/core/useMediaQuery/useMediaQuery';
import {map, eq} from 'lodash/fp';
import React, {useState} from 'react';
import {map} from 'lodash/fp';
import React, {useEffect, useState} from 'react';
import {Match} from 'react-router-dom';
import SectionHeader from 'v2/components/UI/SectionHeader';
import QRPopup from 'v2/components/QRPopup';
import CopyBtn from 'v2/components/UI/CopyBtn';
import Loader from 'v2/components/UI/Loader';
import AccountDetailStore from 'v2/stores/accounts/detail';
import {LAMPORT_SOL_RATIO} from 'v2/constants';
import TabNav from 'v2/components/UI/TabNav';
import AddToFavorites from 'v2/components/AddToFavorites';
import InfoRow from 'v2/components/InfoRow';
Expand All @@ -26,20 +25,25 @@ const AccountDetail = ({match}: {match: Match}) => {
const {
isLoading,
accountId,
accountInfo = {},
accountInfo,
accountView,
init,
} = AccountDetailStore;

if (accountId !== match.params.id) {
AccountDetailStore.init({accountId: match.params.id});
}
useEffect(() => {
init({accountId: match.params.id});
}, [init, match.params.id]);

const [tab, setTab] = useState(0);
const theme = useTheme();
const verticalTable = useMediaQuery(theme.breakpoints.down('xs'));

if (isLoading) {
return <Loader width="533" height="290" />;
return (
<Container className={classes.loader}>
<Loader width="533" height="290" />
</Container>
);
}

const handleTabChange = (event, tab) => setTab(tab);
Expand All @@ -48,9 +52,7 @@ const AccountDetail = ({match}: {match: Match}) => {
{
label: 'Balance',
hint: '',
value: `${((accountInfo.lamports || 0) * LAMPORT_SOL_RATIO).toFixed(
8,
)} SOL`,
value: `${accountInfo.balance} SOL`,
},
{
label: 'Transactions',
Expand All @@ -72,7 +74,11 @@ const AccountDetail = ({match}: {match: Match}) => {
const renderSpec = info => <InfoRow key={info.label} {...info} />;

const tabNav = ['transaction', 'analytics', 'code/source'];

const tabs = {
0: <Transactions transactions={[]} />,
1: <Chart />,
2: <AccountCode accountView={accountView} />,
};
const renderTabNav = label => <TabNav key={label} label={label} />;
const url = currentURL();
const favoritesData = {
Expand All @@ -82,32 +88,28 @@ const AccountDetail = ({match}: {match: Match}) => {

return (
<Container>
<div className={classes.root}>
<SectionHeader title="Account Detail">
<div className={classes.programTitle}>
<span>{accountId}</span>
<CopyBtn text={accountId} />
<QRPopup url={url} />
<AddToFavorites item={favoritesData} type="accounts" />
</div>
</SectionHeader>
<div className={classes.body}>
<div className={classes.spec}>{map(renderSpec)(specs)}</div>
<SectionHeader title="Account Detail">
<div className={classes.programTitle}>
<span>{accountId}</span>
<CopyBtn text={accountId} />
<QRPopup url={url} />
<AddToFavorites item={favoritesData} type="accounts" />
</div>
<Tabs
orientation={verticalTable ? 'vertical' : 'horizontal'}
className={classes.tabs}
classes={{indicator: classes.indicator}}
value={tab}
variant="fullWidth"
onChange={handleTabChange}
>
{map(renderTabNav)(tabNav)}
</Tabs>
{eq(0, tab) && <Transactions transactions={[]} />}
{eq(1, tab) && <Chart />}
{eq(2, tab) && <AccountCode accountView={accountView} />}
</SectionHeader>
<div className={classes.body}>
<div className={classes.spec}>{map(renderSpec)(specs)}</div>
</div>
<Tabs
orientation={verticalTable ? 'vertical' : 'horizontal'}
className={classes.tabs}
classes={{indicator: classes.indicator}}
value={tab}
variant="fullWidth"
onChange={handleTabChange}
>
{map(renderTabNav)(tabNav)}
</Tabs>
{tabs[tab]}
</Container>
);
};
Expand Down
3 changes: 3 additions & 0 deletions src/v2/components/Accounts/Detail/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ export default makeStyles(theme => ({
indicator: {
display: 'none',
},
loader: {
marginTop: 40,
},
}));
21 changes: 12 additions & 9 deletions src/v2/stores/accounts/detail.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import {action, flow, observable, decorate} from 'mobx';
import {apiGetAccountDetail} from 'v2/api/accounts';
import {LAMPORT_SOL_RATIO} from 'v2/constants';

const extendAccountInfo = (account = {}) => ({
...account,
balance: ((account.lamports || 0) * LAMPORT_SOL_RATIO).toFixed(8),
});

class Store {
isLoading = false;
isLoading = true;
accountId = null;
accountView = {};
accountInfo = {};
Expand All @@ -13,16 +19,12 @@ class Store {
this.setLoading(true);
this.accountId = accountId;

if (this.accountInfo.pubkey) {
return this.accountInfo;
}

const res = yield apiGetAccountDetail({accountId});

const {accountInfo, programAccounts, timestamp} = res.data;
this.accountView = res.data;
this.accountInfo = res.data.accountInfo;
this.programAccounts.replace(res.data.programAccounts);
this.timestamp = res.data.timestamp;
this.accountInfo = extendAccountInfo(accountInfo);
this.programAccounts.replace(programAccounts);
this.timestamp = timestamp;
this.setLoading(false);

return res;
Expand All @@ -35,6 +37,7 @@ class Store {

decorate(Store, {
setLoading: action.bound,
init: action.bound,
isLoading: observable,
accountInfo: observable,
programAccounts: observable,
Expand Down

0 comments on commit 1560679

Please sign in to comment.