Skip to content

Commit

Permalink
used a new hook and removed the modal from every place it was called
Browse files Browse the repository at this point in the history
  • Loading branch information
Ran Cohen committed Oct 3, 2022
1 parent d766240 commit aea2cfa
Show file tree
Hide file tree
Showing 19 changed files with 232 additions and 228 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useDispatch } from 'react-redux';
import { useApproveModal } from 'hooks/useApproveModal';
import { Token } from 'services/observables/tokens';
import { addLiquidity } from 'services/web3/liquidity/liquidity';
import {
Expand All @@ -14,6 +13,7 @@ import { useAppSelector } from 'store';
import { Pool } from 'services/observables/pools';
import { useNavigation } from 'hooks/useNavigation';
import { useWalletConnect } from 'elements/walletConnect/useWalletConnect';
import { useApproval } from 'hooks/useApproval';

interface Props {
pool: Pool;
Expand Down Expand Up @@ -83,7 +83,7 @@ export const AddLiquidityEmptyCTA = ({
goToPage,
]);

const [onStart, ModalApprove] = useApproveModal(
const startApprove = useApproval(
[
{ amount: amountBnt, token: bnt },
{ amount: amountTkn, token: tkn },
Expand Down Expand Up @@ -121,7 +121,7 @@ export const AddLiquidityEmptyCTA = ({
if (!account) {
handleWalletButtonClick();
} else {
onStart();
startApprove();
}
};

Expand All @@ -136,7 +136,6 @@ export const AddLiquidityEmptyCTA = ({
>
{btn.label}
</Button>
{ModalApprove}
</>
);
};
16 changes: 11 additions & 5 deletions src/elements/earn/pools/addLiquidity/single/AddLiquiditySingle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { AddLiquiditySingleSpaceAvailable } from 'elements/earn/pools/addLiquidi
import { useAppSelector } from 'store';
import { AddLiquiditySingleAmount } from 'elements/earn/pools/addLiquidity/single/AddLiquiditySingleAmount';
import { useCallback, useState } from 'react';
import { useApproveModal } from 'hooks/useApproveModal';
import { AddLiquiditySingleCTA } from 'elements/earn/pools/addLiquidity/single/AddLiquiditySingleCTA';
import { useDispatch } from 'react-redux';
import { prettifyNumber } from 'utils/helperFunctions';
Expand All @@ -31,6 +30,7 @@ import { useNavigation } from 'hooks/useNavigation';
import { fetchProtectedPositions } from 'services/web3/protection/positions';
import { setProtectedPositions } from 'store/liquidity/liquidity';
import { Events } from 'services/api/googleTagManager';
import { useApproval } from 'hooks/useApproval';

interface Props {
pool: Pool;
Expand Down Expand Up @@ -113,7 +113,7 @@ export const AddLiquiditySingle = ({ pool }: Props) => {
);
};

const [onStart, ModalApprove] = useApproveModal(
const startApprove = useApproval(
[{ amount, token: selectedToken }],
addV2Protection,
ApprovalContract.LiquidityProtection,
Expand Down Expand Up @@ -165,8 +165,15 @@ export const AddLiquiditySingle = ({ pool }: Props) => {
fiatToggle
);
sendLiquidityEvent(Events.click);
onStart();
}, [amount, amountUsd, fiatToggle, onStart, pool.name, selectedToken.symbol]);
startApprove();
}, [
amount,
amountUsd,
fiatToggle,
startApprove,
pool.name,
selectedToken.symbol,
]);

if (!tkn) {
goToPage.notFound();
Expand Down Expand Up @@ -212,7 +219,6 @@ export const AddLiquiditySingle = ({ pool }: Props) => {
errorMsg={handleError()}
isBNTSelected={isBNTSelected}
/>
{ModalApprove}
</Widget>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { usePopper } from 'react-popper';
import { V3EarningsTableMenuContent } from 'elements/earn/portfolio/v3/earningsTable/menu/V3EarningTableMenuContent';
import { Placement } from '@popperjs/core';
import { Holding } from 'store/portfolio/v3Portfolio.types';
import { useApproveModal } from 'hooks/useApproveModal';
import { ContractsApi } from 'services/web3/v3/contractsApi';
import { expandToken } from 'utils/formulas';
import { updatePortfolioData } from 'services/web3/v3/portfolio/helpers';
Expand All @@ -16,6 +15,7 @@ import {
rejectNotification,
} from 'services/notifications/notifications';
import { ErrorCode } from 'services/web3/types';
import { useApproval } from 'hooks/useApproval';

export type EarningTableMenuState = 'main' | 'bonus' | 'rate';

Expand Down Expand Up @@ -74,7 +74,7 @@ export const V3EarningTableMenu = memo(
}
};

const [onStart, ApproveModal] = useApproveModal(
const startApprove = useApproval(
[
{
amount: holding.poolTokenBalance,
Expand All @@ -92,12 +92,11 @@ export const V3EarningTableMenu = memo(

const onStartJoin = useCallback(() => {
setTxJoinBusy(true);
onStart();
}, [onStart]);
startApprove();
}, [startApprove]);

return (
<>
{ApproveModal}
<Popover className="relative">
{({ open }) => (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {
} from 'services/notifications/notifications';
import { updatePortfolioData } from 'services/web3/v3/portfolio/helpers';
import { ErrorCode } from 'services/web3/types';
import { useApproveModal } from 'hooks/useApproveModal';
import { Button, ButtonSize, ButtonVariant } from 'components/button/Button';
import { PopoverV3 } from 'components/popover/PopoverV3';
import { useModal } from 'hooks/useModal';
import { ModalNames } from 'modals';
import { useApproval } from 'hooks/useApproval';

export const V3HoldingsItemUnstaked = ({ holding }: { holding: Holding }) => {
const { pool } = holding;
Expand Down Expand Up @@ -58,7 +58,7 @@ export const V3HoldingsItemUnstaked = ({ holding }: { holding: Holding }) => {
}
};

const [onStart, ApproveModal] = useApproveModal(
const startApprove = useApproval(
[
{
amount: holding.poolTokenBalance,
Expand All @@ -75,8 +75,8 @@ export const V3HoldingsItemUnstaked = ({ holding }: { holding: Holding }) => {

const onStartJoin = useCallback(() => {
setTxJoinBusy(true);
onStart();
}, [onStart]);
startApprove();
}, [startApprove]);

return (
<>
Expand Down Expand Up @@ -131,7 +131,6 @@ export const V3HoldingsItemUnstaked = ({ holding }: { holding: Holding }) => {
)}
</div>
</div>
{ApproveModal}
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const V3WithdrawStep1 = ({

const [isConfirmed, setIsConfirmed] = useState(false);

const { handleButtonClick, ModalApprove, txBusy } = useV3WithdrawStep3({
const { handleButtonClick, txBusy } = useV3WithdrawStep3({
holding,
amount,
setStep,
Expand Down Expand Up @@ -158,7 +158,6 @@ const V3WithdrawStep1 = ({

return (
<div className="w-full max-w-[460px]">
{ModalApprove}
<h1 className="text-[36px] font-normal mb-30 leading-10">
How much {token.symbol} do you want to withdraw?
</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ const V3WithdrawStep3 = ({
isFiat,
setRequestId,
}: Props) => {
const { token, handleButtonClick, ModalApprove, txBusy } = useV3WithdrawStep3(
{
holding,
amount,
setStep,
setRequestId,
}
);
const { token, handleButtonClick, txBusy } = useV3WithdrawStep3({
holding,
amount,
setStep,
setRequestId,
});

return (
<>
Expand Down Expand Up @@ -58,7 +56,6 @@ const V3WithdrawStep3 = ({
</Button>
</div>
</div>
{ModalApprove}
</>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useApproveModal } from 'hooks/useApproveModal';
import { useApproval } from 'hooks/useApproval';
import { ContractsApi } from 'services/web3/v3/contractsApi';
import { Holding } from 'store/portfolio/v3Portfolio.types';
import BigNumber from 'bignumber.js';
Expand Down Expand Up @@ -157,7 +157,7 @@ export const useV3WithdrawStep3 = ({
if (initiatedWithdraw.current) setStep(4);
}, [withdrawalRequests, setStep]);

const [onStart, ModalApprove] = useApproveModal(
const startApprove = useApproval(
approveTokens,
(approvalHash?: string) => initWithdraw(approvalHash),
ContractsApi.BancorNetwork.contractAddress,
Expand All @@ -181,13 +181,12 @@ export const useV3WithdrawStep3 = ({
return;
}
hasStarted.current = true;
onStart();
}, [onStart, poolTokenAmountWei]);
startApprove();
}, [startApprove, poolTokenAmountWei]);

return {
token: reserveToken,
handleButtonClick,
ModalApprove,
txBusy,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import BigNumber from 'bignumber.js';
import useAsyncEffect from 'use-async-effect';
import { fetchWithdrawalRequestOutputBreakdown } from 'services/web3/v3/portfolio/withdraw';
import { wait } from 'utils/pureFunctions';
import { useApproveModal } from 'hooks/useApproveModal';
import { useApproval } from 'hooks/useApproval';
import { ContractsApi } from 'services/web3/v3/contractsApi';
import { WithdrawalRequest } from 'store/portfolio/v3Portfolio.types';
import {
Expand Down Expand Up @@ -176,7 +176,7 @@ export const useV3WithdrawConfirm = ({
return tokensToApprove;
}, [govToken?.address, govToken?.symbol, poolTokenAmount, token]);

const [onStart, ModalApprove] = useApproveModal(
const startApprove = useApproval(
approveTokens,
(approvalHash?: string) => withdraw(approvalHash),
ContractsApi.BancorNetwork.contractAddress,
Expand Down Expand Up @@ -204,9 +204,9 @@ export const useV3WithdrawConfirm = ({
});
sendWithdrawACEvent(WithdrawACEvent.ApproveClick);
setTxBusy(true);
onStart();
startApprove();
}, [
onStart,
startApprove,
pool.name,
withdrawRequest.pool.reserveToken.usdPrice,
withdrawRequest.reserveTokenAmount,
Expand All @@ -220,7 +220,6 @@ export const useV3WithdrawConfirm = ({

return {
onModalClose,
ModalApprove,
token,
withdrawAmounts,
missingGovTokenBalance,
Expand Down
12 changes: 5 additions & 7 deletions src/elements/swapLimit/SwapLimit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from 'components/button/Button';
import useAsyncEffect from 'use-async-effect';
import { useWalletConnect } from 'elements/walletConnect/useWalletConnect';
import { useApproveModal } from 'hooks/useApproveModal';
import { useApproval } from 'hooks/useApproval';
import {
depositETHNotification,
rejectNotification,
Expand Down Expand Up @@ -288,7 +288,7 @@ export const SwapLimit = ({
try {
const txHash = await depositWeth(fromAmount);
depositETHNotification(dispatch, fromAmount, txHash);
approveWETH();
startWETHApprove();
} catch (e: any) {
if (e.code === ErrorCode.DeniedTx)
sendConversionEvent(
Expand Down Expand Up @@ -359,13 +359,13 @@ export const SwapLimit = ({
return 'Trade';
};

const [onStart, ModalApprove] = useApproveModal(
const startApprove = useApproval(
[{ amount: fromAmount, token: fromToken }],
handleSwap,
ApprovalContract.ExchangeProxy
);

const [approveWETH, ModalApproveWETH] = useApproveModal(
const startWETHApprove = useApproval(
[
{
amount: fromAmount,
Expand Down Expand Up @@ -543,8 +543,6 @@ export const SwapLimit = ({
)}
</div>

{ModalApprove}
{ModalApproveWETH}
<Button
size={ButtonSize.Full}
onClick={() => {
Expand All @@ -553,7 +551,7 @@ export const SwapLimit = ({
modalName: ModalNames.DepositETH,
data: { onConfirm: deposiEthWeth(), amount: fromAmount },
});
else onStart();
else startApprove();
}}
disabled={isSwapDisabled()}
className="disabled:bg-silver dark:disabled:bg-charcoal"
Expand Down
1 change: 0 additions & 1 deletion src/elements/trade/TradeWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export const TradeWidget = (props: Props) => {

<TradeWidgetCTA {...trade} {...tradeWidget} />
</div>
{trade.ApproveModal}
</div>
);
};
Loading

0 comments on commit aea2cfa

Please sign in to comment.