diff --git a/packages/light-react/package.json b/packages/light-react/package.json index e927674be735be1fbdab759696d56d5c7e262f83..54b6b94f94d72bb6cd7cc2a6cabab764b735b43f 100644 --- a/packages/light-react/package.json +++ b/packages/light-react/package.json @@ -36,7 +36,7 @@ }, "dependencies": { "@parity/api": "^2.1.22", - "@parity/light.js": "https://github.com/parity-js/light.js#165a97deb86d7b77a96b324b7d42d04a27a43b0b", + "@parity/light.js": "https://github.com/parity-js/light.js#9646ce15d9dd9c4cf11776ddd613d5bd86016f94", "@parity/shared": "^3.0.2", "is-electron": "^2.1.0", "light-hoc": "^0.1.0", diff --git a/packages/light-react/src/Accounts/AccountsList/AccountsList.js b/packages/light-react/src/Accounts/AccountsList/AccountsList.js index b518fe396c6c75332195a47c175245273b8477b6..f3d42635af07685fe46d62238e122768b1bcbc70 100644 --- a/packages/light-react/src/Accounts/AccountsList/AccountsList.js +++ b/packages/light-react/src/Accounts/AccountsList/AccountsList.js @@ -5,16 +5,17 @@ import React, { Component } from 'react'; import { AccountCard, Header } from 'light-ui'; -import { accountsInfo$ } from '@parity/light.js'; +import { accountsInfo$, defaultAccount$ } from '@parity/light.js'; import { inject, observer } from 'mobx-react'; import light from 'light-hoc'; import Health from '../../Health'; @light({ - accountsInfo: accountsInfo$ + accountsInfo: accountsInfo$, + defaultAccount: defaultAccount$ }) -@inject('createAccountStore', 'parityStore') +@inject('createAccountStore', 'parityStore', 'tokensStore') @observer class AccountsList extends Component { handleClick = ({ @@ -23,16 +24,36 @@ class AccountsList extends Component { } }) => { const { + defaultAccount, history, - parityStore: { api } + parityStore: { api }, + tokensStore } = this.props; + + // If we selected the same account, just go back to the tokens page + if (address === defaultAccount) { + history.push('/tokens'); + return; + } + + // We set the tokens in tokensStore to {}, to have a smooth transition (so + // that we don't see the tokens of the previous account for 1s). + tokensStore.fetchTokensFromDb(); + // TODO Ideally we would set the defaultAccount temporarily to null. Change + // light.js to support this. + // defaultAccount$().next(); + // Set default account to the clicked one, and go to Tokens on complete // TODO Not 100% clean, I don't want any api.abc.method() in any React // component. api.parity .setNewDappsDefaultAddress(address) - .then(() => history.push('/tokens')) - .catch(() => {}); // TODO do what? + .then(() => { + history.push('/tokens', { address }); + }) + .catch(err => + console.error(`Error while selecting account, ${err.message}.`) + ); }; handleCreateAccount = () => { @@ -56,9 +77,9 @@ class AccountsList extends Component {
- {accountsInfo ? ( -
diff --git a/packages/light-react/src/Tokens/Tokens.js b/packages/light-react/src/Tokens/Tokens.js index 31f9d177e77584a0e5581c9edae4b995974197d5..52ff2bcee18a31fd89d4fa9f1ebd335607151ce3 100644 --- a/packages/light-react/src/Tokens/Tokens.js +++ b/packages/light-react/src/Tokens/Tokens.js @@ -18,18 +18,27 @@ import TokensList from './TokensList'; }) class Tokens extends PureComponent { render () { - const { accountsInfo, defaultAccount } = this.props; + const { + accountsInfo, + defaultAccount, + location: { state } + } = this.props; + + // The address is defaultAccount, but if we are coming from the accounts + // page, then the address is also put inside the route state, for faster + // access. + const myAddress = (state && state.address) || defaultAccount; return (
diff --git a/packages/light-react/src/Tokens/TokensList/TokenBalance/TokenBalance.js b/packages/light-react/src/Tokens/TokensList/TokenBalance/TokenBalance.js index c5b8bc69fd7eb4d88cdf4a13b44f3d9b816002e2..4d4406c149adbcbafee1de5fd71a53e3f1162084 100644 --- a/packages/light-react/src/Tokens/TokensList/TokenBalance/TokenBalance.js +++ b/packages/light-react/src/Tokens/TokensList/TokenBalance/TokenBalance.js @@ -5,25 +5,40 @@ import React, { Component } from 'react'; import abi from '@parity/shared/lib/contracts/abi/eip20'; -import { defaultAccount$, makeContract$, myBalance$ } from '@parity/light.js'; +import { empty } from 'rxjs'; +import { + defaultAccount$, + isNullOrLoading, + makeContract$, + myBalance$ +} from '@parity/light.js'; +import { filter, map, switchMap } from 'rxjs/operators'; import { fromWei } from '@parity/api/lib/util/wei'; import { inject } from 'mobx-react'; import light from 'light-hoc'; -import { map, switchMap } from 'rxjs/operators'; import PropTypes from 'prop-types'; import { TokenCard } from 'light-ui'; import { withRouter } from 'react-router-dom'; @light({ - balance: ({ token: { address, decimals } }) => - address === 'ETH' - ? myBalance$().pipe(map(value => +fromWei(value))) + balance: ({ token: { address, decimals } }) => { + if (!address) { + return empty(); + } + return address === 'ETH' + ? myBalance$().pipe( + map(value => (isNullOrLoading(value) ? null : value)), + map(value => value && +fromWei(value)) + ) : defaultAccount$().pipe( + filter(x => x), switchMap(defaultAccount => makeContract$(address, abi).balanceOf$(defaultAccount) ), - map(value => +value.div(10 ** decimals)) - ) + map(value => (isNullOrLoading(value) ? null : value)), + map(value => value && +value.div(10 ** decimals)) + ); + } }) @inject('sendStore') @withRouter diff --git a/packages/light-react/src/Tokens/TokensList/TokensList.js b/packages/light-react/src/Tokens/TokensList/TokensList.js index f9718a1884aa3b1ce0120405883fc04573e1a112..a431b498c6bb91b2aa9e70ccf0ad0d82755d6261 100644 --- a/packages/light-react/src/Tokens/TokensList/TokensList.js +++ b/packages/light-react/src/Tokens/TokensList/TokensList.js @@ -16,8 +16,8 @@ class Tokens extends Component { tokensStore: { tokensArray } } = this.props; - // Show empty 3 tokens placeholders if tokens have not been loaded yet - const shownArray = tokensArray.length ? tokensArray : Array(3).fill({}); + // Show empty token placeholder if tokens have not been loaded yet + const shownArray = tokensArray.length ? tokensArray : [{}]; return (
diff --git a/packages/light-react/src/stores/tokensStore.js b/packages/light-react/src/stores/tokensStore.js index c9d9c8b9e6456fa93ed82ee4556dd0621b2f6b32..4e3fd24212fa5e56a12c9ab504381285a3ce63de 100644 --- a/packages/light-react/src/stores/tokensStore.js +++ b/packages/light-react/src/stores/tokensStore.js @@ -33,6 +33,11 @@ class TokensStore { @action fetchTokensFromDb = async (chainName, defaultAccount) => { + if (!defaultAccount || !chainName) { + this.tokens = {}; + return; + } + // Set the localStorage key, we have one key per chain per account, in this // format: __paritylight::tokens::0x123::kovan this.lsKey = `${LS_KEY}::${defaultAccount}::${chainName}`; diff --git a/packages/light-ui/src/AccountCard/AccountCard.js b/packages/light-ui/src/AccountCard/AccountCard.js index c330272d08b19fdfde88c555fdb471de83e42b8a..06108522eee84136201e8667672e040f2b9db57c 100644 --- a/packages/light-ui/src/AccountCard/AccountCard.js +++ b/packages/light-ui/src/AccountCard/AccountCard.js @@ -27,7 +27,7 @@ const AccountCard = ({ address, name, ...otherProps }) => ( )}
- {address || } + {address || }
diff --git a/yarn.lock b/yarn.lock index 7f3d271a6a01527acc4ba2180be7bdec262b6b22..f87b8cc3a220c1b3c33985d07e5ddf98f6496f28 100644 --- a/yarn.lock +++ b/yarn.lock @@ -976,12 +976,13 @@ u2f-api "0.0.9" u2f-api-polyfill "0.4.3" -"@parity/light.js@https://github.com/parity-js/light.js#165a97deb86d7b77a96b324b7d42d04a27a43b0b": +"@parity/light.js@https://github.com/parity-js/light.js#9646ce15d9dd9c4cf11776ddd613d5bd86016f94": version "1.0.0" - resolved "https://github.com/parity-js/light.js#165a97deb86d7b77a96b324b7d42d04a27a43b0b" + resolved "https://github.com/parity-js/light.js#9646ce15d9dd9c4cf11776ddd613d5bd86016f94" dependencies: "@babel/runtime" "^7.0.0-beta.49" "@parity/api" "^2.1.23" + json-prune "^1.1.0" memoizee "^0.4.12" "@parity/shared@^3.0.2": @@ -7230,6 +7231,10 @@ json-parse-better-errors@^1.0.1, json-parse-better-errors@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9" +json-prune@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/json-prune/-/json-prune-1.1.0.tgz#23e2fad108932b255050e3f1a70500c2b4aa30c7" + json-schema-traverse@^0.3.0: version "0.3.1" resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz#349a6d44c53a51de89b40805c5d5e59b417d3340"