on 6b3335e help sign in
bridge/src/views/Activate/ActivateCode.js
157 lines 4.9 kB history raw
1import React, { useCallback, useRef } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import * as azimuth from 'azimuth-js';
4import { Grid, H4 } from 'indigo-react';
6import View from 'components/View';
7import { ForwardButton } from 'components/Buttons';
8import Passport from 'components/Passport';
10import { useHistory } from 'store/history';
12import * as need from 'lib/need';
13import { ROUTE_NAMES } from 'lib/routeNames';
14import FooterButton from 'components/FooterButton';
15import { DEFAULT_HD_PATH, walletFromMnemonic } from 'lib/wallet';
16import { useNetwork } from 'store/network';
17import { generateWallet } from 'lib/invite';
18import { generateTemporaryOwnershipWallet } from 'lib/walletgen';
19import { useActivateFlow } from './ActivateFlow';
20import { useLocalRouter } from 'lib/LocalRouter';
21import useImpliedTicket from 'lib/useImpliedTicket';
22import timeout from 'lib/timeout';
23import useHasDisclaimed from 'lib/useHasDisclaimed';
25import BridgeForm from 'form/BridgeForm';
26import SubmitButton from 'form/SubmitButton';
27import { TicketInput } from 'form/Inputs';
29export default function ActivateCode() {
30 const history = useHistory();
31 const { names, push } = useLocalRouter();
32 const { contracts } = useNetwork();
33 const impliedTicket = useImpliedTicket();
34 const [hasDisclaimed] = useHasDisclaimed();
36 const cachedInviteWallet = useRef(Nothing());
37 const cachedPoint = useRef();
39 const {
40 setDerivedWallet,
41 setInviteWallet,
42 derivedPoint,
43 setDerivedPoint,
44 } = useActivateFlow();
46 const goToLogin = useCallback(() => history.popAndPush(ROUTE_NAMES.LOGIN), [
47 history,
48 ]);
49 const goToPassport = useCallback(() => {
50 push(names.PASSPORT);
52 if (!hasDisclaimed) {
53 push(names.DISCLAIMER);
54 }
55 }, [names, push, hasDisclaimed]);
57 // validate should be a pure function but we don't want to have to recompute
58 // all of this information on submit, so cache the invite wallet and avoid
59 // re-renders that may trigger re-validations (causing infinite loop)
60 const validate = useCallback(
61 async ticket => {
62 await timeout(100); // allow the ui changes to flush before we lag it out
64 const _contracts = need.contracts(contracts);
65 const { seed } = await generateTemporaryOwnershipWallet(ticket);
67 // TODO(fang): isn't all this accessible in the ownership object?
68 const inviteWallet = walletFromMnemonic(seed, DEFAULT_HD_PATH);
69 cachedInviteWallet.current = inviteWallet;
71 const _inviteWallet = need.wallet(inviteWallet);
73 const owned = await azimuth.azimuth.getOwnedPoints(
74 _contracts,
75 _inviteWallet.address
76 );
77 const transferring = await azimuth.azimuth.getTransferringFor(
78 _contracts,
79 _inviteWallet.address
80 );
81 const incoming = [...owned, ...transferring];
83 if (incoming.length > 0) {
84 if (incoming.length > 1) {
85 // TODO: warnings
86 // setGeneralError(
87 // 'This invite code has multiple points available.\n' +
88 // "Once you've activated this point, activate the next with the same process."
89 // );
90 }
92 const point = parseInt(incoming[0], 10);
93 setDerivedPoint(Just(point));
94 cachedPoint.current = point;
95 } else {
96 return (
97 'Invite code has no claimable point.\n' +
98 'Check your invite code and try again?'
99 );
100 }
101 },
102 [contracts, setDerivedPoint]
103 );
105 // set our state on submission
106 const onSubmit = useCallback(
107 async values => {
108 setInviteWallet(cachedInviteWallet.current);
109 setDerivedWallet(Just(await generateWallet(cachedPoint)));
111 goToPassport();
112 },
113 [goToPassport, setDerivedWallet, setInviteWallet]
114 );
116 return (
117 <View inset>
118 <Grid>
119 <Grid.Item full as={Passport} point={derivedPoint} />
120 <Grid.Item full as={H4} className="mt3 mb2">
121 Activate
122 </Grid.Item>
123 <BridgeForm
124 onSubmit={onSubmit}
125 initialValues={{ ticket: impliedTicket || '' }}>
126 {({ validating, submitting, handleSubmit }) => (
127 <>
128 <Grid.Item
129 full
130 as={TicketInput}
131 name="ticket"
132 label="Activation Code"
133 validate={validate}
134 autoFocus
135 />
137 <Grid.Item
138 full
139 as={SubmitButton}
140 className="mt4"
141 handleSubmit={handleSubmit}>
142 {validating
143 ? 'Deriving...'
144 : submitting
145 ? 'Generating...'
146 : 'Go'}
147 </Grid.Item>
148 </>
149 )}
150 </BridgeForm>
151 </Grid>
152 <FooterButton as={ForwardButton} onClick={goToLogin}>
153 Login
154 </FooterButton>
155 </View>
156 );