on abbbcbc help sign in
bridge/src/views/InvitesManage.js
185 lines 5.2 kB history raw
1import { Just, Nothing } from 'folktale/maybe';
2import React from 'react';
3import * as ob from 'urbit-ob';
4import * as azimuth from 'azimuth-js';
5import * as need from '../lib/need';
7import {
8 Warning,
9 Input,
10 PointInput,
11 InnerLabel,
12 ValidatedSigil,
13} from '../components/old/Base';
14import StatelessTransaction from '../components/old/StatelessTransaction';
15import { withNetwork } from '../store/network';
16import { compose } from '../lib/lib';
17import { withPointCursor } from '../store/pointCursor';
18import View from 'components/View';
20class InvitesManage extends React.Component {
21 constructor(props) {
22 super(props);
24 this.state = {
25 invitesWillWork: Nothing(),
26 planetInput: '',
27 targetPlanet: Nothing(),
28 currentPoolSize: Nothing(),
29 cachedPoolSizes: {},
30 targetPoolSize: 5,
31 };
33 this.checkCurrentInvites = this.checkCurrentInvites.bind(this);
34 this.handlePointInput = this.handlePointInput.bind(this);
35 this.updatePoolSize = this.updatePoolSize.bind(this);
36 this.handlePoolSizeInput = this.handlePoolSizeInput.bind(this);
37 this.createUnsignedTxn = this.createUnsignedTxn.bind(this);
38 this.statelessRef = React.createRef();
39 }
41 componentDidMount() {
42 this.point = need.point(this.props.pointCursor);
43 this.contracts = need.contracts(this.props.contracts);
45 azimuth.azimuth
46 .isSpawnProxy(
47 this.contracts,
48 this.point,
49 this.contracts.delegatedSending.address
50 )
51 .then(isSpawnProxy => {
52 this.setState({ invitesWillWork: Just(isSpawnProxy) });
53 });
54 }
56 componentDidUpdate(prevProps) {
57 //
58 }
60 handlePointInput(targetPlanet) {
61 let newState = {
62 targetPlanet: Nothing(),
63 planetInput: targetPlanet,
64 canGenerate: false,
65 currentPoolSize: Nothing(),
66 };
67 if (targetPlanet.length === 14 && ob.isValidPatp(targetPlanet)) {
68 const target = ob.patp2dec(targetPlanet);
69 //TODO there must be a simpler way to do this check
70 const isChild = ob.sein(targetPlanet) === ob.patp(this.point);
71 if (isChild) {
72 newState.targetPlanet = Just(target);
73 newState.currentPoolSize = this.checkCurrentInvites(target);
74 newState.canGenerate = true;
75 }
76 }
77 this.setState(newState);
78 }
80 checkCurrentInvites(who) {
81 const cachedPoolSizes = this.state.cachedPoolSizes;
82 if (typeof cachedPoolSizes[who] !== 'undefined') {
83 return Just(cachedPoolSizes[who]);
84 } else {
85 azimuth.delegatedSending.getPool(this.contracts, who).then(pool => {
86 azimuth.delegatedSending
87 .invitesInPool(this.contracts, pool, this.point)
88 .then(size => {
89 this.updatePoolSize(who, size);
90 });
91 });
92 return Nothing();
93 }
94 }
96 updatePoolSize(who, size) {
97 const cache = this.state.cachedPoolSizes;
98 cache[who] = size;
99 this.setState({ cachedPoolSizes: cache });
100 const target = this.state.targetPlanet;
101 const match = Just.hasInstance(target) ? who === target.value : false;
102 if (match) {
103 this.setState({ currentPoolSize: Just(size) });
104 }
105 }
107 handlePoolSizeInput(targetSize) {
108 this.setState({ targetPoolSize: targetSize });
109 }
111 createUnsignedTxn() {
112 return Just(
113 azimuth.delegatedSending.setPoolSize(
114 this.contracts,
115 this.point,
116 this.state.targetPlanet.value,
117 this.state.targetPoolSize
118 )
119 );
120 }
122 render() {
123 let spawnProxyWarning = null;
124 if (this.state.invitesWillWork.value === false) {
125 spawnProxyWarning = (
126 <Warning>
127 <h3 className={'mb-2'}>{'Warning'}</h3>
128 {'Planets under this star will not be able to send invites ' +
129 'until the invite contract ('}
130 <code>{this.contracts.delegatedSending.address}</code>
131 {') is made spawn proxy for this star.'}
132 </Warning>
133 );
134 }
136 let poolSizeText = this.state.currentPoolSize.matchWith({
137 Just: ps => `(currently ${ps.value})`,
138 Nothing: _ => '',
139 });
141 return (
142 <View>
143 <p>
144 {
145 'manage invites here, for stars. can only give invites to child planets'
146 }
147 </p>
149 {spawnProxyWarning}
151 <PointInput
152 prop-format="innerlabel"
153 prop-size="lg"
154 placeholder={'~sampel-sipnem'}
155 value={this.state.planetInput}
156 onChange={this.handlePointInput}>
157 <InnerLabel>{'Planet to set invites for'}</InnerLabel>
158 <ValidatedSigil
159 patp={this.state.targetPlanet}
160 validator={() => this.validatePoint}
161 />
162 </PointInput>
164 <Input
165 prop-format="innerlabel"
166 prop-size="lg"
167 value={this.state.targetPoolSize}
168 onChange={this.handlePoolSizeInput}>
169 <InnerLabel>{`Available invites ${poolSizeText}`}</InnerLabel>
170 </Input>
172 <StatelessTransaction
173 canGenerate={this.state.canGenerate}
174 createUnsignedTxn={this.createUnsignedTxn}
175 ref={this.statelessRef}
176 />
177 </View>
178 );
179 }
182export default compose(
183 withNetwork,
184 withPointCursor
185)(InvitesManage);