-
Notifications
You must be signed in to change notification settings - Fork 81
/
CreateSchemeRegistrarProposal.tsx
454 lines (406 loc) · 18.4 KB
/
CreateSchemeRegistrarProposal.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import { IProposalType, ISchemeState, Scheme } from "@daostack/client";
import * as arcActions from "actions/arcActions";
import { enableWalletProvider, getArc } from "arc";
import * as classNames from "classnames";
import Loading from "components/Shared/Loading";
import withSubscription, { ISubscriptionProps } from "components/Shared/withSubscription";
import { ErrorMessage, Field, Form, Formik, FormikProps } from "formik";
import { schemeNameAndAddress, isValidUrl, GetSchemeIsActiveActions, getSchemeIsActive } from "lib/util";
import * as React from "react";
import { connect } from "react-redux";
import { showNotification } from "reducers/notifications";
import TagsSelector from "components/Proposal/Create/SchemeForms/TagsSelector";
import TrainingTooltip from "components/Shared/TrainingTooltip";
import * as css from "../CreateProposal.scss";
import MarkdownField from "./MarkdownField";
interface IExternalProps {
daoAvatarAddress: string;
handleClose: () => any;
scheme: ISchemeState;
}
interface IDispatchProps {
createProposal: typeof arcActions.createProposal;
showNotification: typeof showNotification;
}
const mapDispatchToProps = {
createProposal: arcActions.createProposal,
showNotification,
};
type IProps = IExternalProps & IDispatchProps & ISubscriptionProps<Scheme[]>;
interface IFormValues {
description: string;
otherScheme: string;
parametersHash: string;
permissions: {
registerSchemes: boolean;
changeConstraints: boolean;
upgradeController: boolean;
genericCall: boolean;
};
schemeToAdd: string;
schemeToEdit: string;
schemeToRemove: string;
title: string;
url: string;
[key: string]: any;
}
interface IState {
currentTab: string;
tags: Array<string>;
}
class CreateSchemeRegistrarProposal extends React.Component<IProps, IState> {
constructor(props: IProps) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this.state = {
currentTab: "addScheme",
tags: new Array<string>(),
};
}
public async handleSubmit(values: IFormValues, { setSubmitting }: any ): Promise<void> {
if (!await enableWalletProvider({ showNotification: this.props.showNotification })) { return; }
let permissions = 1;
if (values.permissions.registerSchemes) {
permissions += 2;
}
if (values.permissions.changeConstraints) {
permissions += 4;
}
if (values.permissions.upgradeController) {
permissions += 8;
}
if (values.permissions.genericCall) {
permissions += 16;
}
const currentTab = this.state.currentTab;
let proposalType;
if (this.state.currentTab === "removeScheme") {
proposalType = IProposalType.SchemeRegistrarRemove;
} else if (this.state.currentTab === "addScheme") {
proposalType = IProposalType.SchemeRegistrarAdd;
} else {
proposalType = IProposalType.SchemeRegistrarEdit;
}
const proposalValues = {
...values,
dao: this.props.daoAvatarAddress,
type: proposalType,
parametersHash: values.parametersHash,
permissions: "0x" + permissions.toString(16).padStart(8, "0"),
scheme: this.props.scheme.address,
schemeToRegister: currentTab === "addScheme" ? values.schemeToAdd :
currentTab === "editScheme" ? values.schemeToEdit :
values.schemeToRemove,
tags: this.state.tags,
};
setSubmitting(false);
await this.props.createProposal(proposalValues);
this.props.handleClose();
}
public handleTabClick = (tab: string) => (_e: any) => {
this.setState({ currentTab: tab });
}
private onTagsChange = (tags: any[]): void => {
this.setState({tags});
}
public render(): RenderOutput {
// "schemes" are the schemes registered in this DAO
const schemes = this.props.data;
const { handleClose } = this.props;
const currentTab = this.state.currentTab;
const arc = getArc();
const addSchemeButtonClass = classNames({
[css.addSchemeButton]: true,
[css.selected]: currentTab === "addScheme",
});
const editSchemeButtonClass = classNames({
[css.selected]: currentTab === "editScheme",
});
const removeSchemeButtonClass = classNames({
[css.selected]: currentTab === "removeScheme",
});
const schemeRegistrarFormClass = classNames({
[css.formWrapper]: true,
[css.addScheme]: currentTab === "addScheme",
[css.removeScheme]: currentTab === "removeScheme",
[css.editScheme]: currentTab === "editScheme",
});
const isAddActive = getSchemeIsActive(this.props.scheme, GetSchemeIsActiveActions.Register);
const isRemoveActive = getSchemeIsActive(this.props.scheme, GetSchemeIsActiveActions.Remove);
const fnDescription = () => (<span>Short description of the proposal.<ul><li>What are you proposing to do?</li><li>Why is it important?</li><li>How much will it cost the DAO?</li><li>When do you plan to deliver the work?</li></ul></span>);
return (
<div className={css.createWrapperWithSidebar}>
<div className={css.sidebar}>
{ isAddActive ?
<button className={addSchemeButtonClass} onClick={this.handleTabClick("addScheme")} data-test-id="tab-AddScheme">
<span></span>
Add Scheme
</button>
: "" }
{ isAddActive ?
<button className={editSchemeButtonClass} onClick={this.handleTabClick("editScheme")} data-test-id="tab-EditScheme">
<span></span>
Edit Scheme
</button>
: "" }
{ isRemoveActive ?
<button className={removeSchemeButtonClass} onClick={this.handleTabClick("removeScheme")} data-test-id="tab-RemoveScheme">
<span></span>
Remove Scheme
</button>
: "" }
</div>
<div className={schemeRegistrarFormClass}>
<Formik
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
initialValues={{
description: "",
otherScheme: "",
parametersHash: "",
permissions: {
registerSchemes: false,
changeConstraints: false,
upgradeController: false,
genericCall: false,
},
title: "",
url: "",
} as IFormValues}
// eslint-disable-next-line react/jsx-no-bind
validate={(values: IFormValues) => {
const errors: any = {};
const require = (name: string) => {
if (!(values as any)[name]) {
errors[name] = "Required";
}
};
require("description");
require("title");
if (values.title.length > 120) {
errors.title = "Title is too long (max 120 characters)";
}
if (currentTab === "addScheme") {
require("schemeToAdd");
require("parametersHash");
} else if (currentTab === "editScheme") {
require("schemeToEdit");
require("parametersHash");
} else if (currentTab === "removeScheme") {
require("schemeToRemove");
}
if (currentTab === "addScheme" && values.otherScheme && !arc.web3.utils.isAddress(values.otherScheme)) {
errors.otherScheme = "Invalid address";
}
const parametersHashPattern = /0x([\da-f]){64}/i;
if (currentTab !== "removeScheme" && values.parametersHash && !parametersHashPattern.test(values.parametersHash)) {
errors.parametersHash = "Invalid parameters hash";
}
if (!isValidUrl(values.url)) {
errors.url = "Invalid URL";
}
return errors;
}}
onSubmit={this.handleSubmit}
// eslint-disable-next-line react/jsx-no-bind
render={({
errors,
touched,
isSubmitting,
setFieldValue,
}: FormikProps<IFormValues>) => {
return (
<Form noValidate>
<label className={css.description}>What to Expect</label>
{ (currentTab === "addScheme") ?
<div className={css.description}>Propose to add a new scheme to the DAO. If this scheme is a universal scheme, you must also supply its param hash configuration.</div> :
(currentTab === "editScheme") ?
<div className={css.description}>Propose to edit a schemes' param hash configuration.</div> :
(currentTab === "removeScheme") ?
<div className={css.description}>Propose to remove a scheme from the DAO.</div> : ""
}
<TrainingTooltip overlay="The title is the header of the proposal card and will be the first visible information about your proposal" placement="right">
<label htmlFor="titleInput">
Title
<ErrorMessage name="title">{(msg) => <span className={css.errorMessage}>{msg}</span>}</ErrorMessage>
<div className={css.requiredMarker}>*</div>
</label>
</TrainingTooltip>
<Field
autoFocus
id="titleInput"
maxLength={120}
placeholder="Summarize your proposal"
name="title"
type="text"
className={touched.title && errors.title ? css.error : null}
/>
<TrainingTooltip overlay={fnDescription} placement="right">
<label htmlFor="descriptionInput">
Description
<div className={css.requiredMarker}>*</div>
<img className={css.infoTooltip} src="/assets/images/Icon/Info.svg"/>
<ErrorMessage name="description">{(msg) => <span className={css.errorMessage}>{msg}</span>}</ErrorMessage>
</label>
</TrainingTooltip>
<Field
component={MarkdownField}
onChange={(value: any) => { setFieldValue("description", value); }}
id="descriptionInput"
placeholder="Describe your proposal in greater detail"
name="description"
className={touched.description && errors.description ? css.error : null}
/>
<TrainingTooltip overlay="Add some tags to give context about your proposal e.g. idea, signal, bounty, research, etc" placement="right">
<label className={css.tagSelectorLabel}>
Tags
</label>
</TrainingTooltip>
<div className={css.tagSelectorContainer}>
<TagsSelector onChange={this.onTagsChange}></TagsSelector>
</div>
<TrainingTooltip overlay="Link to the fully detailed description of your proposal" placement="right">
<label htmlFor="urlInput">
URL
<ErrorMessage name="url">{(msg) => <span className={css.errorMessage}>{msg}</span>}</ErrorMessage>
</label>
</TrainingTooltip>
<Field
id="urlInput"
maxLength={120}
placeholder="Description URL"
name="url"
type="text"
className={touched.url && errors.url ? css.error : null}
/>
<div className={css.addEditSchemeFields}>
<div className={css.addSchemeSelectContainer}>
<label htmlFor="schemeToAddInput">
Scheme
<ErrorMessage name="schemeToAdd">{(msg) => <span className={css.errorMessage}>{msg}</span>}</ErrorMessage>
<div className={css.requiredMarker}>*</div>
</label>
<Field
id="schemeToAddInput"
placeholder="Enter scheme address"
name="schemeToAdd"
/>
</div>
<div className={css.editSchemeSelectContainer}>
<label htmlFor="schemeToEditInput">
Scheme
<ErrorMessage name="schemeToEdit">{(msg) => <span className={css.errorMessage}>{msg}</span>}</ErrorMessage>
<div className={css.requiredMarker}>*</div>
</label>
<Field
id="schemeToEditInput"
name="schemeToEdit"
component="select"
className={css.schemeSelect}
defaultValue=""
>
<option value="">Select a scheme...</option>
{schemes.map((scheme, _i) => {
return <option key={`edit_scheme_${scheme.staticState.address}`} value={scheme.staticState.address}>{schemeNameAndAddress(scheme.staticState.address)}</option>;
})}
</Field>
</div>
<div className={css.parametersHash}>
<label htmlFor="parametersHashInput">
Parameters Hash
<ErrorMessage name="parametersHash">{(msg) => <span className={css.errorMessage}>{msg}</span>}</ErrorMessage>
<div className={css.requiredMarker}>*</div>
</label>
<Field
id="parametersHashInput"
placeholder="e.g. 0x0000000000000000000000000000000000000000000000000000000000001234"
name="parametersHash"
className={touched.parametersHash && errors.parametersHash ? css.error : null}
/>
</div>
<div className={css.permissions}>
<div className={css.permissionsLabel}>
Permissions
</div>
<div className={css.permissionCheckbox}>
<Field id="registerOtherSchemesInput" type="checkbox" name="permissions.registerSchemes" />
<label htmlFor="registerOtherSchemesInput">
Register other schemes
</label>
</div>
<div className={css.permissionCheckbox}>
<Field id="changeConstraintsInput" type="checkbox" name="permissions.changeConstraints" />
<label htmlFor="changeConstraintsInput">
Add/remove global constraints
</label>
</div>
<div className={css.permissionCheckbox}>
<Field id="upgradeControllerInput" type="checkbox" name="permissions.upgradeController" />
<label htmlFor="upgradeControllerInput">
Upgrade the controller
</label>
</div>
<div className={css.permissionCheckbox}>
<Field id="genericCallInput" type="checkbox" name="permissions.genericCall" />
<label htmlFor="genericCallInput">
Call genericCall on behalf of
</label>
</div>
<div className={css.permissionCheckbox}>
<Field id="mintBurnReputation" type="checkbox" name="mintBurnReputation" disabled="disabled" checked="checked" />
<label htmlFor="mintBurnReputation">
Mint or burn reputation
</label>
</div>
</div>
</div>
<div className={css.removeSchemeFields}>
<div className={css.removeSchemeSelectContainer}>
<label htmlFor="schemeToRemoveInput">
Scheme
<ErrorMessage name="schemeToRemove">{(msg) => <span className={css.errorMessage}>{msg}</span>}</ErrorMessage>
<div className={css.requiredMarker}>*</div>
</label>
<Field
id="schemeToRemoveInput"
name="schemeToRemove"
component="select"
className={css.schemeSelect}
defaultValue=""
>
<option value="">Select a scheme...</option>
{schemes.map((scheme, _i) => {
return <option key={`remove_scheme_${scheme.staticState.address}`} value={scheme.staticState.address}>{schemeNameAndAddress(scheme.staticState.address)}</option>;
})}
</Field>
</div>
</div>
<div className={css.createProposalActions}>
<button className={css.exitProposalCreation} type="button" onClick={handleClose}>
Cancel
</button>
<TrainingTooltip overlay="Once the proposal is submitted it cannot be edited or deleted" placement="top">
<button className={css.submitProposal} type="submit" disabled={isSubmitting}>
Submit proposal
</button>
</TrainingTooltip>
</div>
</Form>
);
}}
/>
</div>
</div>
);
}
}
const SubscribedCreateSchemeRegistrarProposal = withSubscription({
wrappedComponent: CreateSchemeRegistrarProposal,
loadingComponent: <div className={css.loading}><Loading/></div>,
errorComponent: null,
checkForUpdate: ["daoAvatarAddress"],
createObservable: (props: IExternalProps) => {
const arc = getArc();
return arc.dao(props.daoAvatarAddress).schemes();
},
});
export default connect(null, mapDispatchToProps)(SubscribedCreateSchemeRegistrarProposal);