-
Notifications
You must be signed in to change notification settings - Fork 22
/
SubmitChallenge.tsx
214 lines (192 loc) · 6.57 KB
/
SubmitChallenge.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
import * as React from "react";
import { connect } from "react-redux";
import { EthAddress, TwoStepEthTransaction } from "@joincivil/core";
import {
LoadingIndicator,
ModalHeading,
ModalContent,
ModalOrderedList,
ModalListItem,
ModalListItemTypes,
SubmitChallengeStatement as SubmitChallengeStatementComponent,
SubmitChallengeStatementProps,
} from "@joincivil/components";
import { getFormattedParameterValue, Parameters } from "@joincivil/utils";
import { getCivil } from "../../helpers/civilInstance";
import { approveForChallenge, challengeListing } from "../../apis/civilTCR";
import { State } from "../../reducers";
export interface SubmitChallengePageProps {
match: any;
history?: any;
}
interface SubmitChallengeProps {
history?: any;
listingAddress: EthAddress;
listingURI: string;
governanceGuideURI: string;
}
interface SubmitChallengeReduxProps {
newsroomName: string;
constitutionURI: string;
minDeposit: string;
commitStageLen: string;
revealStageLen: string;
}
interface SubmitChallengeState {
challengeStatementSummaryValue?: string;
challengeStatementCiteConstitutionValue?: any;
challengeStatementDetailsValue?: any;
}
enum ModalContentEventNames {
IN_PROGRESS_APPROVE_FOR_CHALLENGE = "IN_PROGRESS:APPROVE_FOR_CHALLENGE",
IN_PROGRESS_SUBMIT_CHALLENGE = "IN_PROGRESS:SUBMIT_CHALLENGE",
}
class SubmitChallengeComponent extends React.Component<
SubmitChallengeProps & SubmitChallengeReduxProps,
SubmitChallengeState
> {
public render(): JSX.Element {
const approveForChallengeProgressModal = <ApproveForChallengeProgressModal />;
const submitChallengeProgressModal = <SubmitChallengeProgressModal />;
const modalContentComponents = {
[ModalContentEventNames.IN_PROGRESS_APPROVE_FOR_CHALLENGE]: approveForChallengeProgressModal,
[ModalContentEventNames.IN_PROGRESS_SUBMIT_CHALLENGE]: submitChallengeProgressModal,
};
const transactions = [
{
transaction: approveForChallenge,
progressEventName: ModalContentEventNames.IN_PROGRESS_APPROVE_FOR_CHALLENGE,
},
{
transaction: this.challenge,
progressEventName: ModalContentEventNames.IN_PROGRESS_SUBMIT_CHALLENGE,
},
];
const {
listingURI,
newsroomName,
constitutionURI,
governanceGuideURI,
minDeposit,
commitStageLen,
revealStageLen,
} = this.props;
const props: SubmitChallengeStatementProps = {
listingURI,
newsroomName,
constitutionURI,
governanceGuideURI,
minDeposit,
commitStageLen,
revealStageLen,
updateStatementValue: this.updateStatement,
transactions,
modalContentComponents,
postExecuteTransactions: this.onSubmitChallengeSuccess,
};
return <SubmitChallengeStatementComponent {...props} />;
}
private updateStatement = (key: string, value: any): void => {
const stateKey = `challengeStatement${key.charAt(0).toUpperCase()}${key.substring(1)}Value`;
this.setState(() => ({ [stateKey]: value }));
};
private onSubmitChallengeSuccess = (): void => {
this.props.history.push("/listing/" + this.props.listingAddress);
};
// Transactions
private challenge = async (): Promise<TwoStepEthTransaction<any>> => {
const {
challengeStatementSummaryValue,
challengeStatementCiteConstitutionValue,
challengeStatementDetailsValue,
} = this.state;
const jsonToSave = {
summary: challengeStatementSummaryValue,
citeConstitution: challengeStatementCiteConstitutionValue.toString("html"),
details: challengeStatementDetailsValue.toString("html"),
};
return challengeListing(this.props.listingAddress, JSON.stringify(jsonToSave));
};
}
const ApproveForChallengeProgressModal: React.SFC = props => {
return (
<>
<LoadingIndicator height={100} />
<ModalHeading>Transactions in progress</ModalHeading>
<ModalOrderedList>
<ModalListItem type={ModalListItemTypes.STRONG}>Approving For Challenge</ModalListItem>
<ModalListItem type={ModalListItemTypes.FADED}>Submitting Challenge</ModalListItem>
</ModalOrderedList>
<ModalContent>This can take 1-3 minutes. Please don't close the tab.</ModalContent>
<ModalContent>How about taking a little breather and standing for a bit? Maybe even stretching?</ModalContent>
</>
);
};
const SubmitChallengeProgressModal: React.SFC = props => {
return (
<>
<LoadingIndicator height={100} />
<ModalHeading>Transactions in progress</ModalHeading>
<ModalOrderedList>
<ModalListItem>Approving For Challenge</ModalListItem>
<ModalListItem type={ModalListItemTypes.STRONG}>Submitting Challenge</ModalListItem>
</ModalOrderedList>
<ModalContent>This can take 1-3 minutes. Please don't close the tab.</ModalContent>
<ModalContent>How about taking a little breather and standing for a bit? Maybe even stretching?</ModalContent>
</>
);
};
const mapStateToProps = (
state: State,
ownProps: SubmitChallengeProps,
): SubmitChallengeProps & SubmitChallengeReduxProps => {
const { newsrooms } = state;
const newsroom = newsrooms.get(ownProps.listingAddress);
let newsroomName = "";
if (newsroom) {
newsroomName = newsroom.wrapper.data.name;
}
const { parameters, constitution } = state.networkDependent;
const constitutionURI = constitution.get("uri") || "#";
let minDeposit = "";
let commitStageLen = "";
let revealStageLen = "";
if (parameters && Object.keys(parameters).length) {
const civil = getCivil();
minDeposit = getFormattedParameterValue(
Parameters.minDeposit,
civil.toBigNumber(parameters[Parameters.minDeposit]),
);
commitStageLen = getFormattedParameterValue(
Parameters.commitStageLen,
civil.toBigNumber(parameters[Parameters.commitStageLen]),
);
revealStageLen = getFormattedParameterValue(
Parameters.revealStageLen,
civil.toBigNumber(parameters[Parameters.revealStageLen]),
);
}
return {
newsroomName,
constitutionURI,
minDeposit,
commitStageLen,
revealStageLen,
...ownProps,
};
};
const SubmitChallenge = connect(mapStateToProps)(SubmitChallengeComponent);
const SubmitChallengePage: React.SFC<SubmitChallengePageProps> = props => {
const listingAddress = props.match.params.listing;
const listingURI = `/listing/${listingAddress}`;
const governanceGuideURI = "https://civil.co";
return (
<SubmitChallenge
listingAddress={listingAddress}
listingURI={listingURI}
governanceGuideURI={governanceGuideURI}
history={props.history}
/>
);
};
export default SubmitChallengePage;