on 39bc817 help sign in
bridge/src/views/Activate/ActivateCode.js
157 lines 4.7 kB history raw
1import React, { useCallback, useMemo } from 'react';
2import { Just } 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';
28import { composeValidator, buildPatqValidator } from 'form/validators';
29import FormError from 'form/FormError';
30import { FORM_ERROR } from 'final-form';
32export default function ActivateCode() {
33 const history = useHistory();
34 const { names, push } = useLocalRouter();
35 const { contracts } = useNetwork();
36 const impliedTicket = useImpliedTicket();
37 const [hasDisclaimed] = useHasDisclaimed();
39 const {
40 setDerivedWallet,
41 setInviteWallet,
42 derivedPoint,
43 setDerivedPoint,
44 } = useActivateFlow();
46 const goToLogin = useCallback(() => history.popAndPush(ROUTE_NAMES.LOGIN), [
47 history,
48 ]);
50 const goToPassport = useCallback(() => {
51 push(names.PASSPORT);
53 if (!hasDisclaimed) {
54 push(names.DISCLAIMER);
55 }
56 }, [names, push, hasDisclaimed]);
58 const validate = useMemo(
59 () => composeValidator({ ticket: buildPatqValidator() }),
60 []
61 );
63 // set our state on submission
64 const onSubmit = useCallback(
65 async values => {
66 await timeout(100); // allow the ui changes to flush before we lag it out
68 const _contracts = need.contracts(contracts);
69 const { seed } = await generateTemporaryOwnershipWallet(values.ticket);
71 const inviteWallet = walletFromMnemonic(seed, DEFAULT_HD_PATH);
73 const _inviteWallet = need.wallet(inviteWallet);
75 const owned = await azimuth.azimuth.getOwnedPoints(
76 _contracts,
77 _inviteWallet.address
78 );
79 const transferring = await azimuth.azimuth.getTransferringFor(
80 _contracts,
81 _inviteWallet.address
82 );
83 const incoming = [...owned, ...transferring];
85 if (incoming.length > 0) {
86 if (incoming.length > 1) {
87 // TODO: warnings
88 // setGeneralError(
89 // 'This invite code has multiple points available.\n' +
90 // "Once you've activated this point, activate the next with the same process."
91 // );
92 }
94 const point = parseInt(incoming[0], 10);
96 setDerivedPoint(Just(point));
97 setInviteWallet(inviteWallet);
98 setDerivedWallet(Just(await generateWallet(point)));
99 } else {
100 return {
101 [FORM_ERROR]:
102 'Invite code has no claimable point.\n' +
103 'Check your invite code and try again?',
104 };
105 }
106 },
107 [contracts, setDerivedPoint, setDerivedWallet, setInviteWallet]
108 );
110 const initialValues = useMemo(() => ({ ticket: impliedTicket || '' }), [
111 impliedTicket,
112 ]);
114 return (
115 <View inset>
116 <Grid>
117 <Grid.Item full as={Passport} point={derivedPoint} />
118 <Grid.Item full as={H4} className="mt3 mb2">
119 Activate
120 </Grid.Item>
121 <BridgeForm
122 validate={validate}
123 onSubmit={onSubmit}
124 afterSubmit={goToPassport}
125 initialValues={initialValues}>
126 {({ validating, submitting, handleSubmit }) => (
127 <>
128 <Grid.Item
129 full
130 as={TicketInput}
131 name="ticket"
132 label="Activation Code"
133 />
135 <Grid.Item full as={FormError} />
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 );