1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-14 12:29:31 +00:00
trezor-wallet/src/js/actions/SelectedAccountActions.js

115 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-02-20 09:30:36 +00:00
/* @flow */
'use strict';
import { LOCATION_CHANGE } from 'react-router-redux';
2018-02-20 09:30:36 +00:00
import * as ACCOUNT from './constants/account';
import * as SEND from './constants/send';
import * as SendFormActions from './SendFormActions';
import * as SessionStorageActions from './SessionStorageActions';
import * as stateUtils from '../reducers/utils';
2018-02-20 09:30:36 +00:00
2018-05-18 17:26:46 +00:00
import { initialState } from '../reducers/SelectedAccountReducer';
2018-04-16 21:19:50 +00:00
import type {
Coin,
TrezorDevice,
AsyncAction,
ThunkAction,
Action,
GetState,
Dispatch,
State,
} from '~/flowtype';
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,
} | {
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
export const updateSelectedValues = (prevState: State, action: Action): AsyncAction => {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
2018-02-20 09:30:36 +00:00
const locationChange: boolean = action.type === LOCATION_CHANGE;
const state: State = getState();
const location = state.router.location;
2018-05-28 09:21:47 +00:00
const prevLocation = prevState.router.location;
2018-02-20 09:30:36 +00:00
let needUpdate: boolean = false;
2018-02-20 09:30:36 +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
// handle devices state change (from trezor-connect events or location change)
if (locationChange
|| prevState.accounts !== state.accounts
|| prevState.discovery !== state.discovery
|| prevState.tokens !== state.tokens
2018-05-28 09:21:47 +00:00
|| prevState.pending !== state.pending
|| prevState.web3 !== state.web3) {
const account = stateUtils.getSelectedAccount(state);
const network = stateUtils.getSelectedNetwork(state);
const discovery = stateUtils.getDiscoveryProcess(state);
const tokens = stateUtils.getAccountTokens(state, account);
2018-05-28 09:21:47 +00:00
const pending = stateUtils.getAccountPendingTx(state.pending, account);
const web3 = stateUtils.getWeb3(state);
2018-02-20 09:30:36 +00:00
const payload: $ElementType<State, 'selectedAccount'> = {
// location: location.pathname,
account,
network,
discovery,
tokens,
pending,
web3
}
2018-04-16 21:19:50 +00:00
let needUpdate: boolean = false;
Object.keys(payload).forEach((key) => {
if (payload[key] !== state.selectedAccount[key]) {
needUpdate = true;
}
})
if (needUpdate) {
dispatch({
type: ACCOUNT.UPDATE_SELECTED_ACCOUNT,
payload,
2018-05-28 09:21:47 +00:00
});
}
if (locationChange) {
2018-02-20 09:30:36 +00:00
2018-05-28 09:21:47 +00:00
if (prevLocation) {
// save form data to session storage
// TODO: move to state.sendForm on change event
if (prevLocation.state.send) {
SessionStorageActions.save(prevState.router.location.pathname, state.sendForm);
}
}
dispatch( dispose() );
2018-05-28 14:56:34 +00:00
}
if (needUpdate) {
if (location.state.send && state.sendForm.currency === "") {
dispatch( SendFormActions.init( SessionStorageActions.load(location.pathname) ) );
}
}
2018-02-20 09:30:36 +00:00
}
}
}
2018-04-16 21:19:50 +00:00
export const dispose = (): Action => {
return {
type: ACCOUNT.DISPOSE
2018-02-20 09:30:36 +00:00
}
}