on 513ddcb help sign in
bridge/src/views/PointHome.js
111 lines 2.8 kB history raw
1import { Just, Nothing } from 'folktale/maybe';
2import React from 'react';
3import * as azimuth from 'azimuth-js';
4import * as need from '../lib/need';
6import { Row, Col, P } from '../components/old/Base';
8import { compose } from '../lib/lib';
10import { withNetwork } from '../store/network';
11import { withPointCursor } from '../store/pointCursor';
12import { withPointCache } from '../store/pointCache';
14class PointHome extends React.Component {
15 constructor(props) {
16 super(props);
18 this.state = {
19 birthday: Nothing(),
20 spawned: [],
21 invites: Nothing(),
22 };
23 }
25 componentDidMount() {
26 this.cachePoints();
27 this.updateBirthday();
28 }
30 //TODO can probably re-use this to implement point switching
31 componentDidUpdate(prevProps) {
32 //
33 // NB (jtobin)
34 //
35 // When a link in the 'issued points' component is clicked, we don't
36 // actually leave the Point component, and componentDidMount will not fire
37 // again. But, we can force a call to cachePoints here.
38 //
39 const oldCursor = prevProps.pointCursor.getOrElse(true);
40 const newCursor = this.props.pointCursor.getOrElse(true);
42 if (oldCursor !== newCursor) {
43 this.cachePoints();
44 }
45 }
47 cachePoints() {
48 const { web3, contracts, pointCursor, addToPointCache } = this.props;
50 web3.chain(_ =>
51 contracts.chain(ctrcs =>
52 pointCursor.chain(point => {
53 azimuth.azimuth
54 .getPoint(ctrcs, point)
55 .then(details => addToPointCache({ [point]: details }));
56 azimuth.delegatedSending
57 .getTotalUsableInvites(ctrcs, point)
58 .then(count => this.setState({ invites: Just(count) }));
59 this.updateSpawned(ctrcs, point);
60 })
61 )
62 );
63 }
65 async updateBirthday() {
66 const birthBlock = await azimuth.azimuth.getActivationBlock(
67 need.contracts(this.props),
68 need.pointCursor(this.props)
69 );
70 if (birthBlock === 0) {
71 this.setState({ birthday: Nothing() });
72 } else {
73 const block = await need.web3(this.props).eth.getBlock(birthBlock);
74 //TODO add to point cache
75 this.setState({ birthday: Just(new Date(block.timestamp * 1000)) });
76 }
77 }
79 updateSpawned = contracts => {
80 const { pointCursor } = this.props;
82 pointCursor.matchWith({
83 Nothing: _ => {},
84 Just: point => {
85 azimuth.azimuth
86 .getSpawned(contracts, point.value)
87 .then(spawned => this.setState({ spawned }));
88 },
89 });
90 };
92 render() {
93 return (
94 <Row>
95 <Col>
96 <P>Admin</P>
98 <P>Boot Arvo</P>
100 <P>Invite</P>
101 </Col>
102 </Row>
103 );
104 }
107export default compose(
108 withNetwork,
109 withPointCursor,
110 withPointCache
111)(PointHome);