2018-02-20 09:30:36 +00:00
|
|
|
/* @flow */
|
2018-07-30 10:52:13 +00:00
|
|
|
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-05-25 09:26:36 +00:00
|
|
|
import { LOCATION_CHANGE } from 'react-router-redux';
|
2018-08-14 13:18:16 +00:00
|
|
|
import * as ACCOUNT from 'actions/constants/account';
|
|
|
|
import * as SEND from 'actions/constants/send';
|
|
|
|
import * as NOTIFICATION from 'actions/constants/notification';
|
|
|
|
import * as PENDING from 'actions/constants/pendingTx';
|
2018-05-25 09:26:36 +00:00
|
|
|
|
2018-08-14 13:18:16 +00:00
|
|
|
import * as stateUtils from 'reducers/utils';
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-08-14 13:18:16 +00:00
|
|
|
import { initialState } from 'reducers/SelectedAccountReducer';
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2018-05-25 09:26:36 +00:00
|
|
|
import type {
|
|
|
|
Coin,
|
|
|
|
TrezorDevice,
|
|
|
|
AsyncAction,
|
|
|
|
ThunkAction,
|
2018-07-30 10:52:13 +00:00
|
|
|
Action,
|
|
|
|
GetState,
|
2018-05-25 09:26:36 +00:00
|
|
|
Dispatch,
|
|
|
|
State,
|
2018-08-14 12:56:47 +00:00
|
|
|
} from 'flowtype';
|
2018-08-14 14:06:34 +00:00
|
|
|
import * as SessionStorageActions from './SessionStorageActions';
|
|
|
|
import * as SendFormActions from './SendFormActions';
|
2018-05-25 09:26:36 +00:00
|
|
|
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2018-05-18 17:26:46 +00:00
|
|
|
export type SelectedAccountAction = {
|
2018-04-16 21:19:50 +00:00
|
|
|
type: typeof ACCOUNT.DISPOSE,
|
2018-05-25 09:26:36 +00:00
|
|
|
} | {
|
|
|
|
type: typeof ACCOUNT.UPDATE_SELECTED_ACCOUNT,
|
|
|
|
payload: $ElementType<State, 'selectedAccount'>
|
2018-04-16 21:19:50 +00:00
|
|
|
};
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
export const updateSelectedValues = (prevState: State, action: Action): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
|
|
|
const locationChange: boolean = action.type === LOCATION_CHANGE;
|
|
|
|
const state: State = getState();
|
|
|
|
const location = state.router.location;
|
|
|
|
const prevLocation = prevState.router.location;
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
const needUpdate: boolean = false;
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
// reset form to default
|
|
|
|
if (action.type === SEND.TX_COMPLETE) {
|
|
|
|
// dispatch( SendFormActions.init() );
|
|
|
|
// linear action
|
|
|
|
// SessionStorageActions.clear(location.pathname);
|
|
|
|
}
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
// handle devices state change (from trezor-connect events or location change)
|
|
|
|
if (locationChange
|
2018-05-25 09:26:36 +00:00
|
|
|
|| prevState.accounts !== state.accounts
|
|
|
|
|| prevState.discovery !== state.discovery
|
|
|
|
|| prevState.tokens !== state.tokens
|
2018-05-28 09:21:47 +00:00
|
|
|
|| prevState.pending !== state.pending
|
2018-05-25 09:26:36 +00:00
|
|
|
|| prevState.web3 !== state.web3) {
|
2018-07-30 10:52:13 +00:00
|
|
|
if (locationChange) {
|
|
|
|
// dispose current account view
|
|
|
|
dispatch(dispose());
|
|
|
|
}
|
2018-05-28 17:48:07 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
const account = stateUtils.getSelectedAccount(state);
|
|
|
|
const network = stateUtils.getSelectedNetwork(state);
|
|
|
|
const discovery = stateUtils.getDiscoveryProcess(state);
|
|
|
|
const tokens = stateUtils.getAccountTokens(state, account);
|
|
|
|
const pending = stateUtils.getAccountPendingTx(state.pending, account);
|
|
|
|
const web3 = stateUtils.getWeb3(state);
|
|
|
|
|
|
|
|
const payload: $ElementType<State, 'selectedAccount'> = {
|
2018-08-16 17:19:54 +00:00
|
|
|
location: location.pathname,
|
2018-07-30 10:52:13 +00:00
|
|
|
account,
|
|
|
|
network,
|
|
|
|
discovery,
|
|
|
|
tokens,
|
|
|
|
pending,
|
|
|
|
web3,
|
|
|
|
};
|
2018-05-28 17:48:07 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
let needUpdate: boolean = false;
|
|
|
|
Object.keys(payload).forEach((key) => {
|
|
|
|
if (Array.isArray(payload[key])) {
|
|
|
|
if (Array.isArray(state.selectedAccount[key]) && payload[key].length !== state.selectedAccount[key].length) {
|
|
|
|
needUpdate = true;
|
2018-05-25 09:26:36 +00:00
|
|
|
}
|
2018-07-30 10:52:13 +00:00
|
|
|
} else if (payload[key] !== state.selectedAccount[key]) {
|
|
|
|
needUpdate = true;
|
|
|
|
}
|
|
|
|
});
|
2018-05-25 09:26:36 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
if (needUpdate) {
|
|
|
|
dispatch({
|
|
|
|
type: ACCOUNT.UPDATE_SELECTED_ACCOUNT,
|
|
|
|
payload,
|
|
|
|
});
|
2018-05-25 09:26:36 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
// initialize SendFormReducer
|
|
|
|
if (location.state.send && getState().sendForm.currency === '') {
|
|
|
|
dispatch(SendFormActions.init());
|
|
|
|
}
|
2018-05-29 08:48:28 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
if (location.state.send) {
|
|
|
|
const rejectedTxs = pending.filter(tx => tx.rejected);
|
|
|
|
rejectedTxs.forEach((tx) => {
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATION.ADD,
|
|
|
|
payload: {
|
|
|
|
type: 'warning',
|
|
|
|
title: 'Pending transaction rejected',
|
|
|
|
message: `Transaction with id: ${tx.id} not found.`,
|
|
|
|
cancelable: true,
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
label: 'OK',
|
|
|
|
callback: () => {
|
|
|
|
dispatch({
|
|
|
|
type: PENDING.TX_RESOLVED,
|
|
|
|
tx,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2018-05-29 08:48:28 +00:00
|
|
|
});
|
2018-07-30 10:52:13 +00:00
|
|
|
});
|
2018-05-25 09:26:36 +00:00
|
|
|
}
|
2018-02-20 09:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-30 10:52:13 +00:00
|
|
|
};
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
export const dispose = (): Action => ({
|
|
|
|
type: ACCOUNT.DISPOSE,
|
|
|
|
});
|