on b4917df help sign in
bridge/src/views/Login.js
122 lines 3.2 kB history raw
1import { Nothing, Just } from 'folktale/maybe';
2import React from 'react';
3import * as azimuth from 'azimuth-js';
5import { H1, Row, Col } from '../components/old/Base';
6import Tabs from '../components/Tabs';
8import Ticket from './Login/Ticket';
9import Mnemonic from './Login/Mnemonic';
10import Advanced from './Login/Advanced';
12import * as need from '../lib/need';
13import { compose } from '../lib/lib';
14import { ROUTE_NAMES } from '../lib/routeNames';
16import { withHistory } from '../store/history';
17import { withNetwork } from '../store/network';
18import { withWallet } from '../store/wallet';
19import { withPointCursor } from '../store/pointCursor';
21const TABS = {
22 TICKET: Symbol('TICKET'),
23 MNEMONIC: Symbol('MNEMONIC'),
24 ADVANCED: Symbol('ADVANCED'),
25};
27class Login extends React.Component {
28 constructor(props) {
29 super(props);
31 this.state = {
32 currentTab: TABS.TICKET,
33 };
35 this.handleTabChange = this.handleTabChange.bind(this);
36 this.continue = this.continue.bind(this);
37 }
39 componentDidMount() {
40 // we expect wallet and pointCursor to not be set yet
41 this.props.setUrbitWallet(Nothing());
42 this.props.setWallet(Nothing());
43 this.props.setPointCursor(Nothing());
44 }
46 handleTabChange(currentTab) {
47 this.setState({ currentTab });
48 }
50 async continue() {
51 const wallet = need.wallet(this.props);
52 const contracts = need.contracts(this.props);
54 // if no point cursor set by login logic, try to deduce it
55 let deduced = Nothing();
56 if (Nothing.hasInstance(this.props.pointCursor)) {
57 const owned = await azimuth.azimuth.getOwnedPoints(
58 contracts,
59 wallet.address
60 );
61 if (owned.length === 1) {
62 deduced = Just(owned[0]);
63 } else if (owned.length === 0) {
64 const canOwn = await azimuth.azimuth.getTransferringFor(
65 contracts,
66 wallet.address
67 );
68 if (canOwn.length === 1) {
69 deduced = Just(canOwn[0]);
70 }
71 }
72 }
74 if (Just.hasInstance(deduced)) {
75 this.props.setPointCursor(deduced);
76 this.props.history.popAndPush(ROUTE_NAMES.POINT_HOME);
77 } else if (Just.hasInstance(this.props.pointCursor)) {
78 this.props.history.popAndPush(ROUTE_NAMES.POINT_HOME);
79 } else {
80 //TODO to new overview (maybe should be merged into point home?)
81 this.props.history.popAndPush(ROUTE_NAMES.SHIPS);
82 }
83 }
85 render() {
86 const tabOptions = [
87 { title: 'Ticket', value: TABS.TICKET },
88 { title: 'Mnemonic', value: TABS.MNEMONIC },
89 { title: 'Advanced', value: TABS.ADVANCED },
90 ];
92 const tabViews = {
93 [TABS.TICKET]: Ticket,
94 [TABS.MNEMONIC]: Mnemonic,
95 [TABS.ADVANCED]: Advanced,
96 };
98 return (
99 <Row>
100 <Col>
101 <H1>{'Login'}</H1>
103 <Tabs
104 tabViews={tabViews}
105 tabOptions={tabOptions}
106 currentTab={this.state.currentTab}
107 onTabChange={this.handleTabChange}
108 //
109 loginCompleted={this.continue}
110 />
111 </Col>
112 </Row>
113 );
114 }
117export default compose(
118 withHistory,
119 withNetwork,
120 withWallet,
121 withPointCursor
122)(Login);