on 2fe1d2f help sign in
bridge/src/views/Activate/ActivateCode.js
156 lines 4.8 kB history raw
1import React, { useCallback, useEffect, useState } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import * as azimuth from 'azimuth-js';
4import { Grid, Input, H4, ErrorText } from 'indigo-react';
6import View from 'components/View';
7import { ForwardButton } from 'components/Buttons';
8import Passport from 'components/Passport';
10import { useHistory } from 'store/history';
12import { useTicketInput } from 'lib/useInputs';
13import * as need from 'lib/need';
14import { ROUTE_NAMES } from 'lib/routeNames';
15import { useSyncKnownPoints } from 'lib/useSyncPoints';
16import FooterButton from 'components/FooterButton';
17import Blinky from 'components/Blinky';
18import { DEFAULT_HD_PATH, walletFromMnemonic } from 'lib/wallet';
19import { useNetwork } from 'store/network';
20import { generateWallet } from 'lib/invite';
21import { generateTemporaryOwnershipWallet } from 'lib/walletgen';
22import { useActivateFlow } from './ActivateFlow';
23import { useLocalRouter } from 'lib/LocalRouter';
24import useImpliedTicket from 'lib/useImpliedTicket';
26export default function ActivateCode() {
27 const history = useHistory();
28 const { names, push } = useLocalRouter();
29 const { contracts } = useNetwork();
30 const impliedTicket = useImpliedTicket();
31 const [generalError, setGeneralError] = useState();
32 const [deriving, setDeriving] = useState(false);
33 const {
34 derivedWallet,
35 setDerivedWallet,
36 setInviteWallet,
37 derivedPoint,
38 setDerivedPoint,
39 } = useActivateFlow();
41 const [ticketInput, { pass: validTicket }] = useTicketInput({
42 name: 'ticket',
43 label: 'Code',
44 initialValue: impliedTicket || '',
45 autoFocus: true,
46 });
48 const goToLogin = useCallback(() => history.popAndPush(ROUTE_NAMES.LOGIN), [
49 history,
50 ]);
51 const goToPassport = useCallback(() => push(names.PASSPORT), [names, push]);
53 const pass = derivedWallet.matchWith({
54 Nothing: () => false,
55 Just: () => true,
56 });
58 useEffect(() => {
59 if (validTicket) {
60 const _contracts = need.contracts(contracts);
62 setDeriving(true);
63 // when the ticket becomes valid, derive the point
64 (async () => {
65 const { seed } = await generateTemporaryOwnershipWallet(
66 ticketInput.data
67 );
69 //TODO isn't all this accessible in the ownership object?
70 const inviteWallet = walletFromMnemonic(seed, DEFAULT_HD_PATH);
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 let realPoint = Nothing();
84 let wallet = Nothing();
86 if (incoming.length > 0) {
87 const pointNum = parseInt(incoming[0], 10);
88 realPoint = Just(pointNum);
89 wallet = Just(await generateWallet(pointNum));
91 if (incoming.length > 1) {
92 setGeneralError(
93 'This invite code has multiple points available.\n' +
94 "Once you've activated this point, activate the next with the same process."
95 );
96 } else {
97 setGeneralError(false);
98 }
99 } else {
100 setGeneralError(
101 'Invite code has no claimable point.\n' +
102 'Check your invite code and try again?'
103 );
104 }
106 setDerivedPoint(realPoint);
107 setDerivedWallet(wallet);
108 setInviteWallet(inviteWallet);
109 setDeriving(false);
110 })();
111 } else {
112 setGeneralError(false);
113 }
114 }, [
115 validTicket,
116 contracts,
117 ticketInput.data,
118 setDerivedPoint,
119 setDerivedWallet,
120 setInviteWallet,
121 ]);
123 // when we know the derived point, ensure we have the data to display it
124 useSyncKnownPoints([derivedPoint.getOrElse(null)].filter(p => p !== null));
126 return (
127 <View>
128 <Grid>
129 <Grid.Item as={Passport} point={derivedPoint} full />
130 <Grid.Item as={H4} className="mt3 mb2" full>
131 Activate
132 </Grid.Item>
133 <Grid.Item as={Input} {...ticketInput} full />
134 {generalError && (
135 <Grid.Item full>
136 <ErrorText>{generalError}</ErrorText>{' '}
137 </Grid.Item>
138 )}
139 <Grid.Item
140 as={ForwardButton}
141 className="mt4"
142 disabled={!pass || deriving}
143 accessory={deriving ? <Blinky /> : undefined}
144 onClick={goToPassport}
145 solid
146 full>
147 {deriving && 'Deriving...'}
148 {!deriving && 'Go'}
149 </Grid.Item>
150 </Grid>
151 <FooterButton as={ForwardButton} onClick={goToLogin}>
152 Login
153 </FooterButton>
154 </View>
155 );