zepio/app/containers/dashboard.js

98 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-12-10 09:10:06 -08:00
// @flow
import { connect } from 'react-redux';
import eres from 'eres';
2018-12-15 07:08:18 -08:00
import flow from 'lodash.flow';
import groupBy from 'lodash.groupby';
import dateFns from 'date-fns';
2019-01-29 07:02:34 -08:00
import { BigNumber } from 'bignumber.js';
2018-12-10 09:10:06 -08:00
import { DashboardView } from '../views/dashboard';
import rpc from '../../services/api';
import { listShieldedTransactions } from '../../services/shielded-transactions';
import store from '../../config/electron-store';
2018-12-15 07:08:18 -08:00
import {
loadWalletSummary,
loadWalletSummarySuccess,
loadWalletSummaryError,
} from '../redux/modules/wallet';
2019-02-11 11:36:29 -08:00
import { sortByDescend } from '../utils/sort-by-descend';
2018-12-10 09:10:06 -08:00
import type { AppState } from '../types/app-state';
import type { Dispatch } from '../types/redux';
const mapStateToProps = ({ walletSummary }: AppState) => ({
total: walletSummary.total,
shielded: walletSummary.shielded,
transparent: walletSummary.transparent,
error: walletSummary.error,
isLoading: walletSummary.isLoading,
zecPrice: walletSummary.zecPrice,
2018-12-10 16:11:53 -08:00
addresses: walletSummary.addresses,
transactions: walletSummary.transactions,
2018-12-10 09:10:06 -08:00
});
const mapDispatchToProps = (dispatch: Dispatch) => ({
getSummary: async () => {
dispatch(loadWalletSummary());
2019-01-29 07:02:34 -08:00
const [walletErr, walletSummary] = await eres(rpc.z_gettotalbalance());
2019-01-28 16:34:07 -08:00
const [zAddressesErr, zAddresses = []] = await eres(rpc.z_listaddresses());
const [tAddressesErr, tAddresses = []] = await eres(rpc.getaddressesbyaccount(''));
const [transactionsErr, transactions] = await eres(rpc.listtransactions());
2019-01-29 07:02:34 -08:00
if (walletErr || zAddressesErr || tAddressesErr || transactionsErr) {
2019-01-28 16:34:07 -08:00
return dispatch(
loadWalletSummaryError({
2019-01-29 07:02:34 -08:00
error: 'Something went wrong!',
2019-01-28 16:34:07 -08:00
}),
);
2018-12-15 07:08:18 -08:00
}
const formattedTransactions: Array<Object> = flow([
arr => arr.map(transaction => ({
transactionId: transaction.txid,
type: transaction.category,
date: new Date(transaction.time * 1000).toISOString(),
address: transaction.address,
amount: Math.abs(transaction.amount),
})),
arr => groupBy(arr, obj => dateFns.format(obj.date, 'MMM DD, YYYY')),
obj => Object.keys(obj).map(day => ({
day,
jsDay: new Date(day),
list: sortByDescend('date')(obj[day]),
})),
sortByDescend('jsDay'),
])([...transactions, ...listShieldedTransactions()]);
if (!zAddresses.length) {
2019-01-29 07:02:34 -08:00
const [, newZAddress] = await eres(rpc.z_getnewaddress());
2019-01-29 07:02:34 -08:00
if (newZAddress) zAddresses.push(newZAddress);
}
2019-01-28 16:34:07 -08:00
if (!tAddresses.length) {
2019-01-29 07:02:34 -08:00
const [, newTAddress] = await eres(rpc.getnewaddress(''));
2019-01-29 07:02:34 -08:00
if (newTAddress) tAddresses.push(newTAddress);
}
2018-12-10 09:10:06 -08:00
dispatch(
loadWalletSummarySuccess({
transparent: walletSummary.transparent,
total: walletSummary.total,
shielded: walletSummary.private,
2019-01-28 16:34:07 -08:00
addresses: [...zAddresses, ...tAddresses],
transactions: formattedTransactions,
2019-01-29 07:02:34 -08:00
zecPrice: new BigNumber(store.get('ZEC_DOLLAR_PRICE')).toNumber(),
2018-12-10 09:10:06 -08:00
}),
);
},
});
// $FlowFixMe
2018-12-10 09:10:06 -08:00
export const DashboardContainer = connect(
mapStateToProps,
mapDispatchToProps,
)(DashboardView);