From c3e66a286c1b4870f5831dde3caacd0dc33a14c6 Mon Sep 17 00:00:00 2001 From: George Lima Date: Fri, 21 Dec 2018 12:44:11 -0300 Subject: [PATCH] feature: add send redux module --- app/containers/send.js | 49 +++++++++++++++++++++++++++++++++--- app/redux/modules/reducer.js | 2 ++ app/redux/modules/send.js | 46 +++++++++++++++++++++++++++++++++ app/types/app-state.js | 2 ++ 4 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 app/redux/modules/send.js diff --git a/app/containers/send.js b/app/containers/send.js index f8b1fe1..c80c27e 100644 --- a/app/containers/send.js +++ b/app/containers/send.js @@ -1,14 +1,57 @@ // @flow import { connect } from 'react-redux'; +import eres from 'eres'; +import rpc from '../../services/api'; import { SendView } from '../views/send'; -import type { AppState } from '../types/app-state'; +import { + sendTransaction, + sendTransactionSuccess, + sendTransactionError, +} from '../redux/modules/send'; -const mapStateToProps = ({ walletSummary }: AppState) => ({ +import type { AppState } from '../types/app-state'; +import type { Dispatch } from '../types/redux'; + +export type SendTransactionInput = { + from: string, + to: string, + amount: number, + fee: number, + memo: string, +}; + +const mapStateToProps = ({ walletSummary, sendStatus }: AppState) => ({ balance: walletSummary.total, zecPrice: walletSummary.zecPrice, addresses: walletSummary.addresses, + error: sendStatus.error, + isSending: sendStatus.isSending, }); -export const SendContainer = connect(mapStateToProps)(SendView); +const mapDispatchToProps = (dispatch: Dispatch) => ({ + sendTransaction: async ({ + from, + to, + amount, + fee, + memo, + }: SendTransactionInput) => { + dispatch(sendTransaction()); + + const [sendErr] = await eres( + rpc.z_sendmany(from, [{ address: to, amount, memo }], 1, fee), + ); + + // eslint-disable-next-line + if (sendErr) return dispatch(sendTransactionError({ error: sendErr.message })); + + dispatch(sendTransactionSuccess()); + }, +}); + +export const SendContainer = connect( + mapStateToProps, + mapDispatchToProps, +)(SendView); diff --git a/app/redux/modules/reducer.js b/app/redux/modules/reducer.js index 8c9e2ad..6d2fb75 100644 --- a/app/redux/modules/reducer.js +++ b/app/redux/modules/reducer.js @@ -6,9 +6,11 @@ import type { RouterHistory } from 'react-router-dom'; import wallet from './wallet'; import transactions from './transactions'; +import send from './send'; export const createRootReducer = (history: RouterHistory) => combineReducers({ walletSummary: wallet, transactions, + sendStatus: send, router: connectRouter(history), }); diff --git a/app/redux/modules/send.js b/app/redux/modules/send.js new file mode 100644 index 0000000..c21daae --- /dev/null +++ b/app/redux/modules/send.js @@ -0,0 +1,46 @@ +// @flow +import type { Action } from '../../types/redux'; + +export const SEND_TRANSACTION = 'SEND_TRANSACTION'; +export const SEND_TRANSACTION_SUCCESS = 'SEND_TRANSACTION_SUCCESS'; +export const SEND_TRANSACTION_ERROR = 'SEND_TRANSACTION_ERROR'; + +export const sendTransaction = () => ({ + type: SEND_TRANSACTION, + payload: {}, +}); + +export const sendTransactionSuccess = () => ({ + type: SEND_TRANSACTION_SUCCESS, + payload: {}, +}); + +export const sendTransactionError = ({ error }: { error: string }) => ({ + type: SEND_TRANSACTION_ERROR, + payload: { + error, + }, +}); + +export type State = { + isSending: boolean, + error: string | null, +}; + +const initialState = { + isSending: false, + error: null, +}; + +export default (state: State = initialState, action: Action) => { + switch (action.type) { + case SEND_TRANSACTION: + return { isSending: true, error: null }; + case SEND_TRANSACTION_SUCCESS: + return { isSending: false, error: null }; + case SEND_TRANSACTION_ERROR: + return { isSending: false, error: action.payload.error }; + default: + return state; + } +}; diff --git a/app/types/app-state.js b/app/types/app-state.js index 281fa60..0954e03 100644 --- a/app/types/app-state.js +++ b/app/types/app-state.js @@ -2,8 +2,10 @@ import type { State as WalletSummaryState } from '../redux/modules/wallet'; import type { State as TransactionsState } from '../redux/modules/transactions'; +import type { State as SendState } from '../redux/modules/send'; export type AppState = { walletSummary: WalletSummaryState, transactions: TransactionsState, + sendStatus: SendState, };