1
0
mirror of https://github.com/trezor/trezor-wallet synced 2025-05-01 22:49:12 +00:00

separate clear from init function

This commit is contained in:
slowbackspace 2019-01-15 15:24:41 +01:00
parent c77cd10f03
commit a8d39d95c2
5 changed files with 62 additions and 32 deletions

View File

@ -18,19 +18,17 @@ import * as EthereumSendFormActions from './ethereum/SendFormActions';
import * as RippleSendFormActions from './ripple/SendFormActions'; import * as RippleSendFormActions from './ripple/SendFormActions';
export type SendFormAction = { export type SendFormAction = {
type: typeof SEND.INIT | typeof SEND.VALIDATION | typeof SEND.CHANGE, type: typeof SEND.INIT | typeof SEND.VALIDATION | typeof SEND.CHANGE | typeof SEND.CLEAR,
networkType: 'ethereum', networkType: 'ethereum',
state: EthereumState, state: EthereumState,
} | { } | {
type: typeof SEND.INIT | typeof SEND.VALIDATION | typeof SEND.CHANGE, type: typeof SEND.INIT | typeof SEND.VALIDATION | typeof SEND.CHANGE | typeof SEND.CLEAR,
networkType: 'ripple', networkType: 'ripple',
state: RippleState, state: RippleState,
} | { } | {
type: typeof SEND.TOGGLE_ADVANCED | typeof SEND.TX_SENDING | typeof SEND.TX_ERROR, type: typeof SEND.TOGGLE_ADVANCED | typeof SEND.TX_SENDING | typeof SEND.TX_ERROR,
} | { } | {
type: typeof SEND.TX_COMPLETE, type: typeof SEND.TX_COMPLETE,
} | {
type: typeof SEND.CLEAR,
}; };

View File

@ -53,13 +53,6 @@ export const observe = (prevState: ReducersState, action: Action): ThunkAction =
return; return;
} }
// clear transaction draft from session storage and reinitialize send form
if (action.type === SEND.CLEAR) {
dispatch(SessionStorageActions.clear());
dispatch(init());
return;
}
// if send form was not initialized // if send form was not initialized
if (currentState.sendFormEthereum.currency === '') { if (currentState.sendFormEthereum.currency === '') {
dispatch(init()); dispatch(init());
@ -116,8 +109,6 @@ export const init = (): AsyncAction => async (dispatch: Dispatch, getState: GetS
network, network,
} = getState().selectedAccount; } = getState().selectedAccount;
const { advanced } = getState().sendFormEthereum;
if (!account || !network) return; if (!account || !network) return;
const stateFromStorage = dispatch(SessionStorageActions.loadEthereumDraftTransaction()); const stateFromStorage = dispatch(SessionStorageActions.loadEthereumDraftTransaction());
@ -149,7 +140,6 @@ export const init = (): AsyncAction => async (dispatch: Dispatch, getState: GetS
recommendedGasPrice: gasPrice.toString(), recommendedGasPrice: gasPrice.toString(),
gasLimit, gasLimit,
gasPrice: gasPrice.toString(), gasPrice: gasPrice.toString(),
advanced,
}, },
}); });
}; };
@ -165,10 +155,37 @@ export const toggleAdvanced = (): Action => ({
/* /*
* Called from UI from "clear" button * Called from UI from "clear" button
*/ */
export const onClear = (): Action => ({ export const onClear = (): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
type: SEND.CLEAR, const { network } = getState().selectedAccount;
networkType: 'ethereum', const { advanced } = getState().sendFormEthereum;
});
if (!network) return;
// clear transaction draft from session storage
dispatch(SessionStorageActions.clear());
const gasPrice: BigNumber = await dispatch(BlockchainActions.getGasPrice(network.shortcut, network.defaultGasPrice));
const gasLimit = network.defaultGasLimit.toString();
const feeLevels = ValidationActions.getFeeLevels(network.symbol, gasPrice, gasLimit);
const selectedFeeLevel = ValidationActions.getSelectedFeeLevel(feeLevels, initialState.selectedFeeLevel);
dispatch({
type: SEND.CLEAR,
networkType: 'ethereum',
state: {
...initialState,
networkName: network.shortcut,
networkSymbol: network.symbol,
currency: network.symbol,
feeLevels,
selectedFeeLevel,
recommendedGasPrice: gasPrice.toString(),
gasLimit,
gasPrice: gasPrice.toString(),
advanced,
},
});
};
/* /*
* Called from UI on "address" field change * Called from UI on "address" field change

View File

@ -35,13 +35,6 @@ export const observe = (prevState: ReducersState, action: Action): ThunkAction =
return; return;
} }
// clear transaction draft from session storage and reinitialize send form
if (action.type === SEND.CLEAR) {
dispatch(SessionStorageActions.clear());
dispatch(init());
return;
}
// if send form was not initialized // if send form was not initialized
if (currentState.sendFormRipple.networkSymbol === '') { if (currentState.sendFormRipple.networkSymbol === '') {
dispatch(init()); dispatch(init());
@ -87,8 +80,6 @@ export const init = (): AsyncAction => async (dispatch: Dispatch, getState: GetS
network, network,
} = getState().selectedAccount; } = getState().selectedAccount;
const { advanced } = getState().sendFormEthereum;
if (!account || account.networkType !== 'ripple' || !network) return; if (!account || account.networkType !== 'ripple' || !network) return;
const stateFromStorage = dispatch(SessionStorageActions.loadRippleDraftTransaction()); const stateFromStorage = dispatch(SessionStorageActions.loadRippleDraftTransaction());
@ -116,7 +107,6 @@ export const init = (): AsyncAction => async (dispatch: Dispatch, getState: GetS
selectedFeeLevel, selectedFeeLevel,
fee: network.fee.defaultFee, fee: network.fee.defaultFee,
sequence: '1', sequence: '1',
advanced,
}, },
}); });
}; };
@ -132,11 +122,34 @@ export const toggleAdvanced = (): Action => ({
/* /*
* Called from UI from "clear" button * Called from UI from "clear" button
*/ */
export const onClear = (): Action => ({ export const onClear = (): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
type: SEND.CLEAR, const { network } = getState().selectedAccount;
networkType: 'ripple', const { advanced } = getState().sendFormRipple;
});
if (!network) return;
// clear transaction draft from session storage
dispatch(SessionStorageActions.clear());
const blockchainFeeLevels = dispatch(BlockchainActions.getFeeLevels(network));
const feeLevels = dispatch(ValidationActions.getFeeLevels(blockchainFeeLevels));
const selectedFeeLevel = ValidationActions.getSelectedFeeLevel(feeLevels, initialState.selectedFeeLevel);
dispatch({
type: SEND.CLEAR,
networkType: 'ripple',
state: {
...initialState,
networkName: network.shortcut,
networkSymbol: network.symbol,
feeLevels,
selectedFeeLevel,
fee: network.fee.defaultFee,
sequence: '1',
advanced,
},
});
};
/* /*
* Called from UI on "address" field change * Called from UI on "address" field change

View File

@ -82,6 +82,7 @@ export default (state: State = initialState, action: Action): State => {
case SEND.INIT: case SEND.INIT:
case SEND.CHANGE: case SEND.CHANGE:
case SEND.VALIDATION: case SEND.VALIDATION:
case SEND.CLEAR:
return action.state; return action.state;
case SEND.TOGGLE_ADVANCED: case SEND.TOGGLE_ADVANCED:

View File

@ -75,6 +75,7 @@ export default (state: State = initialState, action: Action): State => {
case SEND.INIT: case SEND.INIT:
case SEND.CHANGE: case SEND.CHANGE:
case SEND.VALIDATION: case SEND.VALIDATION:
case SEND.CLEAR:
return action.state; return action.state;
case SEND.TOGGLE_ADVANCED: case SEND.TOGGLE_ADVANCED: