@@ -1,7 +1,7 @@
1−import React, { useCallback, useEffect, useState } from 'react';
1+import React, { useCallback, useRef } from 'react';
22import { Just, Nothing } from 'folktale/maybe';
33import * as azimuth from 'azimuth-js';
4−import { Grid, Input, H4, ErrorText } from 'indigo-react';
4+import { Grid, H4 } from 'indigo-react';
55
66import View from 'components/View';
77import { ForwardButton } from 'components/Buttons';
@@ -9,12 +9,9 @@ import Passport from 'components/Passport';
99
1010import { useHistory } from 'store/history';
1111
12−import { useTicketInput } from 'lib/useInputs';
1312import * as need from 'lib/need';
1413import { ROUTE_NAMES } from 'lib/routeNames';
15−import { useSyncKnownPoints } from 'lib/useSyncPoints';
1614import FooterButton from 'components/FooterButton';
17−import { blinkIf } from 'components/Blinky';
1815import { DEFAULT_HD_PATH, walletFromMnemonic } from 'lib/wallet';
1916import { useNetwork } from 'store/network';
2017import { generateWallet } from 'lib/invite';
@@ -22,111 +19,96 @@ import { generateTemporaryOwnershipWallet } from 'lib/walletgen';
2219import { useActivateFlow } from './ActivateFlow';
2320import { useLocalRouter } from 'lib/LocalRouter';
2421import useImpliedTicket from 'lib/useImpliedTicket';
22+import timeout from 'lib/timeout';
2523import useHasDisclaimed from 'lib/useHasDisclaimed';
2624
25+import BridgeForm from 'form/BridgeForm';
26+import SubmitButton from 'form/SubmitButton';
27+import { TicketInput } from 'form/Inputs';
28+
2729export default function ActivateCode() {
2830 const history = useHistory();
2931 const { names, push } = useLocalRouter();
3032 const { contracts } = useNetwork();
3133 const impliedTicket = useImpliedTicket();
3234 const [hasDisclaimed] = useHasDisclaimed();
33− const [generalError, setGeneralError] = useState();
34− const [deriving, setDeriving] = useState(false);
35+
36+ const cachedInviteWallet = useRef(Nothing());
37+ const cachedPoint = useRef();
38+
3539 const {
36− derivedWallet,
3740 setDerivedWallet,
3841 setInviteWallet,
3942 derivedPoint,
4043 setDerivedPoint,
4144 } = useActivateFlow();
4245
43− const [ticketInput, { pass: validTicket, data: ticket }] = useTicketInput({
44− name: 'ticket',
45− label: 'Activation Code',
46− initialValue: impliedTicket || '',
47− autoFocus: true,
48− });
49−
5046 const goToLogin = useCallback(() => history.popAndPush(ROUTE_NAMES.LOGIN), [
5147 history,
5248 ]);
5349 const goToPassport = useCallback(() => {
5450 push(names.PASSPORT);
51+
5552 if (!hasDisclaimed) {
5653 push(names.DISCLAIMER);
5754 }
5855 }, [names, push, hasDisclaimed]);
5956
60− const pass = derivedWallet.matchWith({
61− Nothing: () => false,
62− Just: () => true,
63− });
57+ const validate = useCallback(
58+ async ticket => {
59+ await timeout(100); // allow the ui changes to flush before we lag it out
6460
65− useEffect(() => {
66− if (validTicket) {
6761 const _contracts = need.contracts(contracts);
62+ const { seed } = await generateTemporaryOwnershipWallet(ticket);
63+
64+ // TODO(fang): isn't all this accessible in the ownership object?
65+ const inviteWallet = walletFromMnemonic(seed, DEFAULT_HD_PATH);
66+ cachedInviteWallet.current = inviteWallet;
67+
68+ const _inviteWallet = need.wallet(inviteWallet);
69+
70+ const owned = await azimuth.azimuth.getOwnedPoints(
71+ _contracts,
72+ _inviteWallet.address
73+ );
74+ const transferring = await azimuth.azimuth.getTransferringFor(
75+ _contracts,
76+ _inviteWallet.address
77+ );
78+ const incoming = [...owned, ...transferring];
79+
80+ if (incoming.length > 0) {
81+ if (incoming.length > 1) {
82+ // TODO: warnings
83+ // setGeneralError(
84+ // 'This invite code has multiple points available.\n' +
85+ // "Once you've activated this point, activate the next with the same process."
86+ // );
87+ }
6888
69− setDeriving(true);
70− // when the ticket becomes valid, derive the point
71− (async () => {
72− const { seed } = await generateTemporaryOwnershipWallet(ticket);
73−
74− //TODO isn't all this accessible in the ownership object?
75− const inviteWallet = walletFromMnemonic(seed, DEFAULT_HD_PATH);
76− const _inviteWallet = need.wallet(inviteWallet);
77−
78− const owned = await azimuth.azimuth.getOwnedPoints(
79− _contracts,
80− _inviteWallet.address
81− );
82− const transferring = await azimuth.azimuth.getTransferringFor(
83− _contracts,
84− _inviteWallet.address
89+ const point = parseInt(incoming[0], 10);
90+ // setDerivedPoint(Just(point));
91+ cachedPoint.current = point;
92+ } else {
93+ return (
94+ 'Invite code has no claimable point.\n' +
95+ 'Check your invite code and try again?'
8596 );
86− const incoming = [...owned, ...transferring];
87−
88− let realPoint = Nothing();
89− let wallet = Nothing();
90−
91− if (incoming.length > 0) {
92− const pointNum = parseInt(incoming[0], 10);
93− realPoint = Just(pointNum);
94− wallet = Just(await generateWallet(pointNum));
95−
96− if (incoming.length > 1) {
97− setGeneralError(
98− 'This invite code has multiple points available.\n' +
99− "Once you've activated this point, activate the next with the same process."
100− );
101− } else {
102− setGeneralError(false);
103− }
104− } else {
105− setGeneralError(
106− 'Invite code has no claimable point.\n' +
107− 'Check your invite code and try again?'
108− );
109− }
97+ }
98+ },
99+ [contracts]
100+ );
110101
111− setDerivedPoint(realPoint);
112− setDerivedWallet(wallet);
113− setInviteWallet(inviteWallet);
114− setDeriving(false);
115− })();
116− } else {
117− setGeneralError(false);
118− }
119− }, [
120− validTicket,
121− contracts,
122− ticket,
123− setDerivedPoint,
124− setDerivedWallet,
125− setInviteWallet,
126− ]);
102+ const onSubmit = useCallback(
103+ async values => {
104+ setInviteWallet(cachedInviteWallet.current);
105+ setDerivedPoint(Just(cachedPoint.current));
106+ setDerivedWallet(Just(await generateWallet(cachedPoint)));
127107
128− // when we know the derived point, ensure we have the data to display it
129− useSyncKnownPoints([derivedPoint.getOrElse(null)].filter(p => p !== null));
108+ goToPassport();
109+ },
110+ [goToPassport, setDerivedPoint, setDerivedWallet, setInviteWallet]
111+ );
130112
131113 return (
132114 <View inset>
@@ -135,23 +117,34 @@ export default function ActivateCode() {
135117 <Grid.Item as={H4} className="mt3 mb2" full>
136118 Activate
137119 </Grid.Item>
138− <Grid.Item as={Input} {...ticketInput} full />
139− {generalError && (
140− <Grid.Item full>
141− <ErrorText>{generalError}</ErrorText>{' '}
142− </Grid.Item>
143− )}
144− <Grid.Item
145− as={ForwardButton}
146− className="mt4"
147− disabled={!pass || deriving}
148− accessory={blinkIf(deriving)}
149− onClick={goToPassport}
150− solid
151− full>
152− {deriving && 'Deriving...'}
153− {!deriving && 'Go'}
154− </Grid.Item>
120+ <BridgeForm
121+ onSubmit={onSubmit}
122+ initialValues={{ ticket: impliedTicket || '' }}>
123+ {({ validating, submitting, handleSubmit }) => (
124+ <>
125+ <Grid.Item
126+ full
127+ as={TicketInput}
128+ name="ticket"
129+ label="Activation Code"
130+ config={{ validate }}
131+ autoFocus
132+ />
133+
134+ <Grid.Item
135+ full
136+ as={SubmitButton}
137+ className="mt4"
138+ handleSubmit={handleSubmit}>
139+ {validating
140+ ? 'Deriving...'
141+ : submitting
142+ ? 'Generating...'
143+ : 'Go'}
144+ </Grid.Item>
145+ </>
146+ )}
147+ </BridgeForm>
155148 </Grid>
156149 <FooterButton as={ForwardButton} onClick={goToLogin}>
157150 Login