on f231080 help sign in
bridge/src/views/Activate/ActivateCode.js
153 lines 4.6 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 const validate = useCallback(
58 async ticket => {
59 await timeout(100); // allow the ui changes to flush before we lag it out
61 const _contracts = need.contracts(contracts);
62 const { seed } = await generateTemporaryOwnershipWallet(ticket);
64 // TODO(fang): isn't all this accessible in the ownership object?
65 const inviteWallet = walletFromMnemonic(seed, DEFAULT_HD_PATH);
66 cachedInviteWallet.current = inviteWallet;
68 const _inviteWallet = need.wallet(inviteWallet);
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];
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 }
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?'
96 );
97 }
98 },
99 [contracts, setDerivedPoint]
100 );
102 const onSubmit = useCallback(
103 async values => {
104 setInviteWallet(cachedInviteWallet.current);
105 setDerivedWallet(Just(await generateWallet(cachedPoint)));
107 goToPassport();
108 },
109 [goToPassport, setDerivedWallet, setInviteWallet]
110 );
112 return (
113 <View inset>
114 <Grid>
115 <Grid.Item as={Passport} point={derivedPoint} full />
116 <Grid.Item as={H4} className="mt3 mb2" full>
117 Activate
118 </Grid.Item>
119 <BridgeForm
120 onSubmit={onSubmit}
121 initialValues={{ ticket: impliedTicket || '' }}>
122 {({ validating, submitting, handleSubmit }) => (
123 <>
124 <Grid.Item
125 full
126 as={TicketInput}
127 name="ticket"
128 label="Activation Code"
129 validate={validate}
130 autoFocus
131 />
133 <Grid.Item
134 full
135 as={SubmitButton}
136 className="mt4"
137 handleSubmit={handleSubmit}>
138 {validating
139 ? 'Deriving...'
140 : submitting
141 ? 'Generating...'
142 : 'Go'}
143 </Grid.Item>
144 </>
145 )}
146 </BridgeForm>
147 </Grid>
148 <FooterButton as={ForwardButton} onClick={goToLogin}>
149 Login
150 </FooterButton>
151 </View>
152 );