-
Notifications
You must be signed in to change notification settings - Fork 119
/
hooks.js
117 lines (103 loc) · 3.4 KB
/
hooks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { useState, useCallback } from "react";
import * as sel from "selectors";
import * as ca from "actions/ControlActions";
import { baseOutput } from "./helpers";
import { useSelector, useDispatch, shallowEqual } from "react-redux";
import { usePrevious } from "hooks";
import { compose, get, eq } from "lodash/fp";
export function useSendTransaction() {
const defaultSpendingAccount = useSelector(
sel.defaultSpendingAccount,
shallowEqual
);
const unsignedTransaction = useSelector(sel.unsignedTransaction);
const unsignedRawTx = useSelector(sel.unsignedRawTx);
const estimatedFee = useSelector(sel.estimatedFee);
const estimatedSignedSize = useSelector(sel.estimatedSignedSize);
const totalSpent = useSelector(sel.totalSpent);
const nextAddress = useSelector(sel.nextAddress);
const getNextAddressResponse = useSelector(sel.getNextAddressResponse);
const visibleAccounts = useSelector(sel.visibleAccounts);
const nextAddressAccountNumber = getNextAddressResponse
? getNextAddressResponse.accountNumber
: null;
const nextAddressAccount = visibleAccounts.find(
compose(eq(nextAddressAccountNumber), get("value"))
);
const constructTxLowBalance = useSelector(sel.constructTxLowBalance);
const publishTxResponse = useSelector(sel.publishTxResponse);
const notMixedAccounts = useSelector(
sel.getNotMixedAcctIfAllowed,
shallowEqual
);
const isTrezor = useSelector(sel.isTrezor);
const isWatchingOnly = useSelector(sel.isWatchingOnly);
const isConstructingTransaction = useSelector(sel.isConstructingTransaction);
const constructTxRequestAttempt = useSelector(sel.constructTxRequestAttempt);
const dispatch = useDispatch();
const attemptConstructTransaction = useCallback(
(account, confirmations, outputs, all) =>
dispatch(
ca.constructTransactionAttempt(account, confirmations, outputs, all)
),
[dispatch]
);
const validateAddress = (address) => dispatch(ca.validateAddress(address));
const onClearTransaction = () => dispatch(ca.clearTransaction());
const onGetNextAddressAttempt = useCallback(
(account) => dispatch(ca.getNextAddressAttempt(account)),
[dispatch]
);
const getRunningIndicator = useSelector(sel.getRunningIndicator);
return {
defaultSpendingAccount,
unsignedTransaction,
unsignedRawTx,
nextAddress,
nextAddressAccount,
estimatedFee,
estimatedSignedSize,
constructTxLowBalance,
publishTxResponse,
totalSpent,
notMixedAccounts,
isTrezor,
isWatchingOnly,
isConstructingTransaction,
attemptConstructTransaction,
validateAddress,
onClearTransaction,
onGetNextAddressAttempt,
getRunningIndicator,
constructTxRequestAttempt
};
}
export function useOutputs() {
const [outputs, setOutputs] = useState([baseOutput()]);
const prevOutputs = usePrevious(outputs);
const onAddOutput = () => {
const newOutputs = [...outputs];
newOutputs.push({
key: "output_" + outputs.length,
data: baseOutput().data
});
setOutputs(newOutputs);
};
const onUpdateOutput = (o) =>
setOutputs(
outputs.map((ol) => (ol.key === o.key ? { ...ol, data: o.data } : ol))
);
const onRemoveOutput = (index) => {
const outs = [...outputs];
outs.splice(index, 1);
setOutputs(outs);
};
return {
outputs,
onAddOutput,
onUpdateOutput,
onRemoveOutput,
onSetOutputs: setOutputs,
prevOutputs
};
}