on master help sign in
fix: minor updates from PR self-review
commit228dce84bf48413d1bdf87db950a305fe20ef61c authorMatt Condon (shrugs) <mattgcondon@gmail.com> dateTue 6 Aug 2019, 11:59 (7 years ago) parente82c628
11 files changed+45−49
src/form/SubmitButton.js+1−1 src/form/ValuesHandler.js+4−1 src/form/validators.js+9−7 src/indigo-react/components/SelectInput.js+1−1 src/indigo-react/components/ToggleInput.js+1−1 src/views/Admin/AdminNetworkingKeys.js+3−5 src/views/Admin/AdminSetProxy.js−2 src/views/Invite/InviteEmail.js+12−16 src/views/Login/Ledger.js+1−1 src/views/Login/PrivateKey.js+3−3 src/views/Login/Trezor.js+10−11
src/form/SubmitButton.js+1−1
@@ -1,10 +1,10 @@
11import React from 'react';
22import cn from 'classnames';
3+import { useFormState } from 'react-final-form';
34
45import { ForwardButton } from 'components/Buttons';
56
67import { blinkIf } from 'components/Blinky';
7−import { useFormState } from 'react-final-form';
88
99export default function SubmitButton({
1010 as: As = ForwardButton,
src/form/ValuesHandler.js+4−1
@@ -1,6 +1,9 @@
11import { useEffect } from 'react';
22import { useFormState, useForm } from 'react-final-form';
33
4+/**
5+ * ValuesHandler notifies callback function when form values change.
6+ */
47export default function ValuesHandler({ onValues }) {
58 const form = useForm();
69 const { valid, validating, values } = useFormState({
@@ -13,7 +16,7 @@ export default function ValuesHandler({ onValues }) {
1316
1417 useEffect(() => {
1518 if (!validating) {
16− onValues && onValues({ valid: valid && !validating, values, form });
19+ onValues && onValues({ valid, values, form });
1720 }
1821 }, [form, onValues, valid, validating, values]);
1922
src/form/validators.js+9−7
@@ -29,6 +29,10 @@ const buildValidator = (validators = []) => async value => {
2929 }
3030};
3131
32+// maps a validator across an array of values
33+export const buildArrayValidator = validator => async values =>
34+ await Promise.all(values.map(validator));
35+
3236// error object has errors if some of its fields are
3337// 1) an array with any defined values
3438// 2) any defined values
@@ -72,17 +76,15 @@ export const buildAddressValidator = () =>
7276 ]);
7377export const buildNumberValidator = (min = 0) =>
7478 buildValidator([validateGreaterThan(min)]);
75−export const buildEmailValidator = () =>
76− buildValidator([validateNotEmpty, validateEmail]);
77−
78−// the default form validator just returns field-level validations
79−const kDefaultFormValidator = (values, errors) => errors;
79+export const buildEmailValidator = (validators = []) =>
80+ buildValidator([validateNotEmpty, validateEmail, ...validators]);
8081
8182// the form validator is the composition of all of the field validators
8283// plus an additional form validator function
8384export const composeValidator = (
8485 fieldValidators = {},
85− formValidator = kDefaultFormValidator
86+ // the default form validator just returns field-level validations
87+ formValidator = (values, errors) => errors
8688) => {
8789 const names = Object.keys(fieldValidators);
8890
@@ -105,7 +107,7 @@ export const composeValidator = (
105107 );
106108 };
107109
108− // final-form `validate` function
110+ // the final-form `validate` function
109111 return async values => {
110112 // ask for field-level errors
111113 const errors = await fieldLevelValidator(values);
src/indigo-react/components/SelectInput.js+1−1
@@ -15,7 +15,7 @@ export default function SelectInput({
1515 className,
1616 mono = false,
1717 options = [],
18− disabled,
18+ disabled = false,
1919}) {
2020 const {
2121 input,
src/indigo-react/components/ToggleInput.js+1−1
@@ -13,7 +13,7 @@ export default function ToggleInput({
1313 className,
1414
1515 //
16− disabled,
16+ disabled = false,
1717
1818 ...rest
1919}) {
src/views/Admin/AdminNetworkingKeys.js+3−5
@@ -168,8 +168,6 @@ export default function AdminNetworkingKeys() {
168168 if (values.useNetworkSeed && errors.networkSeed) {
169169 return errors;
170170 }
171−
172− return {};
173171 }, []);
174172
175173 const validate = useMemo(
@@ -236,16 +234,16 @@ export default function AdminNetworkingKeys() {
236234 </Grid.Item>
237235 {key.matchWith({
238236 Nothing: () => (
239− <Grid.Item full as="code" className="f5 gray4">
237+ <Grid.Item full as="code" className="f5 mono gray4">
240238 Unset
241239 </Grid.Item>
242240 ),
243241 Just: ({ value: key }) => (
244242 <>
245− <Grid.Item full as="code" className="f5 wrap">
243+ <Grid.Item full as="code" className="f5 mono wrap">
246244 0x
247245 </Grid.Item>
248− <Grid.Item full as="code" className="f5 wrap">
246+ <Grid.Item full as="code" className="f5 mono wrap">
249247 {renderNetworkKey(key)}
250248 </Grid.Item>
251249 </>
src/views/Admin/AdminSetProxy.js−2
@@ -118,8 +118,6 @@ export default function AdminSetProxy() {
118118 if (!values.unset && errors.address) {
119119 return errors;
120120 }
121−
122− return {};
123121 }, []);
124122
125123 const validate = useMemo(
src/views/Invite/InviteEmail.js+12−16
@@ -50,6 +50,7 @@ import {
5050 buildEmailValidator,
5151 composeValidator,
5252 hasErrors,
53+ buildArrayValidator,
5354} from 'form/validators';
5455import { FORM_ERROR } from 'final-form';
5556import SubmitButton from 'form/SubmitButton';
@@ -98,8 +99,6 @@ const buildAccessoryFor = (dones, errors) => name => {
9899 return <AccessoryIcon.Pending />;
99100};
100101
101−const emailFormatValidator = buildEmailValidator();
102−
103102export default function InviteEmail() {
104103 const { contracts, web3, networkType } = useNetwork();
105104 const { wallet, walletType, walletHdPath } = useWallet();
@@ -135,14 +134,8 @@ export default function InviteEmail() {
135134 const isFailed = status === STATUS.FAILURE;
136135 const isDone = status === STATUS.SUCCESS;
137136
138− const validateEmail = useCallback(
137+ const validateHasReceived = useCallback(
139138 async email => {
140− // check individual email validity
141− const error = await emailFormatValidator(email);
142− if (error) {
143− return error;
144− }
145−
146139 const hasReceived = await getHasReceived(email);
147140 if (hasReceived) {
148141 return HAS_RECEIVED_TEXT;
@@ -151,11 +144,6 @@ export default function InviteEmail() {
151144 [getHasReceived]
152145 );
153146
154− const validateEmails = useCallback(
155− async emails => await Promise.all(emails.map(validateEmail)),
156− [validateEmail]
157− );
158−
159147 const validateForm = useCallback(async (values, errors) => {
160148 if (hasErrors(errors)) {
161149 return errors;
@@ -169,8 +157,16 @@ export default function InviteEmail() {
169157 }, []);
170158
171159 const validate = useMemo(
172− () => composeValidator({ emails: validateEmails }, validateForm),
173− [validateEmails, validateForm]
160+ () =>
161+ composeValidator(
162+ {
163+ emails: buildArrayValidator(
164+ buildEmailValidator([validateHasReceived])
165+ ),
166+ },
167+ validateForm
168+ ),
169+ [validateForm, validateHasReceived]
174170 );
175171
176172 // progress is [0, .length] of invites or receipts, as we're generating them
src/views/Login/Ledger.js+1−1
@@ -77,10 +77,10 @@ export default function Ledger({ className, goHome }) {
7777 const chainCode = Buffer.from(info.chainCode, 'hex');
7878 const pub = secp256k1.publicKeyConvert(publicKey, true);
7979 const hd = bip32.fromPublicKey(pub, chainCode);
80+
8081 setWallet(Just(hd));
8182 setWalletHdPath(addHdPrefix(values.hdpath));
8283 } catch (error) {
83− setWallet(Nothing());
8484 return { [FORM_ERROR]: error.message };
8585 }
8686 },
src/views/Login/PrivateKey.js+3−3
@@ -22,7 +22,7 @@ export default function PrivateKey({ className, goHome }) {
2222 const validate = useMemo(
2323 () =>
2424 composeValidator({
25− privatekey: buildHexValidator(64),
25+ privateKey: buildHexValidator(64),
2626 }),
2727 []
2828 );
@@ -30,7 +30,7 @@ export default function PrivateKey({ className, goHome }) {
3030 const onValues = useCallback(
3131 ({ valid, values }) => {
3232 if (valid) {
33− const sec = Buffer.from(stripHexPrefix(values.privatekey), 'hex');
33+ const sec = Buffer.from(stripHexPrefix(values.privateKey), 'hex');
3434 const newWallet = new EthereumWallet(sec);
3535 setWallet(Just(newWallet));
3636 } else {
@@ -48,7 +48,7 @@ export default function PrivateKey({ className, goHome }) {
4848 <Grid.Item
4949 full
5050 as={HexInput}
51− name="privatekey"
51+ name="privateKey"
5252 label="Private key"
5353 />
5454 <Grid.Item full as={FormError} />
src/views/Login/Trezor.js+10−11
@@ -60,22 +60,21 @@ export default function Trezor({ className, goHome }) {
6060 appUrl: 'https://github.com/urbit/bridge',
6161 });
6262
63− const info = await TrezorConnect.getPublicKey({
63+ const { success, payload } = await TrezorConnect.getPublicKey({
6464 path: values.hdpath,
6565 });
6666
67− if (info.success === true) {
68− const payload = info.payload;
69− const publicKey = Buffer.from(payload.publicKey, 'hex');
70− const chainCode = Buffer.from(payload.chainCode, 'hex');
71− const pub = secp256k1.publicKeyConvert(publicKey, true);
72− const hd = bip32.fromPublicKey(pub, chainCode);
73− setWallet(Just(hd));
74− setWalletHdPath(values.hdPath);
75− } else {
76− setWallet(Nothing());
67+ if (!success) {
7768 return { [FORM_ERROR]: 'Failed to authenticate with your Trezor.' };
7869 }
70+
71+ const publicKey = Buffer.from(payload.publicKey, 'hex');
72+ const chainCode = Buffer.from(payload.chainCode, 'hex');
73+ const pub = secp256k1.publicKeyConvert(publicKey, true);
74+ const hd = bip32.fromPublicKey(pub, chainCode);
75+
76+ setWallet(Just(hd));
77+ setWalletHdPath(values.hdPath);
7978 },
8079 [setWallet, setWalletHdPath]
8180 );