Refactor code for checking balance
This commit is contained in:
parent
875a404702
commit
a0cdb486c5
@ -1,27 +1,54 @@
|
|||||||
import { useEffect } from "react";
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
import { Modal } from "@mui/material";
|
import { Modal } from '@mui/material';
|
||||||
|
|
||||||
import {
|
import { VITE_WALLET_IFRAME_URL } from 'utils/constants';
|
||||||
VITE_WALLET_IFRAME_URL,
|
|
||||||
} from 'utils/constants';
|
|
||||||
import useCheckBalance from '../../../hooks/useCheckBalance';
|
import useCheckBalance from '../../../hooks/useCheckBalance';
|
||||||
|
|
||||||
const CheckBalanceIframe = ({ setIsSufficient }: { setIsSufficient: (isSufficient: boolean | undefined) => void }) => {
|
const CHECK_BALANCE_INTERVAL = 5000;
|
||||||
const { checkBalance, isBalanceSufficient } = useCheckBalance(
|
const IFRAME_ID = 'checkBalanceIframe';
|
||||||
'1',
|
|
||||||
'checkBalanceIframe'
|
const CheckBalanceIframe = ({
|
||||||
|
onBalanceChange,
|
||||||
|
isPollingEnabled,
|
||||||
|
amount,
|
||||||
|
}: {
|
||||||
|
onBalanceChange: (value: boolean | undefined) => void;
|
||||||
|
isPollingEnabled: boolean;
|
||||||
|
amount: string;
|
||||||
|
}) => {
|
||||||
|
const { isBalanceSufficient, checkBalance } = useCheckBalance(
|
||||||
|
amount,
|
||||||
|
IFRAME_ID,
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setIsSufficient(isBalanceSufficient);
|
checkBalance();
|
||||||
|
}, [amount, checkBalance]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isPollingEnabled || isBalanceSufficient) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
checkBalance();
|
||||||
|
}, CHECK_BALANCE_INTERVAL);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
clearInterval(interval);
|
||||||
|
};
|
||||||
|
}, [isBalanceSufficient, isPollingEnabled, checkBalance]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
onBalanceChange(isBalanceSufficient);
|
||||||
}, [isBalanceSufficient]);
|
}, [isBalanceSufficient]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Modal open={false} disableEscapeKeyDown keepMounted>
|
<Modal open={false} disableEscapeKeyDown keepMounted>
|
||||||
<iframe
|
<iframe
|
||||||
onLoad={checkBalance}
|
onLoad={checkBalance}
|
||||||
id="checkBalanceIframe"
|
id={IFRAME_ID}
|
||||||
src={VITE_WALLET_IFRAME_URL}
|
src={VITE_WALLET_IFRAME_URL}
|
||||||
width="100%"
|
width="100%"
|
||||||
height="100%"
|
height="100%"
|
||||||
|
@ -27,7 +27,7 @@ import {
|
|||||||
VITE_LACONICD_CHAIN_ID,
|
VITE_LACONICD_CHAIN_ID,
|
||||||
VITE_WALLET_IFRAME_URL,
|
VITE_WALLET_IFRAME_URL,
|
||||||
} from 'utils/constants';
|
} from 'utils/constants';
|
||||||
import useCheckBalance from '../../../hooks/useCheckBalance';
|
import CheckBalanceIframe from './CheckBalanceIframe';
|
||||||
|
|
||||||
type ConfigureDeploymentFormValues = {
|
type ConfigureDeploymentFormValues = {
|
||||||
option: string;
|
option: string;
|
||||||
@ -41,7 +41,6 @@ type ConfigureFormValues = ConfigureDeploymentFormValues &
|
|||||||
|
|
||||||
const DEFAULT_MAX_PRICE = '10000';
|
const DEFAULT_MAX_PRICE = '10000';
|
||||||
const TX_APPROVAL_TIMEOUT_MS = 60000;
|
const TX_APPROVAL_TIMEOUT_MS = 60000;
|
||||||
const CHECK_BALANCE_INTERVAL = 5000;
|
|
||||||
|
|
||||||
const Configure = () => {
|
const Configure = () => {
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
@ -53,6 +52,7 @@ const Configure = () => {
|
|||||||
const [isFrameVisible, setIsFrameVisible] = useState(false);
|
const [isFrameVisible, setIsFrameVisible] = useState(false);
|
||||||
const [isAccountsDataReceived, setIsAccountsDataReceived] = useState(false);
|
const [isAccountsDataReceived, setIsAccountsDataReceived] = useState(false);
|
||||||
const [balanceMessage, setBalanceMessage] = useState<string>();
|
const [balanceMessage, setBalanceMessage] = useState<string>();
|
||||||
|
const [isBalanceSufficient, setIsBalanceSufficient] = useState<boolean>();
|
||||||
|
|
||||||
const [searchParams] = useSearchParams();
|
const [searchParams] = useSearchParams();
|
||||||
const templateId = searchParams.get('templateId');
|
const templateId = searchParams.get('templateId');
|
||||||
@ -94,6 +94,7 @@ const Configure = () => {
|
|||||||
if (selectedOption === 'LRN') {
|
if (selectedOption === 'LRN') {
|
||||||
amount = selectedDeployer?.minimumPayment?.toString() ?? '0';
|
amount = selectedDeployer?.minimumPayment?.toString() ?? '0';
|
||||||
} else {
|
} else {
|
||||||
|
// TODO: Use BigNumber
|
||||||
amount = (selectedNumProviders * Number(selectedMaxPrice)).toString();
|
amount = (selectedNumProviders * Number(selectedMaxPrice)).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,11 +106,6 @@ const Configure = () => {
|
|||||||
selectedNumProviders,
|
selectedNumProviders,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const { isBalanceSufficient, checkBalance } = useCheckBalance(
|
|
||||||
amountToBePaid,
|
|
||||||
'walletIframe',
|
|
||||||
);
|
|
||||||
|
|
||||||
const createProject = async (
|
const createProject = async (
|
||||||
data: FieldValues,
|
data: FieldValues,
|
||||||
envVariables: AddEnvironmentVariableInput[],
|
envVariables: AddEnvironmentVariableInput[],
|
||||||
@ -435,22 +431,6 @@ const Configure = () => {
|
|||||||
fetchDeployers();
|
fetchDeployers();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (isAccountsDataReceived) {
|
|
||||||
checkBalance();
|
|
||||||
|
|
||||||
if (!isBalanceSufficient) {
|
|
||||||
const interval = setInterval(() => {
|
|
||||||
checkBalance();
|
|
||||||
}, CHECK_BALANCE_INTERVAL);
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
clearInterval(interval);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, [isAccountsDataReceived, isBalanceSufficient, checkBalance]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-7 px-4 py-6">
|
<div className="space-y-7 px-4 py-6">
|
||||||
<div className="flex justify-between mb-6">
|
<div className="flex justify-between mb-6">
|
||||||
@ -680,6 +660,7 @@ const Configure = () => {
|
|||||||
setIsDataReceived={setIsAccountsDataReceived}
|
setIsDataReceived={setIsAccountsDataReceived}
|
||||||
isVisible={isFrameVisible}
|
isVisible={isFrameVisible}
|
||||||
/>
|
/>
|
||||||
|
<CheckBalanceIframe onBalanceChange={setIsBalanceSufficient} amount={amountToBePaid} isPollingEnabled={true}/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
@ -6,9 +6,10 @@ import { Button } from 'components/shared';
|
|||||||
import CheckBalanceIframe from 'components/projects/create/CheckBalanceIframe';
|
import CheckBalanceIframe from 'components/projects/create/CheckBalanceIframe';
|
||||||
|
|
||||||
const BuyPrepaidService = () => {
|
const BuyPrepaidService = () => {
|
||||||
const [isBalanceSufficient, setIsBalanceSufficient] = useState<boolean>();
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
const [isBalanceSufficient, setIsBalanceSufficient] = useState<boolean>();
|
||||||
|
|
||||||
const isTabletView = useMediaQuery('(min-width: 720px)'); // md:
|
const isTabletView = useMediaQuery('(min-width: 720px)'); // md:
|
||||||
const buttonSize = isTabletView ? { size: 'lg' as const } : {};
|
const buttonSize = isTabletView ? { size: 'lg' as const } : {};
|
||||||
|
|
||||||
@ -28,7 +29,7 @@ const BuyPrepaidService = () => {
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<CheckBalanceIframe setIsSufficient={setIsBalanceSufficient} />
|
<CheckBalanceIframe onBalanceChange={setIsBalanceSufficient} isPollingEnabled={false} amount='1'/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -10,11 +10,11 @@ import CheckBalanceIframe from 'components/projects/create/CheckBalanceIframe';
|
|||||||
|
|
||||||
const Projects = () => {
|
const Projects = () => {
|
||||||
const [isBalanceSufficient, setIsBalanceSufficient] = useState<boolean>();
|
const [isBalanceSufficient, setIsBalanceSufficient] = useState<boolean>();
|
||||||
|
const [projects, setProjects] = useState<Project[]>([]);
|
||||||
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const client = useGQLClient();
|
const client = useGQLClient();
|
||||||
const { orgSlug } = useParams();
|
const { orgSlug } = useParams();
|
||||||
const [projects, setProjects] = useState<Project[]>([]);
|
|
||||||
|
|
||||||
const fetchProjects = useCallback(async () => {
|
const fetchProjects = useCallback(async () => {
|
||||||
const { projectsInOrganization } = await client.getProjectsInOrganization(
|
const { projectsInOrganization } = await client.getProjectsInOrganization(
|
||||||
@ -60,7 +60,7 @@ const Projects = () => {
|
|||||||
})}
|
})}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<CheckBalanceIframe setIsSufficient={setIsBalanceSufficient} />
|
<CheckBalanceIframe onBalanceChange={setIsBalanceSufficient} isPollingEnabled={false} amount='1' />
|
||||||
</section>
|
</section>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user