From 1e90f0ad494f549ead37941104a2871950768525 Mon Sep 17 00:00:00 2001 From: George Lima Date: Fri, 8 Feb 2019 01:19:45 -0300 Subject: [PATCH] feat(send): add load address balance in send container --- app/containers/send.js | 21 ++++++++++++++------- app/redux/modules/send.js | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/app/containers/send.js b/app/containers/send.js index 7f05ea3..10e2f86 100644 --- a/app/containers/send.js +++ b/app/containers/send.js @@ -16,17 +16,17 @@ import { resetSendTransaction, validateAddressSuccess, validateAddressError, + loadAddressBalanceSuccess, + loadAddressBalanceError, } from '../redux/modules/send'; import { filterObjectNullKeys } from '../utils/filter-object-null-keys'; +import { ascii2hex } from '../utils/ascii-to-hexadecimal'; import type { AppState } from '../types/app-state'; import type { Dispatch } from '../types/redux'; -import { - loadAddressesSuccess, - loadAddressesError, -} from '../redux/modules/receive'; +import { loadAddressesSuccess, loadAddressesError } from '../redux/modules/receive'; export type SendTransactionInput = { from: string, @@ -36,8 +36,8 @@ export type SendTransactionInput = { memo: string, }; -const mapStateToProps = ({ walletSummary, sendStatus, receive }: AppState) => ({ - balance: walletSummary.total, +const mapStateToProps = ({ sendStatus, receive }: AppState) => ({ + balance: sendStatus.addressBalance, zecPrice: sendStatus.zecPrice, addresses: receive.addresses, error: sendStatus.error, @@ -61,7 +61,7 @@ const mapDispatchToProps = (dispatch: Dispatch) => ({ filterObjectNullKeys({ address: to, amount: new BigNumber(amount).toNumber(), - memo, + memo: ascii2hex(memo), }), ], 1, @@ -144,6 +144,13 @@ const mapDispatchToProps = (dispatch: Dispatch) => ({ value: Number(store.get('ZEC_DOLLAR_PRICE')), }), ), + getAddressBalance: async ({ address }: { address: string }) => { + const [err, balance] = await eres(rpc.z_getbalance(address)); + + if (err) return dispatch(loadAddressBalanceError({ error: "Can't load your balance address" })); + + return dispatch(loadAddressBalanceSuccess({ balance })); + }, }); // $FlowFixMe diff --git a/app/redux/modules/send.js b/app/redux/modules/send.js index 1f6a000..4733db6 100644 --- a/app/redux/modules/send.js +++ b/app/redux/modules/send.js @@ -8,6 +8,8 @@ export const RESET_SEND_TRANSACTION = 'RESET_SEND_TRANSACTION'; export const VALIDATE_ADDRESS_SUCCESS = 'VALIDATE_ADDRESS_SUCCESS'; export const VALIDATE_ADDRESS_ERROR = 'VALIDATE_ADDRESS_SUCCESS'; export const LOAD_ZEC_PRICE = 'LOAD_ZEC_PRICE'; +export const LOAD_ADDRESS_BALANCE_SUCCESS = 'LOAD_ADDRESS_BALANCE_SUCCESS'; +export const LOAD_ADDRESS_BALANCE_ERROR = 'LOAD_ADDRESS_BALANCE_ERROR'; export const sendTransaction = () => ({ type: SEND_TRANSACTION, @@ -52,12 +54,27 @@ export const loadZECPrice = ({ value }: { value: number }) => ({ }, }); +export const loadAddressBalanceSuccess = ({ balance }: { balance: number }) => ({ + type: LOAD_ADDRESS_BALANCE_SUCCESS, + payload: { + balance, + }, +}); + +export const loadAddressBalanceError = ({ error }: { error: string }) => ({ + type: LOAD_ADDRESS_BALANCE_SUCCESS, + payload: { + error, + }, +}); + export type State = { isSending: boolean, isToAddressValid: boolean, error: string | null, operationId: string | null, zecPrice: number, + addressBalance: number, }; const initialState: State = { @@ -66,6 +83,7 @@ const initialState: State = { operationId: null, isToAddressValid: false, zecPrice: 0, + addressBalance: 0, }; // eslint-disable-next-line @@ -104,6 +122,10 @@ export default (state: State = initialState, action: Action): State => { }; case LOAD_ZEC_PRICE: return { ...state, zecPrice: action.payload.value }; + case LOAD_ADDRESS_BALANCE_SUCCESS: + return { ...state, addressBalance: action.payload.balance }; + case LOAD_ADDRESS_BALANCE_ERROR: + return { ...state, error: action.payload.error }; case RESET_SEND_TRANSACTION: return initialState; default: