on 58fbcd2 help sign in
bridge/src/views/Party/PartySetPoolSize.js
162 lines 4.5 kB history raw
1import React, { useCallback, useMemo } from 'react';
2import cn from 'classnames';
3import { Grid, Text } from 'indigo-react';
4import * as azimuth from 'azimuth-js';
6import { useNetwork } from 'store/network';
7import { usePointCursor } from 'store/pointCursor';
8import { usePointCache } from 'store/pointCache';
10import * as need from 'lib/need';
11import useEthereumTransaction from 'lib/useEthereumTransaction';
12import { GAS_LIMITS } from 'lib/constants';
13import { useLocalRouter } from 'lib/LocalRouter';
14import useCurrentPermissions from 'lib/useCurrentPermissions';
15import patp2dec from 'lib/patp2dec';
17import ViewHeader from 'components/ViewHeader';
18import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
19import View from 'components/View';
20import WarningBox from 'components/WarningBox';
22import BridgeForm from 'form/BridgeForm';
23import { PointInput, NumberInput } from 'form/Inputs';
24import {
25 composeValidator,
26 buildNumberValidator,
27 buildPointValidator,
28} from 'form/validators';
29import FormError from 'form/FormError';
31function useSetPoolSize() {
32 const { contracts } = useNetwork();
33 const { pointCursor } = usePointCursor();
34 const { syncOwnedPoint } = usePointCache();
36 const _contracts = need.contracts(contracts);
37 const _point = need.point(pointCursor);
39 return useEthereumTransaction(
40 useCallback(
41 (poolOwner, poolSize) =>
42 azimuth.delegatedSending.setPoolSize(
43 _contracts,
44 _point,
45 poolOwner,
46 poolSize
47 ),
48 [_contracts, _point]
49 ),
50 useCallback(() => syncOwnedPoint(_point), [_point, syncOwnedPoint]),
51 GAS_LIMITS.DEFAULT // TODO: GAS_LIMITS.SET_POOL_SIZE
52 );
55export default function PartySetPoolSize() {
56 const { pop } = useLocalRouter();
57 const { contracts } = useNetwork();
59 const _contracts = need.contracts(contracts);
61 const { spawnIsDelegatedSending } = useCurrentPermissions();
63 const {
64 construct,
65 unconstruct,
66 completed,
67 inputsLocked,
68 bind,
69 } = useSetPoolSize();
71 const validate = useMemo(
72 () =>
73 composeValidator({
74 poolOwner: buildPointValidator(),
75 poolSize: buildNumberValidator(0),
76 }),
77 []
78 );
80 const onValues = useCallback(
81 ({ valid, values }) => {
82 if (valid) {
83 construct(patp2dec(values.poolOwner), values.poolSize);
84 } else {
85 unconstruct();
86 }
87 },
88 [construct, unconstruct]
89 );
91 const initialValues = useMemo(() => ({ poolSize: 5 }), []);
93 return (
94 <View pop={pop} inset>
95 <Grid>
96 <Grid.Item full as={ViewHeader}>
97 Set Pool Size
98 </Grid.Item>
100 {!spawnIsDelegatedSending && (
101 <Grid.Item full as={WarningBox} className="mb4">
102 The spawn proxy must be set to{' '}
103 <code className="mono f6">
104 {_contracts.delegatedSending.address}
105 </code>{' '}
106 for invitations to be available.
107 </Grid.Item>
108 )}
110 <BridgeForm
111 validate={validate}
112 onValues={onValues}
113 initialValues={initialValues}>
114 {({ handleSubmit, values }) => (
115 <>
116 <Grid.Item
117 full
118 as={Text}
119 className={cn('f5', {
120 green3: completed,
121 })}>
122 {completed
123 ? `${values.poolSize} invites have been allocated to ${values.poolOwner}`
124 : `Allocate invites to a child point.`}
125 </Grid.Item>
127 {!completed && (
128 <>
129 <Grid.Item
130 full
131 as={PointInput}
132 className="mt4"
133 name="poolOwner"
134 label="Point"
135 disabled={inputsLocked}
136 />
137 <Grid.Item
138 full
139 as={NumberInput}
140 className="mb4"
141 name="poolSize"
142 label="Pool Size"
143 disabled={inputsLocked}
144 />
145 </>
146 )}
148 <Grid.Item full as={FormError} />
150 <Grid.Item
151 full
152 as={InlineEthereumTransaction}
153 {...bind}
154 onReturn={() => pop()}
155 />
156 </>
157 )}
158 </BridgeForm>
159 </Grid>
160 </View>
161 );