From bb22ca54f017c92cd83454804779117fd5fd31aa Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Wed, 11 Apr 2018 15:21:43 +0200 Subject: [PATCH] AbstractAccound refactor --- src/js/actions/AbstractAccountActions.js | 31 ++-- src/js/actions/ReceiveActions.js | 18 +-- src/js/actions/SendFormActions.js | 139 ++++++++++-------- src/js/actions/SummaryActions.js | 19 +-- src/js/components/wallet/Receive.js | 11 +- .../wallet/account/AbstractAccount.js | 10 +- src/js/components/wallet/send/AdvancedForm.js | 2 +- src/js/components/wallet/send/SendForm.js | 9 +- src/js/components/wallet/send/index.js | 3 + src/js/components/wallet/summary/Summary.js | 12 +- src/js/components/wallet/summary/index.js | 5 + src/js/reducers/AbstractAccountReducer.js | 13 +- src/js/reducers/AccountsReducer.js | 4 + src/js/reducers/ReceiveReducer.js | 12 -- src/js/reducers/SendFormReducer.js | 14 -- src/js/reducers/SummaryReducer.js | 14 -- 16 files changed, 130 insertions(+), 186 deletions(-) diff --git a/src/js/actions/AbstractAccountActions.js b/src/js/actions/AbstractAccountActions.js index c6bb7f8d..ad39e3de 100644 --- a/src/js/actions/AbstractAccountActions.js +++ b/src/js/actions/AbstractAccountActions.js @@ -3,11 +3,9 @@ import * as ACCOUNT from './constants/account'; -import { initialState } from '../reducers/AccountDetailReducer'; +import { initialState } from '../reducers/AbstractAccountReducer'; import { findSelectedDevice } from '../reducers/TrezorConnectReducer'; - -import type { State } from '../reducers/AccountDetailReducer'; -import type { Discovery } from '../reducers/DiscoveryReducer'; +import type { State } from '../reducers/AbstractAccountReducer'; export const init = (): any => { return (dispatch, getState): void => { @@ -18,10 +16,11 @@ export const init = (): any => { const selected = findSelectedDevice( getState().connect ); if (!selected) return; - const state: State = { index: parseInt(urlParams.address), deviceState: selected.state, + deviceId: selected.features.device_id, + deviceInstance: selected.instance, network: urlParams.network, location: location.pathname }; @@ -33,33 +32,21 @@ export const init = (): any => { } } -export const update = (newProps: any): any => { +export const update = (): any => { return (dispatch, getState): void => { - const { - accountDetail, - connect, - discovery, - accounts, + abstractAccount, router } = getState(); - - const isLocationChanged: boolean = newProps.location.pathname !== accountDetail.location; - + const isLocationChanged: boolean = router.location.pathname !== abstractAccount.location; if (isLocationChanged) { - dispatch({ - type: ACCOUNT.INIT, - state: { - ...accountDetail, - location: newProps.location.pathname, - } - }); + dispatch( init() ); return; } } } -export const dispose = (device: any): any => { +export const dispose = (): any => { return (dispatch, getState): void => { dispatch({ type: ACCOUNT.DISPOSE, diff --git a/src/js/actions/ReceiveActions.js b/src/js/actions/ReceiveActions.js index 6d3cabd9..4fa54bd2 100644 --- a/src/js/actions/ReceiveActions.js +++ b/src/js/actions/ReceiveActions.js @@ -12,20 +12,9 @@ import { findSelectedDevice } from '../reducers/TrezorConnectReducer'; export const init = (): any => { return (dispatch, getState): void => { - const { location } = getState().router; - const urlParams = location.params; - - const selected = findSelectedDevice( getState().connect ); - if (!selected) return; - + const state: State = { ...initialState, - deviceState: selected.state, - deviceId: selected.features.device_id, - deviceInstance: selected.instance, - accountIndex: parseInt(urlParams.address), - network: urlParams.network, - location: location.pathname, }; dispatch({ @@ -39,14 +28,13 @@ export const init = (): any => { export const update = (newProps: any): any => { return (dispatch, getState): void => { const { - receive, + abstractAccount, router } = getState(); - const isLocationChanged: boolean = router.location.pathname !== receive.location; + const isLocationChanged: boolean = router.location.pathname !== abstractAccount.location; if (isLocationChanged) { dispatch( init() ); - return; } } } diff --git a/src/js/actions/SendFormActions.js b/src/js/actions/SendFormActions.js index 6a54712d..2f9f3fe1 100644 --- a/src/js/actions/SendFormActions.js +++ b/src/js/actions/SendFormActions.js @@ -15,7 +15,9 @@ import { push } from 'react-router-redux'; import BigNumber from 'bignumber.js'; import { initialState } from '../reducers/SendFormReducer'; +import { findAccount } from '../reducers/AccountsReducer'; import type { State, FeeLevel } from '../reducers/SendFormReducer'; +import type { Account } from '../reducers/AccountsReducer'; import { findSelectedDevice } from '../reducers/TrezorConnectReducer'; //const numberRegExp = new RegExp('^([0-9]{0,10}\\.)?[0-9]{1,18}$'); @@ -33,7 +35,7 @@ const calculateTotal = (amount: string, gasPrice: string, gasLimit: string): str } } -export const calculateMaxAmount = (balance: string, gasPrice: string, gasLimit: string): string => { +const calculateMaxAmount = (balance: string, gasPrice: string, gasLimit: string): string => { try { const fee = EthereumjsUnits.convert( new BigNumber(gasPrice).times(gasLimit), 'gwei', 'ether'); const b = new BigNumber(balance); @@ -46,6 +48,10 @@ export const calculateMaxAmount = (balance: string, gasPrice: string, gasLimit: } +const getMaxAmount = () => { + +} + export const getFeeLevels = (symbol: string, gasPrice: BigNumber | string, gasLimit: string): Array => { if (typeof gasPrice === 'string') gasPrice = new BigNumber(gasPrice); const quarter: BigNumber = gasPrice.dividedBy(4); @@ -77,11 +83,12 @@ export const getFeeLevels = (symbol: string, gasPrice: BigNumber | string, gasLi } export const findBalance = (getState: any): string => { - const state = getState().sendForm; - const account = getState().accounts.find(a => a.deviceState === state.deviceState && a.index === state.accountIndex && a.network === state.network); + const accountState = getState().abstractAccount; + const { token } = getState().sendForm; + const account: ?Account = findAccount(getState().accounts, accountState.index, accountState.deviceState, accountState.network); - if (state.token !== state.network) { - return getState().tokens.find(t => t.ethAddress === account.address && t.symbol === state.token).balance; + if (token !== state.network) { + return getState().tokens.find(t => t.ethAddress === account.address && t.symbol === token).balance; } else { return account.balance; } @@ -116,14 +123,8 @@ export const init = (): any => { const state: State = { ...initialState, - deviceState: selected.state, - deviceId: selected.features.device_id, - deviceInstance: selected.instance, - accountIndex: parseInt(urlParams.address), - network: urlParams.network, coinSymbol: coin.symbol, - token: urlParams.network, - location: location.pathname, + token: coin.network, feeLevels, selectedFeeLevel: feeLevels.find(f => f.value === 'Normal'), @@ -143,11 +144,11 @@ export const init = (): any => { export const update = (): any => { return (dispatch, getState): void => { const { - sendForm, + abstractAccount, router } = getState(); - const isLocationChanged: boolean = router.location.pathname !== sendForm.location; + const isLocationChanged: boolean = router.location.pathname !== abstractAccount.location; if (isLocationChanged) { dispatch( init() ); return; @@ -170,6 +171,7 @@ export const toggleAdvanced = (address: string): any => { export const validation = (): any => { return (dispatch, getState): void => { + const accountState = getState().abstractAccount; const state: State = getState().sendForm; const errors: {[k: string]: string} = {}; const warnings: {[k: string]: string} = {}; @@ -188,7 +190,7 @@ export const validation = (): any => { } else if (!EthereumjsUtil.isValidAddress(state.address)) { errors.address = 'Address is not valid'; } else if (myAccount) { - if (myAccount.network === state.network) { + if (myAccount.network === accountState.network) { infos.address = `TREZOR Address #${ (myAccount.index + 1) }`; } else { // TODO: load coins from config @@ -206,10 +208,10 @@ export const validation = (): any => { } else if (state.amount.length > 0 && !state.amount.match(numberRegExp)) { errors.amount = 'Amount is not a number'; } else { - const account = getState().accounts.find(a => a.deviceState === state.deviceState && a.index === state.accountIndex && a.network === state.network); + const account: ?Account = findAccount(getState().accounts, accountState.index, accountState.deviceState, accountState.network); let decimalRegExp; - if (state.token !== state.network) { + if (state.token !== accountState.network) { const token: any = getState().tokens.find(t => t.ethAddress === account.address && t.symbol === state.token); if (parseInt(token.decimals) > 0) { @@ -272,7 +274,7 @@ export const validation = (): any => { } // valid data - if (state.touched.data && state.network === state.token && state.data.length > 0) { + if (state.touched.data && accountState.network === state.token && state.data.length > 0) { const re = /^[0-9A-Fa-f]+$/g; //const re = /^[0-9A-Fa-f]{6}$/g; if (!re.test(state.data)) { @@ -319,10 +321,12 @@ export const onAddressChange = (address: string): any => { export const onAmountChange = (amount: string): any => { return (dispatch, getState): void => { + const accountState = getState().abstractAccount; const currentState: State = getState().sendForm; + const isToken: boolean = currentState.token !== accountState.network; const touched = { ...currentState.touched }; touched.amount = true; - const total: string = calculateTotal(currentState.token !== currentState.network ? '0' : amount, currentState.gasPrice, currentState.gasLimit); + const total: string = calculateTotal(isToken ? '0' : amount, currentState.gasPrice, currentState.gasLimit); const state: State = { ...currentState, @@ -344,23 +348,24 @@ export const onAmountChange = (amount: string): any => { export const onCurrencyChange = (currency: any): any => { return (dispatch, getState): void => { - + const accountState = getState().abstractAccount; const currentState = getState().sendForm; + const isToken: boolean = currency.value !== accountState.network; - const account = getState().accounts.find(a => a.deviceState === currentState.deviceState && a.index === currentState.accountIndex && a.network === currentState.network); + const account: ?Account = findAccount(getState().accounts, accountState.index, accountState.deviceState, accountState.network); if (!account) { // account not found return; } const { config } = getState().localStorage; - const coin = config.coins.find(c => c.network === currentState.network); + const coin = config.coins.find(c => c.network === accountState.network); let gasLimit: string = ''; let amount: string = currentState.amount; let total: string; - if (currentState.network !== currency.value) { + if (isToken) { gasLimit = coin.defaultGasLimitTokens.toString(); if (currentState.setMax) { const tokenBalance: string = getState().tokens.find(t => t.ethAddress === account.address && t.symbol === currency.value).balance; @@ -399,11 +404,13 @@ export const onCurrencyChange = (currency: any): any => { export const onSetMax = (): any => { return (dispatch, getState): void => { + const accountState = getState().abstractAccount; const currentState = getState().sendForm; + const isToken: boolean = currentState.token !== accountState.network; const touched = { ...currentState.touched }; touched.amount = true; - const account = getState().accounts.find(a => a.deviceState === currentState.deviceState && a.index === currentState.accountIndex && a.network === currentState.network); + const account: ?Account = findAccount(getState().accounts, accountState.index, accountState.deviceState, accountState.network); if (!account) { // account not found return; @@ -412,7 +419,7 @@ export const onSetMax = (): any => { let amount: string = currentState.amount; let total: string = currentState.total; if (!currentState.setMax) { - if (currentState.token !== currentState.network) { + if (isToken) { const tokenBalance: string = getState().tokens.find(t => t.ethAddress === account.address && t.symbol === currentState.token).balance; amount = tokenBalance; total = calculateTotal('0', currentState.gasPrice, currentState.gasLimit); @@ -441,8 +448,10 @@ export const onSetMax = (): any => { export const onFeeLevelChange = (feeLevel: any): any => { return (dispatch, getState): void => { - + const accountState = getState().abstractAccount; const currentState = getState().sendForm; + const isToken: boolean = currentState.token !== accountState.network; + const state: State = { ...currentState, untouched: false, @@ -461,15 +470,15 @@ export const onFeeLevelChange = (feeLevel: any): any => { } if (currentState.setMax) { - const account = getState().accounts.find(a => a.deviceState === currentState.deviceState && a.index === currentState.accountIndex && a.network === currentState.network); - if (state.token !== state.network) { + const account: ?Account = findAccount(getState().accounts, accountState.index, accountState.deviceState, accountState.network); + if (isToken) { const tokenBalance: string = getState().tokens.find(t => t.ethAddress === account.address && t.symbol === currentState.token).balance; state.amount = tokenBalance; } else { state.amount = calculateMaxAmount(account.balance, state.gasPrice, state.gasLimit); } } - state.total = calculateTotal(state.token !== state.network ? '0' : state.amount, state.gasPrice, state.gasLimit); + state.total = calculateTotal(isToken ? '0' : state.amount, state.gasPrice, state.gasLimit); dispatch({ type: SEND.FEE_LEVEL_CHANGE, @@ -481,7 +490,10 @@ export const onFeeLevelChange = (feeLevel: any): any => { export const updateFeeLevels = (): any => { return (dispatch, getState): void => { + const accountState = getState().abstractAccount; const currentState = getState().sendForm; + const isToken: boolean = currentState.token !== accountState.network; + const feeLevels: Array = getFeeLevels(currentState.coinSymbol, currentState.recommendedGasPrice, currentState.gasLimit); const selectedFeeLevel: ?FeeLevel = feeLevels.find(f => f.value === currentState.selectedFeeLevel.value) const state: State = { @@ -494,15 +506,15 @@ export const updateFeeLevels = (): any => { }; if (currentState.setMax) { - const account = getState().accounts.find(a => a.deviceState === currentState.deviceState && a.index === currentState.accountIndex && a.network === currentState.network); - if (state.token !== state.network) { + const account: ?Account = findAccount(getState().accounts, accountState.index, accountState.deviceState, accountState.network); + if (isToken) { const tokenBalance: string = getState().tokens.find(t => t.ethAddress === account.address && t.symbol === currentState.token).balance; state.amount = tokenBalance; } else { state.amount = calculateMaxAmount(account.balance, state.gasPrice, state.gasLimit); } } - state.total = calculateTotal(state.token !== state.network ? '0' : state.amount, state.gasPrice, state.gasLimit); + state.total = calculateTotal(isToken ? '0' : state.amount, state.gasPrice, state.gasLimit); dispatch({ type: SEND.UPDATE_FEE_LEVELS, @@ -514,8 +526,10 @@ export const updateFeeLevels = (): any => { export const onGasPriceChange = (gasPrice: string): any => { return (dispatch, getState): void => { - + const accountState = getState().abstractAccount; const currentState = getState().sendForm; + const isToken: boolean = currentState.token !== accountState.network; + const touched = { ...currentState.touched }; touched.gasPrice = true; @@ -534,8 +548,8 @@ export const onGasPriceChange = (gasPrice: string): any => { state.selectedFeeLevel = customLevel; if (currentState.setMax) { - const account = getState().accounts.find(a => a.deviceState === currentState.deviceState && a.index === currentState.accountIndex && a.network === currentState.network); - if (state.token !== state.network) { + const account: ?Account = findAccount(getState().accounts, accountState.index, accountState.deviceState, accountState.network); + if (isToken) { const tokenBalance: string = getState().tokens.find(t => t.ethAddress === account.address && t.symbol === currentState.token).balance; state.amount = tokenBalance; } else { @@ -544,7 +558,7 @@ export const onGasPriceChange = (gasPrice: string): any => { } } - state.total = calculateTotal(state.token !== state.network ? '0' : state.amount, state.gasPrice, state.gasLimit); + state.total = calculateTotal(isToken ? '0' : state.amount, state.gasPrice, state.gasLimit); dispatch({ type: SEND.GAS_PRICE_CHANGE, @@ -556,7 +570,10 @@ export const onGasPriceChange = (gasPrice: string): any => { export const onGasLimitChange = (gasLimit: string): any => { return (dispatch, getState): void => { + const accountState = getState().abstractAccount; const currentState = getState().sendForm; + const isToken: boolean = currentState.token !== accountState.network; + const touched = { ...currentState.touched }; touched.gasLimit = true; @@ -568,15 +585,15 @@ export const onGasLimitChange = (gasLimit: string): any => { }; if (gasLimit.match(numberRegExp) && state.gasPrice.match(numberRegExp)) { - const customLevel = currentState.feeLevels.find(f => f.value === 'Custom'); - customLevel.label = `${ calculateFee(state.gasPrice, gasLimit) } ${ state.coinSymbol }`; + const customLevel = state.feeLevels.find(f => f.value === 'Custom'); + customLevel.label = `${ calculateFee(currentState.gasPrice, gasLimit) } ${ state.coinSymbol }`; state.selectedFeeLevel = customLevel; - if (currentState.setMax) { - const account = getState().accounts.find(a => a.deviceState === currentState.deviceState && a.index === currentState.accountIndex && a.network === currentState.network); - if (state.token !== state.network) { - const tokenBalance: string = getState().tokens.find(t => t.ethAddress === account.address && t.symbol === currentState.token).balance; + if (state.setMax) { + const account: ?Account = findAccount(getState().accounts, accountState.index, accountState.deviceState, accountState.network); + if (isToken) { + const tokenBalance: string = getState().tokens.find(t => t.ethAddress === account.address && t.symbol === state.token).balance; state.amount = tokenBalance; } else { state.amount = calculateMaxAmount(account.balance, state.gasPrice, state.gasLimit); @@ -584,7 +601,7 @@ export const onGasLimitChange = (gasLimit: string): any => { } } - state.total = calculateTotal(state.token !== state.network ? '0' : state.amount, state.gasPrice, state.gasLimit); + state.total = calculateTotal(isToken ? '0' : state.amount, state.gasPrice, state.gasLimit); dispatch({ type: SEND.GAS_LIMIT_CHANGE, @@ -620,32 +637,30 @@ export const onSend = (): any => { return async (dispatch, getState): Promise => { - const state: State = getState().sendForm; - const web3instance = getState().web3.filter(w3 => w3.network === state.network)[0]; + const accountState = getState().abstractAccount; + const currentState: State = getState().sendForm; + const web3instance = getState().web3.filter(w3 => w3.network === accountState.network)[0]; const web3 = web3instance.web3; - - const account = getState().accounts.find(a => a.deviceState === state.deviceState && a.index === state.accountIndex && a.network === state.network); + const account: ?Account = findAccount(getState().accounts, accountState.index, accountState.deviceState, accountState.network); + const isToken: boolean = currentState.token !== accountState.network; const address_n = account.addressPath; let data: string = ''; - let txAmount = web3.toHex(web3.toWei(state.amount, 'ether')); - let txAddress = state.address; - if (state.network !== state.token) { - const tokens = getState().tokens - const t = tokens.find(t => t.ethAddress === account.address && t.symbol === state.token); + let txAmount = web3.toHex(web3.toWei(currentState.amount, 'ether')); + let txAddress = currentState.address; + if (isToken) { + const t = getState().tokens.find(t => t.ethAddress === account.address && t.symbol === currentState.token); const contract = web3instance.erc20.at(t.address); - data = contract.transfer.getData(state.address, state.amount, { + data = contract.transfer.getData(currentState.address, currentState.amount, { from: account.address, - gasLimit: state.gasLimit, - gasPrice: state.gasPrice + gasLimit: currentState.gasLimit, + gasPrice: currentState.gasPrice }); txAmount = '0x00'; txAddress = t.address; } - - const txData = { address_n, // from: currentAddress.address @@ -655,8 +670,8 @@ export const onSend = (): any => { //chainId: 3 // ropsten chainId: web3instance.chainId, nonce: web3.toHex(account.nonce), - gasLimit: web3.toHex(state.gasLimit), - gasPrice: web3.toHex( EthereumjsUnits.convert(state.gasPrice, 'gwei', 'wei') ), + gasLimit: web3.toHex(currentState.gasLimit), + gasPrice: web3.toHex( EthereumjsUnits.convert(currentState.gasPrice, 'gwei', 'wei') ), r: '', s: '', v: '' @@ -721,7 +736,7 @@ export const onSend = (): any => { // console.log("---->GASSS", txData, gasLimit2.toString() ); const { config } = getState().localStorage; - const selectedCoin = config.coins.find(c => c.network === state.network); + const selectedCoin = config.coins.find(c => c.network === currentState.network); try { const tx = new EthereumjsTx(txData); @@ -731,8 +746,8 @@ export const onSend = (): any => { dispatch({ type: SEND.TX_COMPLETE, address: account, - token: state.token, - amount: state.amount, + token: currentState.token, + amount: currentState.amount, txid, txData, }); diff --git a/src/js/actions/SummaryActions.js b/src/js/actions/SummaryActions.js index 7f7b39a6..3faf89c9 100644 --- a/src/js/actions/SummaryActions.js +++ b/src/js/actions/SummaryActions.js @@ -15,20 +15,9 @@ import { findSelectedDevice } from '../reducers/TrezorConnectReducer'; export const init = (): any => { return (dispatch, getState): void => { - const { location } = getState().router; - const urlParams = location.params; - - const selected = findSelectedDevice( getState().connect ); - if (!selected || !selected.state) return; - + const state: State = { ...initialState, - deviceState: selected.state, - deviceId: selected.features.device_id, - deviceInstance: selected.instance, - accountIndex: parseInt(urlParams.address), - network: urlParams.network, - location: location.pathname, }; dispatch({ @@ -42,12 +31,12 @@ export const init = (): any => { export const update = (newProps: any): any => { return (dispatch, getState): void => { const { - summary, + abstractAccount, router } = getState(); - const isLocationChanged: boolean = router.location.pathname !== summary.location; - if (isLocationChanged || !summary.deviceState) { + const isLocationChanged: boolean = router.location.pathname !== abstractAccount.location; + if (isLocationChanged) { dispatch( init() ); return; } diff --git a/src/js/components/wallet/Receive.js b/src/js/components/wallet/Receive.js index d6c8b3ef..354b002d 100644 --- a/src/js/components/wallet/Receive.js +++ b/src/js/components/wallet/Receive.js @@ -11,26 +11,21 @@ import { QRCode } from 'react-qr-svg'; import AbstractAccount from './account/AbstractAccount'; import { Notification } from '../common/Notification'; import * as ReceiveActions from '../../actions/ReceiveActions'; +import * as AbstractAccountActions from '../../actions/AbstractAccountActions'; class Receive extends AbstractAccount { render() { - return super.render(this.props.receive) || _render(this.props, this.device, this.account, this.deviceStatusNotification); + return super.render() || _render(this.props, this.device, this.account, this.deviceStatusNotification); } } const _render = (props: any, device, account, deviceStatusNotification): any => { const { - network, - deviceState, - accountIndex, addressVerified, addressUnverified, } = props.receive; - // const device = props.devices.find(d => d.state === deviceState); - // const account = props.accounts.find(a => a.deviceState === deviceState && a.index === accountIndex && a.network === network); - let qrCode = null; let address = `${account.address.substring(0, 20)}...`; let className = 'address hidden'; @@ -91,6 +86,7 @@ const _render = (props: any, device, account, deviceStatusNotification): any => const mapStateToProps = (state, own) => { return { + abstractAccount: state.abstractAccount, location: state.router.location, devices: state.connect.devices, accounts: state.accounts, @@ -101,6 +97,7 @@ const mapStateToProps = (state, own) => { const mapDispatchToProps = (dispatch) => { return { + abstractAccountActions: bindActionCreators(AbstractAccountActions, dispatch), initAccount: bindActionCreators(ReceiveActions.init, dispatch), updateAccount: bindActionCreators(ReceiveActions.update, dispatch), disposeAccount: bindActionCreators(ReceiveActions.dispose, dispatch), diff --git a/src/js/components/wallet/account/AbstractAccount.js b/src/js/components/wallet/account/AbstractAccount.js index 482a05f5..386b6b9f 100644 --- a/src/js/components/wallet/account/AbstractAccount.js +++ b/src/js/components/wallet/account/AbstractAccount.js @@ -32,6 +32,7 @@ export default class AbstractAccount extends Component { } componentDidMount() { + this.props.abstractAccountActions.init(); this.props.initAccount(); } @@ -41,10 +42,12 @@ export default class AbstractAccount extends Component { this.account = null; this.deviceStatusNotification = null; + this.props.abstractAccountActions.update(); this.props.updateAccount(); } componentWillUnmount() { + this.props.abstractAccountActions.dispose(); this.props.disposeAccount(); this.device = null; @@ -53,21 +56,22 @@ export default class AbstractAccount extends Component { this.deviceStatusNotification = null; } - render(state: any): any { + render(): any { const props = this.props; + const state = props.abstractAccount; if (!state.deviceState) { return (
); } - const device = findDevice(this.props.devices, state.deviceState, state.deviceId, state.deviceInstance); + const device = findDevice(props.devices, state.deviceState, state.deviceId, state.deviceInstance); if (!device) { return (
Device with state {state.deviceState} not found
); } const discovery = props.discovery.find(d => d.deviceState === device.state && d.network === state.network); - const account = props.accounts.find(a => a.deviceState === state.deviceState && a.index === state.accountIndex && a.network === state.network); + const account = props.accounts.find(a => a.deviceState === state.deviceState && a.index === state.index && a.network === state.network); let deviceStatusNotification = null; if (!account) { diff --git a/src/js/components/wallet/send/AdvancedForm.js b/src/js/components/wallet/send/AdvancedForm.js index bfeb19ff..badaaebb 100644 --- a/src/js/components/wallet/send/AdvancedForm.js +++ b/src/js/components/wallet/send/AdvancedForm.js @@ -6,8 +6,8 @@ import Tooltip from 'rc-tooltip'; const AdvancedForm = (props: any): any => { + const { network } = props.abstractAccount; const { - network, token, gasPrice, gasLimit, diff --git a/src/js/components/wallet/send/SendForm.js b/src/js/components/wallet/send/SendForm.js index 8aae9733..d4219288 100644 --- a/src/js/components/wallet/send/SendForm.js +++ b/src/js/components/wallet/send/SendForm.js @@ -11,23 +11,20 @@ import AbstractAccount from '../account/AbstractAccount'; export default class Send extends AbstractAccount { render() { - return super.render(this.props.sendForm) || _render(this.props, this.device, this.discovery, this.account, this.deviceStatusNotification); + return super.render() || _render(this.props, this.device, this.discovery, this.account, this.deviceStatusNotification); } } const _render = (props: any, device, discovery, account, deviceStatusNotification): any => { - // const device = props.devices.find(device => device.state === props.sendForm.deviceState); - // const discovery = props.discovery.find(d => d.deviceState === device.state && d.network === props.sendForm.network); - // const account = props.accounts.find(a => a.deviceState === props.sendForm.deviceState && a.index === props.sendForm.accountIndex && a.network === props.sendForm.network); const addressTokens = props.tokens.filter(t => t.ethAddress === account.address); + const { network } = props.abstractAccount; const { address, amount, setMax, - network, coinSymbol, token, feeLevels, @@ -54,7 +51,7 @@ const _render = (props: any, device, discovery, account, deviceStatusNotificatio const { config } = props.localStorage; const selectedCoin = config.coins.find(c => c.network === network); - const fiatRate = props.fiat.find(f => f.network === selectedCoin.network); + const fiatRate = props.fiat.find(f => f.network === network); const tokens = addressTokens.map(t => { return { value: t.symbol, label: t.symbol }; diff --git a/src/js/components/wallet/send/index.js b/src/js/components/wallet/send/index.js index 5dc31053..86b6bdcc 100644 --- a/src/js/components/wallet/send/index.js +++ b/src/js/components/wallet/send/index.js @@ -6,10 +6,12 @@ import { bindActionCreators } from 'redux'; import { connect } from 'react-redux'; import * as SendFormActions from '../../../actions/SendFormActions'; +import * as AbstractAccountActions from '../../../actions/AbstractAccountActions'; import SendForm from './SendForm'; const mapStateToProps = (state, own) => { return { + abstractAccount: state.abstractAccount, location: state.router.location, devices: state.connect.devices, accounts: state.accounts, @@ -24,6 +26,7 @@ const mapStateToProps = (state, own) => { const mapDispatchToProps = (dispatch) => { return { + abstractAccountActions: bindActionCreators(AbstractAccountActions, dispatch), sendFormActions: bindActionCreators(SendFormActions, dispatch), initAccount: bindActionCreators(SendFormActions.init, dispatch), updateAccount: bindActionCreators(SendFormActions.update, dispatch), diff --git a/src/js/components/wallet/summary/Summary.js b/src/js/components/wallet/summary/Summary.js index fcf4d480..1c7d22c4 100644 --- a/src/js/components/wallet/summary/Summary.js +++ b/src/js/components/wallet/summary/Summary.js @@ -14,17 +14,13 @@ import { findDevice } from '../../../utils/reducerUtils'; export default class Summary extends AbstractAccount { render() { - console.warn("RENDER SUMMARY!", this.device, this.discovery, this) - return super.render(this.props.summary) || _render(this.props, this.device, this.discovery, this.account, this.deviceStatusNotification); + return super.render() || _render(this.props, this.device, this.discovery, this.account, this.deviceStatusNotification); } } const _render = (props: any, device, discovery, account, deviceStatusNotification): any => { - //const device = props.devices.find(d => d.state === props.summary.deviceState && d.features.device_id === props.summary.deviceId); - // const device = findDevice(props.devices, props.summary.deviceState, props.summary.deviceId); - // const discovery = props.discovery.find(d => d.deviceState === device.state && d.network === props.summary.network); - //const account = props.accounts.find(a => a.deviceState === props.summary.deviceState && a.index === props.summary.accountIndex && a.network === props.summary.network); + const abstractAccount = props.abstractAccount; const tokens = props.tokens.filter(t => t.ethAddress === account.address); return ( @@ -32,12 +28,12 @@ const _render = (props: any, device, discovery, account, deviceStatusNotificatio
{ deviceStatusNotification } -

Address #{ parseInt(props.match.params.address) + 1 }

+

Address #{ parseInt(abstractAccount.index) + 1 }

diff --git a/src/js/components/wallet/summary/index.js b/src/js/components/wallet/summary/index.js index 2f85cd64..f7ce74d6 100644 --- a/src/js/components/wallet/summary/index.js +++ b/src/js/components/wallet/summary/index.js @@ -6,15 +6,19 @@ import { bindActionCreators } from 'redux'; import { connect } from 'react-redux'; import Summary from './Summary'; +import * as AbstractAccountActions from '../../../actions/AbstractAccountActions'; import * as SummaryActions from '../../../actions/SummaryActions'; const mapStateToProps = (state, own) => { return { + abstractAccount: state.abstractAccount, + location: state.router.location, devices: state.connect.devices, accounts: state.accounts, discovery: state.discovery, tokens: state.tokens, + summary: state.summary, fiat: state.fiat, localStorage: state.localStorage @@ -23,6 +27,7 @@ const mapStateToProps = (state, own) => { const mapDispatchToProps = (dispatch) => { return { + abstractAccountActions: bindActionCreators(AbstractAccountActions, dispatch), summaryActions: bindActionCreators(SummaryActions, dispatch), initAccount: bindActionCreators(SummaryActions.init, dispatch), updateAccount: bindActionCreators(SummaryActions.update, dispatch), diff --git a/src/js/reducers/AbstractAccountReducer.js b/src/js/reducers/AbstractAccountReducer.js index f56a44dc..f9d65f59 100644 --- a/src/js/reducers/AbstractAccountReducer.js +++ b/src/js/reducers/AbstractAccountReducer.js @@ -6,7 +6,9 @@ import * as CONNECT from '../actions/constants/TrezorConnect'; export type State = { +index: number; - +ch: ?string; + +deviceState: ?string; + +deviceId: ?string; + +deviceInstance: ?string; +network: string; location: string; } @@ -14,7 +16,10 @@ export type State = { export const initialState: State = { index: 0, deviceState: null, + deviceId: null, + deviceInstance: null, network: '', + location: '', }; @@ -25,12 +30,6 @@ export default (state: State = initialState, action: any): State => { case ACCOUNT.INIT : return action.state; - case CONNECT.DEVICE_STATE_EXCEPTION : - return { - ...state, - deviceStateError: true - } - case ACCOUNT.DISPOSE : return initialState; diff --git a/src/js/reducers/AccountsReducer.js b/src/js/reducers/AccountsReducer.js index 79242a6a..ccf07b21 100644 --- a/src/js/reducers/AccountsReducer.js +++ b/src/js/reducers/AccountsReducer.js @@ -18,6 +18,10 @@ export type Account = { const initialState: Array = []; +export const findAccount = (state: Array, index: number, deviceState: string, network: string): ?Account => { + return state.find(a => a.deviceState === deviceState && a.index === index && a.network === network); +} + const createAccount = (state: Array, action: any): Array => { // TODO check with device_id diff --git a/src/js/reducers/ReceiveReducer.js b/src/js/reducers/ReceiveReducer.js index 7f079f11..cccdb20a 100644 --- a/src/js/reducers/ReceiveReducer.js +++ b/src/js/reducers/ReceiveReducer.js @@ -4,23 +4,11 @@ import * as RECEIVE from '../actions/constants/receive'; export type State = { - +deviceState: ?string; - +deviceId: ?string; - +deviceInstance: ?string; - +accountIndex: ?number; - +network: ?string; - location: string; addressVerified: boolean; adressUnverified: boolean; } export const initialState: State = { - deviceState: null, - deviceId: null, - deviceInstance: null, - accountIndex: null, - network: null, - location: '', addressVerified: false, addressUnverified: false, }; diff --git a/src/js/reducers/SendFormReducer.js b/src/js/reducers/SendFormReducer.js index 7899e9f3..769ce823 100644 --- a/src/js/reducers/SendFormReducer.js +++ b/src/js/reducers/SendFormReducer.js @@ -10,18 +10,10 @@ import BigNumber from 'bignumber.js'; import { getFeeLevels } from '../actions/SendFormActions'; export type State = { - +deviceState: ?string; - +deviceId: ?string; - +deviceInstance: ?string; - +accountIndex: number; - +network: string; +coinSymbol: string; token: string; - location: string; - balanceNeedUpdate: boolean; - // form fields advanced: boolean; untouched: boolean; // set to true when user made any changes in form @@ -52,14 +44,8 @@ export type FeeLevel = { export const initialState: State = { - deviceState: null, - deviceId: null, - deviceInstance: null, - accountIndex: 0, - network: '', coinSymbol: '', token: '', - location: '', advanced: false, untouched: true, diff --git a/src/js/reducers/SummaryReducer.js b/src/js/reducers/SummaryReducer.js index 415fa5cb..30763e15 100644 --- a/src/js/reducers/SummaryReducer.js +++ b/src/js/reducers/SummaryReducer.js @@ -4,25 +4,11 @@ import * as SUMMARY from '../actions/constants/summary'; export type State = { - +deviceState: ?string; - +deviceId: ?string; - +deviceInstance: ?string; - +accountIndex: ?number; - +network: ?string; - location: string; - details: boolean; selectedToken: any; } export const initialState: State = { - deviceState: null, - deviceId: null, - deviceInstance: null, - accountIndex: null, - network: null, - location: '', - details: true, selectedToken: null };