-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathCreateProjectController.jsx
More file actions
229 lines (216 loc) · 7.7 KB
/
Copy pathCreateProjectController.jsx
File metadata and controls
229 lines (216 loc) · 7.7 KB
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
// @flow
import React from "react";
import _ from "lodash";
import CurrentUser from "../../components/utils/CurrentUser.js";
import metrics from "../utils/metrics.js";
import LogInController from "./LogInController.jsx";
import Section from "../enums/Section.js";
import ProjectOverviewForm from "../componentsBySection/CreateProject/ProjectOverviewForm.jsx";
import ProjectInfoForm from "../componentsBySection/CreateProject/ProjectInfoForm.jsx";
import ProjectPreviewForm from "../componentsBySection/CreateProject/ProjectPreviewForm.jsx";
import ProjectDescriptionForm from "../componentsBySection/CreateProject/ProjectDescriptionForm.jsx";
import ProjectPositionsForm from "../componentsBySection/CreateProject/ProjectPositionsForm.jsx";
import ProjectResourcesForm from "../componentsBySection/CreateProject/ProjectResourcesForm.jsx";
import ProjectAPIUtils, {
ProjectDetailsAPIData,
} from "../utils/ProjectAPIUtils.js";
import api from "../utils/api.js";
import url from "../utils/url.js";
import utils from "../utils/utils.js";
import FormWorkflow, {
FormWorkflowStepConfig,
} from "../forms/FormWorkflow.jsx";
import VerifyEmailBlurb from "../common/notification/VerifyEmailBlurb.jsx";
import type { Dictionary } from "../types/Generics.jsx";
import type { APIResponse } from "../utils/api.js";
type State = {|
projectId: ?number,
project: ?ProjectDetailsAPIData,
steps: $ReadOnlyArray<FormWorkflowStepConfig>,
startStep: ?number,
|};
/**
* Encapsulates form for creating projects
*/
class CreateProjectController extends React.PureComponent<{||}, State> {
constructor(props: {||}): void {
super(props);
const projectId: number = url.argument("id");
this.onNextPageSuccess = this.onNextPageSuccess.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.onFinalSubmitSuccess = this.onFinalSubmitSuccess.bind(this);
this.state = {
projectId: projectId,
startStep: 0,
steps: [
{
header: "Let's get started!",
subHeader: "Tell us how you want to create a better world.",
onSubmit: this.onSubmit,
onSubmitSuccess: this.onNextPageSuccess,
formComponent: ProjectOverviewForm,
},
{
header: "Let others know what your project is about...",
subHeader: "You can always change details about your project later.",
onSubmit: this.onSubmit,
onSubmitSuccess: this.onNextPageSuccess,
formComponent: ProjectInfoForm,
},
{
header: "Let others know what your project is about...",
subHeader: "You can always change details about your project later.",
onSubmit: this.onSubmit,
onSubmitSuccess: this.onNextPageSuccess,
formComponent: ProjectDescriptionForm,
},
{
header: "What resources would you like to share?",
subHeader:
"At DemocracyLab, we're all about transparency. Share your project's internal collaboration resources and social media to help volunteers understand your goals and processes.",
onSubmit: this.onSubmit,
onSubmitSuccess: this.onNextPageSuccess,
formComponent: ProjectResourcesForm,
},
{
header: "What type of volunteers does your project need?",
subHeader:
"You can always change the type of help your project needs later.",
onSubmit: this.onSubmit,
onSubmitSuccess: this.onNextPageSuccess,
formComponent: ProjectPositionsForm,
},
{
header: "Ready to submit your project?",
subHeader:
'Please review your project’s details and click "Submit" below when you’re ready.',
submitButtonText: "Submit",
onSubmit: this.onSubmit,
onSubmitSuccess: this.onFinalSubmitSuccess,
formComponent: ProjectPreviewForm,
},
],
};
}
componentDidMount(): void {
if (this.state.projectId) {
ProjectAPIUtils.fetchProjectDetails(
this.state.projectId,
true,
this.loadProjectDetails.bind(this),
this.handleLoadProjectError.bind(this)
);
}
if (CurrentUser.isLoggedIn() && CurrentUser.isEmailVerified()) {
// Only fire event on initial page when the project is not yet created
if (!url.argument("id")) {
metrics.logProjectClickCreate(CurrentUser.userID());
}
}
}
updatePageUrl() {
if (this.state.projectId && !url.argument("id")) {
url.updateArgs({ id: this.state.projectId });
}
utils.navigateToTopOfPage();
}
loadProjectDetails(project: ProjectDetailsAPIData): void {
if (!CurrentUser.isCoOwnerOrOwner(project) && !CurrentUser.isStaff()) {
// TODO: Handle someone other than owner
} else {
if (project.project_created) {
const lastStep: FormWorkflowStepConfig = _.last(this.state.steps);
lastStep.header = "Ready to save your edits?";
lastStep.subHeader =
'When everything looks good, click "Update Project" below.';
lastStep.submitButtonText = "Update Project";
}
this.setState({
project: project,
projectIsLoading: false,
startStep: url.argument("step") || 1,
steps: _.clone(this.state.steps),
});
}
}
handleLoadProjectError(error: APIError): void {
this.setState({
error: "Failed to load project information",
});
}
onSubmit(
event: SyntheticEvent<HTMLFormElement>,
formRef: HTMLFormElement,
onSubmitSuccess: ProjectDetailsAPIData => void
): void {
const formSubmitUrl: string =
this.state.project && this.state.project.project_id
? "/api/projects/edit/" + this.state.project.project_id + "/"
: "/api/projects/create/";
api.postForm(
formSubmitUrl,
formRef,
(response: APIResponse) => onSubmitSuccess(JSON.parse(response)),
response => null /* TODO: Report error to user */
);
}
onNextPageSuccess(project: ProjectDetailsAPIData): void {
this.setState(
{
project: project,
projectId: project.project_id,
},
this.updatePageUrl.bind(this)
);
}
onFinalSubmitSuccess(project: ProjectDetailsAPIData): void {
this.setState({
project: project,
projectId: project.project_id,
});
metrics.logProjectCreated(CurrentUser.userID());
// TODO: Fix bug with switching to this section without page reload
let urlArgs: Dictionary<string> = {
projectAwaitingApproval: url.encodeNameForUrlPassing(
project.project_name
),
};
if (project.event_created_from) {
// Show modal on next page for prompting to create event project
urlArgs = Object.assign(urlArgs, {
fromProjectId: project.project_id,
fromEventId: project.event_created_from,
});
}
if(!project.project_approved){
window.location.href = url.section(Section.MyProjects, urlArgs);
} else {
window.location.href = url.section(Section.MyProjects, null);
}
}
render(): React$Node {
return (
<React.Fragment>
<div className="form-body">
{!CurrentUser.isLoggedIn() ? (
<LogInController prevPage={Section.CreateProject} />
) : (
<React.Fragment>
{CurrentUser.isEmailVerified() ? (
<FormWorkflow
steps={this.state.steps}
startStep={this.state.startStep}
isLoading={this.state.projectId && !this.state.project}
formFields={this.state.project}
/>
) : (
<VerifyEmailBlurb />
)}
</React.Fragment>
)}
</div>
</React.Fragment>
);
}
}
export default CreateProjectController;