@@ -1,4 +1,4 @@
1−import React, { useEffect } from 'react';
1+import React, { useMemo, useCallback } from 'react';
22import cn from 'classnames';
33import {
44 Grid,
@@ -12,12 +12,14 @@ import {
1212} from 'indigo-react';
1313import { fromWei } from 'web3-utils';
1414
15−import { useCheckboxInput } from 'lib/useInputs';
1615import { useExploreTxUrl } from 'lib/explorer';
1716import { hexify } from 'lib/txn';
1817
1918import { GenerateButton, ForwardButton, RestartButton } from './Buttons';
2019import WarningBox from './WarningBox';
20+import { composeValidator, buildCheckboxValidator } from 'form/Inputs';
21+import BridgeForm from 'form/BridgeForm';
22+import Condition from 'form/Condition';
2123
2224export default function InlineEthereumTransaction({
2325 // from useEthereumTransaction.bind
@@ -57,29 +59,24 @@ export default function InlineEthereumTransaction({
5759 const showSignedTx = signed;
5860
5961 const exploreTxUrl = useExploreTxUrl(txHash);
60− const [advancedInput, { data: advancedOpen }] = useCheckboxInput({
61− name: 'advanced',
62− label: 'Advanced Configuration',
63− inverseLabel: 'Cancel Advanced Configuration',
64− disabled: !showConfigureInput,
65− });
6662
67− const [
68− viewSignedTransaction,
69− { data: signedTransactionOpen },
70− ] = useCheckboxInput({
71− name: 'viewsigned',
72− label: 'View Signed Transaction',
73− inverseLabel: 'Hide Signed Transaction',
74− disabled: !showSignedTx,
75− });
63+ const validate = useMemo(
64+ () =>
65+ composeValidator({
66+ useAdvanced: buildCheckboxValidator(),
67+ viewSigned: buildCheckboxValidator(),
68+ }),
69+ []
70+ );
7671
77− // reset gas price when closing advanced configuration
78− useEffect(() => {
79− if (!advancedOpen) {
80− resetGasPrice();
81− }
82− }, [advancedOpen, resetGasPrice]);
72+ const onValues = useCallback(
73+ ({ valid, values, form }) => {
74+ if (!values.useAdvanced) {
75+ resetGasPrice();
76+ }
77+ },
78+ [resetGasPrice]
79+ );
8380
8481 const renderPrimaryButton = () => {
8582 if (error) {
@@ -126,94 +123,118 @@ export default function InlineEthereumTransaction({
126123
127124 return (
128125 <Grid className={cn(className, 'mt1')}>
129− {renderPrimaryButton()}
126+ <BridgeForm validate={validate} onValues={onValues} onSubmit={() => {}}>
127+ {({ handleSubmit }) => (
128+ <>
129+ {renderPrimaryButton()}
130130
131− {error && (
132− <Grid.Item full as={ErrorText} className="mt1">
133− {error.message}
134− </Grid.Item>
135− )}
136−
137− {needFunds && (
138− <Grid.Item full as={WarningBox} className="mt3">
139− The address {needFunds.address} needs at least{' '}
140− {fromWei(needFunds.minBalance)} ETH and currently has{' '}
141− {fromWei(needFunds.balance)} ETH. Waiting until the account has enough
142− funds.
143− </Grid.Item>
144− )}
145−
146− {showConfigureInput && (
147− <>
148− <Grid.Item full as={ToggleInput} {...advancedInput} />
149− {advancedOpen && (
150− <>
151− <Grid.Item full as={Flex} row justify="between" className="mt2">
152− <Flex.Item as={H5}>Gas Price</Flex.Item>
153− <Flex.Item as={H5}>{gasPrice} Gwei</Flex.Item>
154− </Grid.Item>
155− {/* TODO(shrugs): move to indigo/RangeInput */}
156− <Grid.Item
157− full
158− as="input"
159− type="range"
160− min="1"
161− max="100"
162− value={gasPrice}
163− onChange={e => setGasPrice(parseInt(e.target.value, 10))}
164− />
165− <Grid.Item
166− full
167− as={Flex}
168− row
169− justify="between"
170− className="f6 mt1">
171− <Flex.Item as={Text}>Cheap</Flex.Item>
172− <Flex.Item as={Text}>Fast</Flex.Item>
131+ {error && (
132+ <Grid.Item full as={ErrorText} className="mt1">
133+ {error.message}
173134 </Grid.Item>
174− <Grid.Item full as={HelpText} className="mt3">
175− Nonce: {nonce}
176− </Grid.Item>
177− <Grid.Item full as={HelpText} className="mt1">
178− Chain ID: {chainId}
135+ )}
136+
137+ {needFunds && (
138+ <Grid.Item full as={WarningBox} className="mt3">
139+ The address {needFunds.address} needs at least{' '}
140+ {fromWei(needFunds.minBalance)} ETH and currently has{' '}
141+ {fromWei(needFunds.balance)} ETH. Waiting until the account has
142+ enough funds.
179143 </Grid.Item>
180− <Grid.Divider className="mt3" />
181− </>
182− )}
183− </>
184− )}
144+ )}
185145
186− {showSignedTx && (
187− <>
188− <Grid.Item full as={ToggleInput} {...viewSignedTransaction} />
189− {signedTransactionOpen && (
190− <Grid.Item full as="code" className="f6 mono gray4 wrap">
191− {hexify(signedTransaction.serialize())}
192− </Grid.Item>
193− )}
194− </>
195− )}
146+ {showConfigureInput && (
147+ <>
148+ <Grid.Item
149+ full
150+ as={ToggleInput}
151+ name="useAdvanced"
152+ label="Advanced Configuration"
153+ inverseLabel="Cancel Advanced Configuration"
154+ disabled={!showConfigureInput}
155+ />
196156
197− {showReceipt && (
198− <>
199− <Grid.Divider />
200− <Grid.Item full as={Flex} col className="pv4">
201− <Flex.Item as={Flex} row justify="between">
202− <Flex.Item as={H5}>Transaction Hash</Flex.Item>
203− <Flex.Item as={LinkButton} href={exploreTxUrl}>
204− Etherscan↗
205− </Flex.Item>
206− </Flex.Item>
207− <Flex.Item as={Flex}>
208− <Flex.Item flex as="code" className="f6 mono gray4 wrap">
209− {txHash}
210− </Flex.Item>
211− <Flex.Item flex />
212− </Flex.Item>
213− </Grid.Item>
214− <Grid.Divider />
215− </>
216− )}
157+ <Condition when="useAdvanced" is={true}>
158+ <Grid.Item
159+ full
160+ as={Flex}
161+ row
162+ justify="between"
163+ className="mt2">
164+ <Flex.Item as={H5}>Gas Price</Flex.Item>
165+ <Flex.Item as={H5}>{gasPrice} Gwei</Flex.Item>
166+ </Grid.Item>
167+ {/* TODO(shrugs): move to indigo/RangeInput */}
168+ <Grid.Item
169+ full
170+ as="input"
171+ type="range"
172+ min="1"
173+ max="100"
174+ value={gasPrice}
175+ onChange={e => setGasPrice(parseInt(e.target.value, 10))}
176+ />
177+ <Grid.Item
178+ full
179+ as={Flex}
180+ row
181+ justify="between"
182+ className="f6 mt1">
183+ <Flex.Item as={Text}>Cheap</Flex.Item>
184+ <Flex.Item as={Text}>Fast</Flex.Item>
185+ </Grid.Item>
186+ <Grid.Item full as={HelpText} className="mt3">
187+ Nonce: {nonce}
188+ </Grid.Item>
189+ <Grid.Item full as={HelpText} className="mt1">
190+ Chain ID: {chainId}
191+ </Grid.Item>
192+ <Grid.Divider className="mt3" />
193+ </Condition>
194+ </>
195+ )}
196+
197+ {showSignedTx && (
198+ <>
199+ <Grid.Item
200+ full
201+ as={ToggleInput}
202+ name="viewSigned"
203+ label="View Signed Transaction"
204+ inverseLabel="Hide Signed Transaction"
205+ disabled={!showSignedTx}
206+ />
207+ <Condition when="viewSigned" is={true}>
208+ <Grid.Item full as="code" className="f6 mono gray4 wrap">
209+ {hexify(signedTransaction.serialize())}
210+ </Grid.Item>
211+ </Condition>
212+ </>
213+ )}
214+
215+ {showReceipt && (
216+ <>
217+ <Grid.Divider />
218+ <Grid.Item full as={Flex} col className="pv4">
219+ <Flex.Item as={Flex} row justify="between">
220+ <Flex.Item as={H5}>Transaction Hash</Flex.Item>
221+ <Flex.Item as={LinkButton} href={exploreTxUrl}>
222+ Etherscan↗
223+ </Flex.Item>
224+ </Flex.Item>
225+ <Flex.Item as={Flex}>
226+ <Flex.Item flex as="code" className="f6 mono gray4 wrap">
227+ {txHash}
228+ </Flex.Item>
229+ <Flex.Item flex />
230+ </Flex.Item>
231+ </Grid.Item>
232+ <Grid.Divider />
233+ </>
234+ )}
235+ </>
236+ )}
237+ </BridgeForm>
217238 </Grid>
218239 );
219240}