Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

profile: display balance breakdown #534

Merged
merged 23 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1782d8d
profile: show balance breakdown
Patrick1904 Apr 22, 2020
ff900b9
prevent user from sending/signing above available balance amount
Patrick1904 Apr 23, 2020
5c33ebf
locked + amount = totalBalance
Patrick1904 Apr 23, 2020
075f5f8
profile: add info popup to 'available balance'
Patrick1904 Apr 24, 2020
a0f68e4
Add tooltip info to available and min balance
Patrick1904 Apr 24, 2020
462fa43
update min. balance copy
Patrick1904 Apr 24, 2020
b28d54f
add state stake amount
Patrick1904 Apr 25, 2020
c7e2ded
fix env variable
Patrick1904 Apr 25, 2020
d4bdf70
user balance: cleanup
Patrick1904 Apr 28, 2020
694b50a
Merge remote-tracking branch 'origin/master' into locked-balance
Patrick1904 May 14, 2020
bf7f1f1
Fetch account balance breakdown from near-api-js
Patrick1904 May 15, 2020
53bee9b
Change name of action, since the same name is used in wallet method (…
marcinbodnar May 18, 2020
5cf8ee6
Refactor refreshAccountExternal action (#481)
marcinbodnar May 18, 2020
ffdeb81
Add balance to refreshAccountExternal action (#481)
marcinbodnar May 18, 2020
f4c319d
Show balance from redux state on Profile (#481)
marcinbodnar May 18, 2020
b82a05e
Dispatch refreshAccountExternal for external accountId only (#481)
marcinbodnar May 18, 2020
6189b55
Refactor loadRecoveryMethods and create separate wallet method (#580)
marcinbodnar May 18, 2020
c64bbd8
RecoveryContainer take recovery methods from redux state (#580)
marcinbodnar May 18, 2020
d056e5a
Fix lint warnings
marcinbodnar May 18, 2020
b339ebd
Fix typo (#481)
marcinbodnar May 18, 2020
21a8e48
Send Money: Available Balance info stays while typing (#481)
marcinbodnar May 19, 2020
39af2df
remove nearlib dependency (#481)
marcinbodnar May 19, 2020
3a83e07
Change from nearlib to nearApiJs (#481)
marcinbodnar May 19, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"connected-react-router": "^6.6.0",
"fetch-send-json": "0.0.2",
"js-sha256": "^0.9.0",
"near-api-js": "^0.25.0",
"near-ledger-js": "^0.0.4",
"near-seed-phrase": "^0.0.2",
"nearlib": "^0.22.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nearlib should be removed

Expand Down
21 changes: 10 additions & 11 deletions src/actions/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,9 @@ import { push } from 'connected-react-router'
import { loadState, saveState, clearState } from '../utils/sessionStorage'
import { WALLET_CREATE_NEW_ACCOUNT_URL, WALLET_CREATE_NEW_ACCOUNT_FLOW_URLS, WALLET_LOGIN_URL } from '../utils/wallet'

export const loadAccount = createAction('LOAD_ACCOUNT',
accountId => wallet.getAccount(accountId).state(),
accountId => ({ accountId })
)

export const loadRecoveryMethods = createAction('LOAD_RECOVERY_METHODS',
async accountId => sendJson('POST', `${ACCOUNT_HELPER_URL}/account/recoveryMethods`, {
accountId: wallet.accountId,
...(await wallet.signatureFor(wallet.accountId))
}),
accountId => ({ accountId })
wallet.getRecoveryMethods.bind(wallet),
() => ({})
)

export const handleRedirectUrl = (previousLocation) => (dispatch, getState) => {
Expand Down Expand Up @@ -203,12 +195,19 @@ export const { signAndSendTransactions } = createActions({
]
})

export const { switchAccount, refreshAccount, resetAccounts, refreshUrl, setFormLoader } = createActions({
export const { switchAccount, refreshAccount, refreshAccountExternal, resetAccounts, refreshUrl, setFormLoader } = createActions({
SWITCH_ACCOUNT: wallet.selectAccount.bind(wallet),
REFRESH_ACCOUNT: [
wallet.loadAccount.bind(wallet),
() => ({ accountId: wallet.getAccountId(), })
],
REFRESH_ACCOUNT_EXTERNAL: [
async (accountId) => ({
...await wallet.getAccount(accountId).state(),
balance: await wallet.getBalance(accountId)
}),
accountId => ({ accountId })
],
RESET_ACCOUNTS: wallet.clearState.bind(wallet),
REFRESH_URL: null,
SET_FORM_LOADER: null
Expand Down
3 changes: 3 additions & 0 deletions src/components/GlobalStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ export default createGlobalStyle`
.ui.left.center.popup:before {
box-shadow: 1px -1px 0 0 #eee;
}
.ui.top.center.popup:before {
box-shadow: 1px 1px 0px 0px #eee;
}
.ui.bottom.right.popup:before {
box-shadow: -1px -1px 0 0 #eee;
}
Expand Down
17 changes: 9 additions & 8 deletions src/components/common/Balance.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from 'react'
import styled from 'styled-components'
import { List } from 'semantic-ui-react'
import { utils } from 'nearlib'
import { utils } from 'near-api-js'
import { BN } from 'bn.js'

const CustomDiv = styled(List)`
position: relative;
display: inline;
white-space: nowrap;
`

const FRAC_DIGITS = 5
Expand All @@ -16,6 +17,7 @@ const Balance = ({ amount }) => {
if (!amount) {
throw new Error('amount property should not be null')
}

let amountShow = formatNEAR(amount)

return (
Expand All @@ -27,14 +29,13 @@ const Balance = ({ amount }) => {

export const formatNEAR = (amount) => {
let ret = utils.format.formatNearAmount(amount, FRAC_DIGITS)
if (ret === '0') {
return `<${
!FRAC_DIGITS
? `0`
: `0.${'0'.repeat((FRAC_DIGITS || 1) - 1)}1`
}`

if (amount === '0') {
return amount;
} else if (ret === '0') {
return `<${!FRAC_DIGITS ? `0` : `0.${'0'.repeat((FRAC_DIGITS || 1) - 1)}1`}`;
}
return ret
return ret;
}

const showInYocto = (amountStr) => {
Expand Down
6 changes: 6 additions & 0 deletions src/components/common/Balance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import Balance from './Balance'
configure({adapter: new Adapter()});

describe('<Balance.js>', ()=>{
const contextZero = '0'
const contextTiny = '123456'
const contextSmall = "1"+"0".repeat(19)
const contextBig = "1234567"+"0".repeat(21)

it('balance should return properly for 0',()=>{
let wrapper = shallow(<Balance amount={contextZero} />)
expect(wrapper.text()).toEqual("0 Ⓝ");
})

it('balance should return properly for non 0 for 0.0987',()=>{
let wrapper = shallow(<Balance amount={contextTiny} />)
expect(wrapper.text()).toEqual("<0.00001 Ⓝ");
Expand Down
45 changes: 45 additions & 0 deletions src/components/common/InfoPopup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from 'react'
import styled from 'styled-components'
import { Popup } from 'semantic-ui-react'
import InfoIcon from '../svg/InfoIcon.js'

const Trigger = styled.div`
height: 20px;
width: 20px;
display: inline-block;
margin-left: 5px;

svg {
height: 20px;
width: 20px;
fill: #F8F8F8;

circle {
:first-of-type {
stroke: none;
}
:last-of-type {
stroke: #b5b5b5;
fill: #b5b5b5;
}
}

line {
stroke: #b5b5b5;
}
}

`

const InfoPopup = ({
content,
position = 'top center'
}) => (
<Popup
content={content}
trigger={<Trigger className='trigger'><InfoIcon/></Trigger>}
position={position}
/>
)

export default InfoPopup
18 changes: 13 additions & 5 deletions src/components/dashboard/DashboardDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import React, { Component, Fragment } from 'react'
import { connect } from 'react-redux'
import { Translate } from 'react-localize-redux'
import { withRouter } from 'react-router-dom'

import { getAccessKeys, getTransactions, getTransactionStatus } from '../../actions/account'

import DashboardSection from './DashboardSection'
import DashboardActivity from './DashboardActivity'
import PageContainer from '../common/PageContainer'
Expand Down Expand Up @@ -66,14 +64,24 @@ class DashboardDetail extends Component {

render() {
const { loader, notice } = this.state
const { authorizedApps, fullAccessKeys, transactions, amount, accountId, formLoader, getTransactionStatus } = this.props

const {
authorizedApps,
fullAccessKeys,
transactions,
accountId,
formLoader,
getTransactionStatus,
balance
} = this.props

return (
<PageContainer
title={(
amount
balance
? <Fragment>
<span className='balance'><Translate id='balance.balance' />: </span>
<Balance amount={amount} />
<Balance amount={balance.total}/>
</Fragment>
: <Translate id='balance.balanceLoading' />
)}
Expand Down
2 changes: 1 addition & 1 deletion src/components/navigation/DesktopContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class DesktopContainer extends Component {
<User onClick={toggleMenu}>
<UserIcon/>
<UserName accountId={account.accountId}/>
<UserBalance amount={account.amount}/>
<UserBalance balance={account.balance}/>
</User>
<DesktopMenu
show={menuOpen}
Expand Down
2 changes: 1 addition & 1 deletion src/components/navigation/MobileContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class MobileContainer extends Component {
<>
<User>
<UserName accountId={account.accountId}/>
<UserBalance amount={account.amount}/>
<UserBalance balance={account.balance}/>
</User>
<MenuButton onClick={toggleMenu} open={menuOpen}/>
</>
Expand Down
4 changes: 2 additions & 2 deletions src/components/navigation/UserBalance.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const StyledBalance = styled.div`
color: #8FD6BD;
`

const UserBalance = ({ amount }) => (
const UserBalance = ({ balance }) => (
<StyledBalance className='user-balance'>
{amount && <Balance amount={amount}/>}
{balance && <Balance amount={balance.total}/>}
</StyledBalance>
)

Expand Down
15 changes: 7 additions & 8 deletions src/components/profile/Profile.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import React from 'react'
import { Translate } from 'react-localize-redux'
import { useSelector } from 'react-redux'

import PageContainer from '../common/PageContainer';
import ProfileDetails from './ProfileDetails'
import ProfileSection from './ProfileSection'
import RecoveryContainer from './Recovery/RecoveryContainer'
import { LOADING, NOT_FOUND, useAccount } from '../../hooks/allAccounts'
import { useRecoveryMethods } from '../../hooks/recoveryMethods'

export function Profile({ match }) {
const { accountId } = match.params
const account = useAccount(accountId)
const recoveryMethods = useRecoveryMethods(account.accountId)
const loginAccountId = useSelector(state => state.account.accountId)

if (account.__status === LOADING) {
return <PageContainer title={<Translate id='profile.pageTitle.loading' />} />
Expand All @@ -24,12 +24,11 @@ export function Profile({ match }) {
return (
<PageContainer title={<Translate id='profile.pageTitle.default' data={{ accountId }} />}>
<ProfileSection>
<ProfileDetails account={account} />
<RecoveryContainer
accountId={accountId}
activeMethods={recoveryMethods}
/>
<ProfileDetails account={account}/>
{accountId === loginAccountId && (
<RecoveryContainer />
)}
</ProfileSection>
</PageContainer>
)
}
}
Loading