-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathCreateEventController.jsx
More file actions
162 lines (148 loc) · 4.71 KB
/
Copy pathCreateEventController.jsx
File metadata and controls
162 lines (148 loc) · 4.71 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
// @flow
import React from "react";
import Section from "../enums/Section.js";
import EventOverviewForm from "../componentsBySection/CreateEvent/EventOverviewForm.jsx";
import EventPreviewForm from "../componentsBySection/CreateEvent/EventPreviewForm.jsx";
import EventDescriptionForm from "../componentsBySection/CreateEvent/EventDescriptionForm.jsx";
import EventAPIUtils, { EventData } from "../utils/EventAPIUtils.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 CurrentUser from "../utils/CurrentUser";
import LogInController from "./LogInController.jsx";
import VerifyEmailBlurb from "../common/notification/VerifyEmailBlurb.jsx";
import type { APIResponse } from "../utils/api.js";
type State = {|
eventId: ?number,
event: ?EventData,
steps: $ReadOnlyArray<FormWorkflowStepConfig>,
|};
/**
* Encapsulates form for creating Events
*/
class CreateEventController extends React.PureComponent<{||}, State> {
constructor(props: {||}): void {
super(props);
const eventId: number = url.argument("id");
this.onNextPageSuccess = this.onNextPageSuccess.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.onFinalSubmitSuccess = this.onFinalSubmitSuccess.bind(this);
this.state = {
eventId: eventId,
steps: [
{
header: "Let's get started!",
subHeader: "",
onSubmit: this.onSubmit,
onSubmitSuccess: this.onNextPageSuccess,
formComponent: EventOverviewForm,
},
{
header: "Let others know what your Event is about...",
subHeader: "You can always change details about your Event later.",
onSubmit: this.onSubmit,
onSubmitSuccess: this.onNextPageSuccess,
formComponent: EventDescriptionForm,
},
{
header: "Ready to publish your Event?",
subHeader:
"Congratulations! You have successfully created a tech-for-good Event.",
onSubmit: this.onSubmit,
onSubmitSuccess: this.onFinalSubmitSuccess,
formComponent: EventPreviewForm,
},
],
};
}
componentDidMount(): void {
if (this.state.eventId) {
EventAPIUtils.fetchEventDetails(
this.state.eventId,
this.loadEventDetails.bind(this),
this.handleLoadEventError.bind(this)
);
}
}
updatePageUrl() {
if (this.state.eventId && !url.argument("id")) {
url.updateArgs({ id: this.state.eventId });
}
utils.navigateToTopOfPage();
}
loadEventDetails(event: EventData): void {
if (!EventAPIUtils.isOwner(event) && !CurrentUser.isStaff()) {
// TODO: Handle someone other than owner/admin
} else {
this.setState({
event: event,
eventIsLoading: false,
});
}
}
handleLoadEventError(error: APIError): void {
this.setState({
error: "Failed to load Event information",
});
}
onSubmit(
event: SyntheticEvent<HTMLFormElement>,
formRef: HTMLFormElement,
onSubmitSuccess: (EventData, () => void) => void
): void {
const formSubmitUrl: string =
this.state.event && this.state.event.event_id
? "/api/events/edit/" + this.state.event.event_id + "/"
: "/api/events/create/";
api.postForm(
formSubmitUrl,
formRef,
(response: APIResponse) => onSubmitSuccess(JSON.parse(response)),
response => null /* TODO: Report error to user */
);
}
onNextPageSuccess(event: EventData): void {
this.setState({
event: event,
eventId: event.event_id,
});
this.updatePageUrl();
}
onFinalSubmitSuccess(event: EventData): void {
if(!event.event_approved){
window.location.href = url.section(Section.AboutEvent, {
id: event.event_id,
eventAwaitingApproval: url.encodeNameForUrlPassing(event.event_name),
});
} else {
window.location.href = url.section(Section.AboutEvent, null);
}
}
render(): React$Node {
return (
<React.Fragment>
<div className="form-body">
{!CurrentUser.isLoggedIn() ? (
<LogInController prevPage={Section.CreateEvent} />
) : (
<React.Fragment>
{CurrentUser.isEmailVerified() ? (
<FormWorkflow
steps={this.state.steps}
isLoading={this.state.eventId && !this.state.event}
formFields={this.state.event}
/>
) : (
<VerifyEmailBlurb />
)}
</React.Fragment>
)}
</div>
</React.Fragment>
);
}
}
export default CreateEventController;