Skip to content

Commit

Permalink
[send] Fix Send all funds issue (#2822)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgptr authored Oct 29, 2020
1 parent a4dec62 commit 106226b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const getSendAllFundsIcon = ({
<a className={classNames(
styles.sendIconWrapper,
styles.cancelIcon
)} onClick={onHideSendAll} s/>
)} onClick={onHideSendAll} />
</Tooltip>
);

Expand Down
43 changes: 31 additions & 12 deletions app/components/views/TransactionsPage/SendTab/SendTab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ const SendTab = () => {
onAddOutput,
onUpdateOutput,
onRemoveOutput,
onSetOutputs
onSetOutputs,
prevOutputs
} = useOutputs();

const { walletService } = useService();
Expand All @@ -76,6 +77,7 @@ const SendTab = () => {
const prevPublishTxResponse = usePrevious(publishTxResponse);
const prevNextAddress = usePrevious(nextAddress);
const prevIsSendSelf = usePrevious(isSendSelf);
const prevAccount = usePrevious(account);

const onValidateAddress = async ({ address, index }) => {
let error;
Expand All @@ -95,7 +97,6 @@ const SendTab = () => {
}
ref.data.error.address = error;
onUpdateOutput(ref);
onAttemptConstructTransaction();
} catch (err) {
return err;
}
Expand All @@ -120,39 +121,49 @@ const SendTab = () => {
ref.data.amount = atomValue;
ref.data.error.amount = error;
onUpdateOutput(ref);
onAttemptConstructTransaction();
};

const onChangeAccount = (account) => {
setAccount(account);
onAttemptConstructTransaction();

if (isSendAll) {
const newOutputs = [{
...outputs[0],
data: {
...outputs[0].data,
amount: account.spendable
}
}];
onSetOutputs(newOutputs);
}
};

const onShowSendSelf = () => {
const newOutputs = [{ ...outputs[0], data: baseOutput().data }];
setIsSendSelf(true);
onSetOutputs(newOutputs);
onAttemptConstructTransaction();
};

const onShowSendOthers = () => {
const newOutputs = [{ ...outputs[0], data: baseOutput().data }];
setIsSendSelf(false);
onSetOutputs(newOutputs);
onAttemptConstructTransaction();
};

const onShowSendAll = () => {
const newOutputs = [{
...outputs[0],
data: {
...outputs[0].data,
amount: account.spendable
amount: account.spendable,
error: {
...outputs[0].data.error,
amount: null
}
}
}];
setIsSendAll(true);
onSetOutputs(newOutputs);
onAttemptConstructTransaction();
};

const onKeyDown = (e) => {
Expand All @@ -171,7 +182,6 @@ const SendTab = () => {
}];
setIsSendAll(false);
onSetOutputs(newOutputs);
onAttemptConstructTransaction();
};

const hasError = useCallback(() => {
Expand Down Expand Up @@ -293,15 +303,17 @@ const SendTab = () => {
}
if (newOutputs) {
onSetOutputs(newOutputs);
onAttemptConstructTransaction();
}

if (constructTxLowBalance) {
setInsuficientFunds(true);
} else {
setInsuficientFunds(false);
}

return () => onClearTransaction();
if(prevOutputs != outputs || prevAccount != account) {
onAttemptConstructTransaction();
}
}, [
publishTxResponse,
prevPublishTxResponse,
Expand All @@ -312,12 +324,19 @@ const SendTab = () => {
constructTxLowBalance,
nextAddressAccount.value,
outputs,
prevOutputs,
onSetOutputs,
onAttemptConstructTransaction,
onClearTransaction,
onGetNextAddressAttempt
onGetNextAddressAttempt,
account,
prevAccount
]);

useEffect(() => {
return () => onClearTransaction;
});

return !walletService ? <ErrorScreen /> : (
<SendPage
{...{
Expand Down
5 changes: 4 additions & 1 deletion app/components/views/TransactionsPage/SendTab/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as sel from "selectors";
import * as ca from "actions/ControlActions";
import { baseOutput } from "./helpers";
import { useSelector, useDispatch } from "react-redux";
import { usePrevious } from "hooks";

export function useSendTab() {
const defaultSpendingAccount = useSelector(sel.defaultSpendingAccount);
Expand Down Expand Up @@ -58,6 +59,7 @@ export function useSendTab() {

export function useOutputs() {
const [outputs, setOutputs] = useState([baseOutput()]);
const prevOutputs = usePrevious(outputs);

const onAddOutput = () => {
const newOutputs = [ ...outputs ];
Expand All @@ -83,6 +85,7 @@ export function useOutputs() {
onAddOutput,
onUpdateOutput,
onRemoveOutput,
onSetOutputs: setOutputs
onSetOutputs: setOutputs,
prevOutputs
};
}

0 comments on commit 106226b

Please sign in to comment.