1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-24 09:18:09 +00:00

fixed abstractAccount default State

This commit is contained in:
Szymon Lesisz 2018-05-10 18:36:25 +02:00
parent cf863664b7
commit c3079bc8d9
9 changed files with 54 additions and 59 deletions

View File

@ -25,6 +25,7 @@ export const init = (): ThunkAction => {
const selected: ?TrezorDevice = findSelectedDevice( getState().connect ); const selected: ?TrezorDevice = findSelectedDevice( getState().connect );
if (!selected) return; if (!selected) return;
if (!selected.state || !selected.features) return;
const { config } = getState().localStorage; const { config } = getState().localStorage;
const coin: ?Coin = config.coins.find(c => c.network === urlParams.network); const coin: ?Coin = config.coins.find(c => c.network === urlParams.network);
@ -54,10 +55,11 @@ export const update = (initAccountAction: () => ThunkAction): ThunkAction => {
abstractAccount, abstractAccount,
router router
} = getState(); } = getState();
const isLocationChanged: boolean = router.location.pathname !== abstractAccount.location; const isLocationChanged: boolean = (!abstractAccount || router.location.pathname !== abstractAccount.location);
if (isLocationChanged) { if (isLocationChanged) {
dispatch( init() ); dispatch( init() );
initAccountAction(); if (abstractAccount !== null)
initAccountAction();
} }
} }
} }

View File

@ -159,7 +159,7 @@ export const getFeeLevels = (symbol: string, gasPrice: BigNumber | string, gasLi
export const init = (): ThunkAction => { export const init = (): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const accountState: AccountState = getState().abstractAccount; const accountState: ?AccountState = getState().abstractAccount;
const { location } = getState().router; const { location } = getState().router;
const urlParams: RouterLocationState = location.state; const urlParams: RouterLocationState = location.state;
@ -221,7 +221,9 @@ export const toggleAdvanced = (address: string): Action => {
export const validation = (): ThunkAction => { export const validation = (): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const accountState: AccountState = getState().abstractAccount; const accountState: ?AccountState = getState().abstractAccount;
if (!accountState) return;
const state: State = getState().sendForm; const state: State = getState().sendForm;
const errors: {[k: string]: string} = {}; const errors: {[k: string]: string} = {};
@ -380,7 +382,7 @@ export const onAddressChange = (address: string): ThunkAction => {
export const onAmountChange = (amount: string): ThunkAction => { export const onAmountChange = (amount: string): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const accountState: AccountState = getState().abstractAccount; const accountState: ?AccountState = getState().abstractAccount;
const currentState: State = getState().sendForm; const currentState: State = getState().sendForm;
const isToken: boolean = currentState.selectedCurrency !== currentState.coinSymbol; const isToken: boolean = currentState.selectedCurrency !== currentState.coinSymbol;
const touched = { ...currentState.touched }; const touched = { ...currentState.touched };
@ -406,7 +408,8 @@ export const onAmountChange = (amount: string): ThunkAction => {
export const onCurrencyChange = (currency: any): ThunkAction => { export const onCurrencyChange = (currency: any): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const accountState: AccountState = getState().abstractAccount; const accountState: ?AccountState = getState().abstractAccount;
if (!accountState) return;
const currentState: State = getState().sendForm; const currentState: State = getState().sendForm;
const isToken: boolean = currency.value !== currentState.coinSymbol; const isToken: boolean = currency.value !== currentState.coinSymbol;
@ -465,7 +468,8 @@ export const onCurrencyChange = (currency: any): ThunkAction => {
export const onSetMax = (): ThunkAction => { export const onSetMax = (): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const accountState: AccountState = getState().abstractAccount; const accountState: ?AccountState = getState().abstractAccount;
if (!accountState) return;
const currentState: State = getState().sendForm; const currentState: State = getState().sendForm;
const isToken: boolean = currentState.selectedCurrency !== currentState.coinSymbol; const isToken: boolean = currentState.selectedCurrency !== currentState.coinSymbol;
const touched = { ...currentState.touched }; const touched = { ...currentState.touched };
@ -510,14 +514,12 @@ export const onSetMax = (): ThunkAction => {
export const onFeeLevelChange = (feeLevel: FeeLevel): ThunkAction => { export const onFeeLevelChange = (feeLevel: FeeLevel): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const accountState: AccountState = getState().abstractAccount; const accountState: ?AccountState = getState().abstractAccount;
if (!accountState) return;
const currentState: State = getState().sendForm; const currentState: State = getState().sendForm;
const isToken: boolean = currentState.selectedCurrency !== currentState.coinSymbol; const isToken: boolean = currentState.selectedCurrency !== currentState.coinSymbol;
const { config } = getState().localStorage; const coin: Coin = accountState.coin;
if (!config) return;
const coin: ?Coin = config.coins.find(c => c.network === accountState.network);
if (!coin) return;
const state: State = { const state: State = {
...currentState, ...currentState,
@ -563,7 +565,8 @@ export const onFeeLevelChange = (feeLevel: FeeLevel): ThunkAction => {
export const updateFeeLevels = (): ThunkAction => { export const updateFeeLevels = (): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const accountState: AccountState = getState().abstractAccount; const accountState: ?AccountState = getState().abstractAccount;
if (!accountState) return;
const currentState: State = getState().sendForm; const currentState: State = getState().sendForm;
const isToken: boolean = currentState.selectedCurrency !== currentState.coinSymbol; const isToken: boolean = currentState.selectedCurrency !== currentState.coinSymbol;
@ -604,7 +607,8 @@ export const updateFeeLevels = (): ThunkAction => {
export const onGasPriceChange = (gasPrice: string): ThunkAction => { export const onGasPriceChange = (gasPrice: string): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const accountState: AccountState = getState().abstractAccount; const accountState: ?AccountState = getState().abstractAccount;
if (!accountState) return;
const currentState: State = getState().sendForm; const currentState: State = getState().sendForm;
const isToken: boolean = currentState.selectedCurrency !== accountState.network; const isToken: boolean = currentState.selectedCurrency !== accountState.network;
@ -652,7 +656,8 @@ export const onGasPriceChange = (gasPrice: string): ThunkAction => {
export const onGasLimitChange = (gasLimit: string): ThunkAction => { export const onGasLimitChange = (gasLimit: string): ThunkAction => {
return (dispatch: Dispatch, getState: GetState): void => { return (dispatch: Dispatch, getState: GetState): void => {
const accountState: AccountState = getState().abstractAccount; const accountState: ?AccountState = getState().abstractAccount;
if (!accountState) return;
const currentState: State = getState().sendForm; const currentState: State = getState().sendForm;
const isToken: boolean = currentState.selectedCurrency !== currentState.coinSymbol; const isToken: boolean = currentState.selectedCurrency !== currentState.coinSymbol;
@ -723,7 +728,9 @@ export const onSend = (): AsyncAction => {
//return onSendERC20(); //return onSendERC20();
return async (dispatch: Dispatch, getState: GetState): Promise<any> => { return async (dispatch: Dispatch, getState: GetState): Promise<any> => {
const accountState: AccountState = getState().abstractAccount; const accountState: ?AccountState = getState().abstractAccount;
if (!accountState) return;
const currentState: State = getState().sendForm; const currentState: State = getState().sendForm;
const web3instance: ?Web3Instance = getState().web3.filter(w3 => w3.network === accountState.network)[0]; const web3instance: ?Web3Instance = getState().web3.filter(w3 => w3.network === accountState.network)[0];
const account: ?Account = findAccount(getState().accounts, accountState.index, accountState.deviceState, accountState.network); const account: ?Account = findAccount(getState().accounts, accountState.index, accountState.deviceState, accountState.network);

View File

@ -10,6 +10,7 @@ import type { Props } from './index';
const ConfirmAddress = (props: Props) => { const ConfirmAddress = (props: Props) => {
const { accounts, abstractAccount } = props; const { accounts, abstractAccount } = props;
if (!abstractAccount) return null;
const account = findAccount(accounts, abstractAccount.index, abstractAccount.deviceState, abstractAccount.network); const account = findAccount(accounts, abstractAccount.index, abstractAccount.deviceState, abstractAccount.network);
if (!account) return null; if (!account) return null;
@ -46,6 +47,8 @@ export const ConfirmUnverifiedAddress = (props: Props): any => {
showAddress showAddress
} = props.receiveActions; } = props.receiveActions;
if(!abstractAccount) return null;
const account = findAccount(accounts, abstractAccount.index, abstractAccount.deviceState, abstractAccount.network); const account = findAccount(accounts, abstractAccount.index, abstractAccount.deviceState, abstractAccount.network);
if (!account) return null; if (!account) return null;

View File

@ -52,14 +52,14 @@ export default class AbstractAccount<P> extends Component<Props & P, AccountStat
this.props.abstractAccountActions.update( this.props.initAccount ); this.props.abstractAccountActions.update( this.props.initAccount );
const currentState = props.abstractAccount; const accountState = props.abstractAccount;
if (!accountState) return;
const device = findDevice(props.devices, currentState.deviceState, currentState.deviceId, currentState.deviceInstance); const device = findDevice(props.devices, accountState.deviceState, accountState.deviceId, accountState.deviceInstance);
if (!device) return; if (!device) return;
const discovery = props.discovery.find(d => d.deviceState === device.state && d.network === currentState.network); const discovery = props.discovery.find(d => d.deviceState === device.state && d.network === accountState.network);
// if (!discovery) return; // if (!discovery) return;
const account = props.accounts.find(a => a.deviceState === currentState.deviceState && a.index === currentState.index && a.network === currentState.network); const account = props.accounts.find(a => a.deviceState === accountState.deviceState && a.index === accountState.index && a.network === accountState.network);
let deviceStatusNotification: ?React$Element<typeof Notification> = null; let deviceStatusNotification: ?React$Element<typeof Notification> = null;
if (account) { if (account) {
@ -90,9 +90,9 @@ export default class AbstractAccount<P> extends Component<Props & P, AccountStat
render(): ?React$Element<string> { render(): ?React$Element<string> {
const props = this.props; const props = this.props;
const currentState = props.abstractAccount; const accountState = props.abstractAccount;
if (!currentState.deviceState) { if (!accountState) {
return (<section><Notification className="info" title="Loading device" /></section>); return (<section><Notification className="info" title="Loading device" /></section>);
} }
@ -105,7 +105,7 @@ export default class AbstractAccount<P> extends Component<Props & P, AccountStat
// const device = findDevice(props.devices, accountState.deviceState, accountState.deviceId, accountState.deviceInstance); // const device = findDevice(props.devices, accountState.deviceState, accountState.deviceId, accountState.deviceInstance);
if (!device) { if (!device) {
return (<section>Device with state {currentState.deviceState} not found</section>); return (<section>Device with state {accountState.deviceState} not found</section>);
} }
if (!account) { if (!account) {

View File

@ -8,7 +8,10 @@ import type { Props } from './index';
const AdvancedForm = (props: Props) => { const AdvancedForm = (props: Props) => {
const { network } = props.abstractAccount; const abstractAccount = props.abstractAccount;
if (!abstractAccount) return null;
const { network } = abstractAccount;
const { const {
coinSymbol, coinSymbol,
selectedCurrency, selectedCurrency,

View File

@ -28,11 +28,12 @@ const _render = (props: Props, state: AccountState): React$Element<string> => {
discovery, discovery,
deviceStatusNotification deviceStatusNotification
} = state; } = state;
const abstractAccount = props.abstractAccount;
if (!device || !account || !discovery) return <section></section>; if (!device || !account || !discovery || !abstractAccount) return <section></section>;
const tokens = findAccountTokens(props.tokens, account); const tokens = findAccountTokens(props.tokens, account);
const { network } = props.abstractAccount; const { network } = abstractAccount;
const { const {
address, address,
@ -61,7 +62,7 @@ const _render = (props: Props, state: AccountState): React$Element<string> => {
onSend, onSend,
} = props.sendFormActions; } = props.sendFormActions;
const selectedCoin = props.abstractAccount.coin; const selectedCoin = abstractAccount.coin;
const fiatRate = props.fiat.find(f => f.network === network); const fiatRate = props.fiat.find(f => f.network === network);
const tokensSelectData = tokens.map(t => { const tokensSelectData = tokens.map(t => {

View File

@ -33,10 +33,11 @@ const _render = (props: Props, state: AccountState): React$Element<string> => {
account, account,
deviceStatusNotification deviceStatusNotification
} = state; } = state;
if (!device || !account) return <section></section>;
const abstractAccount = props.abstractAccount; const abstractAccount = props.abstractAccount;
if (!device || !account || !abstractAccount) return <section></section>;
const tokens = findAccountTokens(props.tokens, account); const tokens = findAccountTokens(props.tokens, account);
const explorerLink: string = `${abstractAccount.coin.explorer.address}${account.address}`; const explorerLink: string = `${abstractAccount.coin.explorer.address}${account.address}`;

View File

@ -5,9 +5,11 @@ import React from 'react';
import BigNumber from 'bignumber.js'; import BigNumber from 'bignumber.js';
import type { Props as BaseProps } from './index'; import type { Props as BaseProps } from './index';
import type { Coin } from '../../../reducers/LocalStorageReducer';
type Props = { type Props = {
coin: $PropertyType<$ElementType<BaseProps, 'abstractAccount'>, 'coin'>, // coin: $PropertyType<$ElementType<BaseProps, 'abstractAccount'>, 'coin'>,
coin: Coin,
summary: $ElementType<BaseProps, 'summary'>, summary: $ElementType<BaseProps, 'summary'>,
balance: string, balance: string,
network: string, network: string,

View File

@ -14,36 +14,12 @@ export type State = {
+deviceInstance: ?number; +deviceInstance: ?number;
+network: string; +network: string;
+coin: Coin; +coin: Coin;
location: string; +location: string;
}
export const initialState: State = {
index: 0,
deviceState: '0',
deviceId: '0',
deviceInstance: null,
network: '',
coin: {
name: '',
network: '',
symbol: '',
bip44: '',
defaultGasLimit: 0,
defaultGasLimitTokens: 0,
defaultGasPrice: 0,
explorer: {
tx: '',
address: ''
},
tokens: '',
backends: []
},
location: '',
}; };
export const initialState: ?State = null;
export default (state: State = initialState, action: Action): State => { export default (state: ?State = initialState, action: Action): ?State => {
switch (action.type) { switch (action.type) {