-
Notifications
You must be signed in to change notification settings - Fork 119
/
ReceiveAccountsSelect.jsx
67 lines (60 loc) · 1.95 KB
/
ReceiveAccountsSelect.jsx
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
import { useCallback, useEffect } from "react";
import AccountsSelect from "./AccountsSelect";
import { useSelector, useDispatch } from "react-redux";
import * as ca from "actions/ControlActions";
import * as sel from "selectors";
import { compose, get, eq } from "lodash/fp";
function ReceiveAccountsSelect({
onChange,
showAccountsButton,
account,
...props
}) {
const dispatch = useDispatch();
const mixedAccount = useSelector(sel.getMixedAccount);
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 getNextAddressAttempt = useCallback(
(value) => dispatch(ca.getNextAddressAttempt(value)),
[dispatch]
);
const onChangeAccount = useCallback(
(account) => {
onChange && onChange(account);
getNextAddressAttempt(account.value);
},
[getNextAddressAttempt, onChange]
);
useEffect(() => {
if (account && account != nextAddressAccount.value) {
getNextAddressAttempt(account);
}
}, [account, getNextAddressAttempt, nextAddressAccount.value]);
return (
<AccountsSelect
{...{
showAccountsButton,
onChange: onChangeAccount,
accountsType: "visible",
filterAccounts: [mixedAccount],
// If account property is defined, the getNextAddressAttampt action
// needs to be called with it, which updates nextAddressAccount
// eventually. Until it happens, it's better to show no account
// than a previously chosen one.
account:
account && account != nextAddressAccount.value
? null
: nextAddressAccount,
isSearchable: true,
...props
}}
/>
);
}
export default ReceiveAccountsSelect;