From a8d39d95c2256cb7aa3c35e42e39fa4ba278b0cf Mon Sep 17 00:00:00 2001 From: slowbackspace Date: Tue, 15 Jan 2019 15:24:41 +0100 Subject: [PATCH] separate clear from init function --- src/actions/SendFormActions.js | 6 ++-- src/actions/ethereum/SendFormActions.js | 45 +++++++++++++++++-------- src/actions/ripple/SendFormActions.js | 41 ++++++++++++++-------- src/reducers/SendFormEthereumReducer.js | 1 + src/reducers/SendFormRippleReducer.js | 1 + 5 files changed, 62 insertions(+), 32 deletions(-) diff --git a/src/actions/SendFormActions.js b/src/actions/SendFormActions.js index 84d8fcf8..6116c403 100644 --- a/src/actions/SendFormActions.js +++ b/src/actions/SendFormActions.js @@ -18,19 +18,17 @@ import * as EthereumSendFormActions from './ethereum/SendFormActions'; import * as RippleSendFormActions from './ripple/SendFormActions'; 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', 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', state: RippleState, } | { type: typeof SEND.TOGGLE_ADVANCED | typeof SEND.TX_SENDING | typeof SEND.TX_ERROR, } | { type: typeof SEND.TX_COMPLETE, -} | { - type: typeof SEND.CLEAR, }; diff --git a/src/actions/ethereum/SendFormActions.js b/src/actions/ethereum/SendFormActions.js index c2fde3bd..52894fd0 100644 --- a/src/actions/ethereum/SendFormActions.js +++ b/src/actions/ethereum/SendFormActions.js @@ -53,13 +53,6 @@ export const observe = (prevState: ReducersState, action: Action): ThunkAction = 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 (currentState.sendFormEthereum.currency === '') { dispatch(init()); @@ -116,8 +109,6 @@ export const init = (): AsyncAction => async (dispatch: Dispatch, getState: GetS network, } = getState().selectedAccount; - const { advanced } = getState().sendFormEthereum; - if (!account || !network) return; const stateFromStorage = dispatch(SessionStorageActions.loadEthereumDraftTransaction()); @@ -149,7 +140,6 @@ export const init = (): AsyncAction => async (dispatch: Dispatch, getState: GetS recommendedGasPrice: gasPrice.toString(), gasLimit, gasPrice: gasPrice.toString(), - advanced, }, }); }; @@ -165,10 +155,37 @@ export const toggleAdvanced = (): Action => ({ /* * Called from UI from "clear" button */ -export const onClear = (): Action => ({ - type: SEND.CLEAR, - networkType: 'ethereum', -}); +export const onClear = (): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise => { + const { network } = getState().selectedAccount; + 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 diff --git a/src/actions/ripple/SendFormActions.js b/src/actions/ripple/SendFormActions.js index f763882e..90e78eec 100644 --- a/src/actions/ripple/SendFormActions.js +++ b/src/actions/ripple/SendFormActions.js @@ -35,13 +35,6 @@ export const observe = (prevState: ReducersState, action: Action): ThunkAction = 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 (currentState.sendFormRipple.networkSymbol === '') { dispatch(init()); @@ -87,8 +80,6 @@ export const init = (): AsyncAction => async (dispatch: Dispatch, getState: GetS network, } = getState().selectedAccount; - const { advanced } = getState().sendFormEthereum; - if (!account || account.networkType !== 'ripple' || !network) return; const stateFromStorage = dispatch(SessionStorageActions.loadRippleDraftTransaction()); @@ -116,7 +107,6 @@ export const init = (): AsyncAction => async (dispatch: Dispatch, getState: GetS selectedFeeLevel, fee: network.fee.defaultFee, sequence: '1', - advanced, }, }); }; @@ -132,11 +122,34 @@ export const toggleAdvanced = (): Action => ({ /* * Called from UI from "clear" button */ -export const onClear = (): Action => ({ - type: SEND.CLEAR, - networkType: 'ripple', -}); +export const onClear = (): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise => { + const { network } = getState().selectedAccount; + 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 diff --git a/src/reducers/SendFormEthereumReducer.js b/src/reducers/SendFormEthereumReducer.js index 5053f673..58b8e11d 100644 --- a/src/reducers/SendFormEthereumReducer.js +++ b/src/reducers/SendFormEthereumReducer.js @@ -82,6 +82,7 @@ export default (state: State = initialState, action: Action): State => { case SEND.INIT: case SEND.CHANGE: case SEND.VALIDATION: + case SEND.CLEAR: return action.state; case SEND.TOGGLE_ADVANCED: diff --git a/src/reducers/SendFormRippleReducer.js b/src/reducers/SendFormRippleReducer.js index f56083bc..63a1a717 100644 --- a/src/reducers/SendFormRippleReducer.js +++ b/src/reducers/SendFormRippleReducer.js @@ -75,6 +75,7 @@ export default (state: State = initialState, action: Action): State => { case SEND.INIT: case SEND.CHANGE: case SEND.VALIDATION: + case SEND.CLEAR: return action.state; case SEND.TOGGLE_ADVANCED: