feature: add send redux module
This commit is contained in:
parent
2c71bcbbdc
commit
c3e66a286c
|
@ -1,14 +1,57 @@
|
||||||
// @flow
|
// @flow
|
||||||
import { connect } from 'react-redux';
|
import { connect } from 'react-redux';
|
||||||
|
import eres from 'eres';
|
||||||
|
|
||||||
|
import rpc from '../../services/api';
|
||||||
import { SendView } from '../views/send';
|
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,
|
balance: walletSummary.total,
|
||||||
zecPrice: walletSummary.zecPrice,
|
zecPrice: walletSummary.zecPrice,
|
||||||
addresses: walletSummary.addresses,
|
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);
|
||||||
|
|
|
@ -6,9 +6,11 @@ import type { RouterHistory } from 'react-router-dom';
|
||||||
|
|
||||||
import wallet from './wallet';
|
import wallet from './wallet';
|
||||||
import transactions from './transactions';
|
import transactions from './transactions';
|
||||||
|
import send from './send';
|
||||||
|
|
||||||
export const createRootReducer = (history: RouterHistory) => combineReducers({
|
export const createRootReducer = (history: RouterHistory) => combineReducers({
|
||||||
walletSummary: wallet,
|
walletSummary: wallet,
|
||||||
transactions,
|
transactions,
|
||||||
|
sendStatus: send,
|
||||||
router: connectRouter(history),
|
router: connectRouter(history),
|
||||||
});
|
});
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
};
|
|
@ -2,8 +2,10 @@
|
||||||
|
|
||||||
import type { State as WalletSummaryState } from '../redux/modules/wallet';
|
import type { State as WalletSummaryState } from '../redux/modules/wallet';
|
||||||
import type { State as TransactionsState } from '../redux/modules/transactions';
|
import type { State as TransactionsState } from '../redux/modules/transactions';
|
||||||
|
import type { State as SendState } from '../redux/modules/send';
|
||||||
|
|
||||||
export type AppState = {
|
export type AppState = {
|
||||||
walletSummary: WalletSummaryState,
|
walletSummary: WalletSummaryState,
|
||||||
transactions: TransactionsState,
|
transactions: TransactionsState,
|
||||||
|
sendStatus: SendState,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue