import React, { useState, useCallback } from 'react'; import { Just } from 'folktale/maybe'; import { Grid, H4, Text, ErrorText } from 'indigo-react'; import { RestartButton } from 'components/Buttons'; import WarningBox from 'components/WarningBox'; import LoadingBar from 'components/LoadingBar'; import Highlighted from 'components/Highlighted'; import { claimPointFromInvite } from 'lib/invite'; import { fromWei } from 'lib/txn'; import * as need from 'lib/need'; import { useNetwork } from 'store/network'; import { useWallet } from 'store/wallet'; import { usePointCursor } from 'store/pointCursor'; import useLifecycle from 'lib/useLifecycle'; //TODO maybe we want to drop in PassportTransfer here? export default function DoReticket({ newWallet, completed }) { const { web3, contracts } = useNetwork(); const { wallet, setUrbitWallet } = useWallet(); const { pointCursor } = usePointCursor(); const [generalError, setGeneralError] = useState(); const [{ label, progress }, setState] = useState({ label: 'Starting...', progress: 0, }); const [needFunds, setNeedFunds] = useState(); // start reticketing transactions on mount useLifecycle(() => { (async () => { try { await claimPointFromInvite({ inviteWallet: need.wallet(wallet), wallet: newWallet.value.wallet, point: need.point(pointCursor), web3: need.web3(web3), contracts: need.contracts(contracts), onUpdate: handleUpdate, }); //TODO would it be better for this to happen in completed()? setUrbitWallet(Just(newWallet.value.wallet)); completed(); //TODO don't auto-redirect } catch (err) { setGeneralError(err); } })(); }); const handleUpdate = useCallback( ({ type, state, value }) => { switch (type) { case 'progress': return setState(state); case 'askFunding': return setNeedFunds(value); case 'gotFunding': return setNeedFunds(false); default: console.error(`Unknown update: ${type}`); } }, [setState, setNeedFunds] ); const renderAdditionalInfo = () => { if (generalError) { return ( <> {generalError.message.toString()} ); } if (needFunds) { return ( The address {needFunds.address} needs at least{' '} {fromWei(needFunds.minBalance)} ETH and currently has{' '} {fromWei(needFunds.balance)} ETH. Waiting until the account has enough funds. ); } if (progress < 100) { return ( Never give your Master Ticket to anyone ); } return null; }; return ( {label} This process can take up to 5 minutes to complete. Don't leave this page until the process is complete. {renderAdditionalInfo()} ); }