forked from opencollective/opencollective-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateEvent.js
166 lines (151 loc) · 5.91 KB
/
CreateEvent.js
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
import React from 'react';
import PropTypes from 'prop-types';
import { withRouter } from 'next/router';
import { FormattedMessage } from 'react-intl';
import { FEATURES, isFeatureEnabled } from '../lib/allowed-features';
import { convertDateToApiUtc } from '../lib/date-utils';
import dayjs from '../lib/dayjs';
import { getErrorFromGraphqlException } from '../lib/errors';
import { addCreateCollectiveMutation } from '../lib/graphql/mutations';
import { getCollectivePageRoute } from '../lib/url-helpers';
import Body from './Body';
import CollectiveNavbar from './collective-navbar';
import Container from './Container';
import CreateEventForm from './CreateEventForm';
import Footer from './Footer';
import Header from './Header';
import { getI18nLink } from './I18nFormatters';
import Link from './Link';
import MessageBox from './MessageBox';
import StyledButton from './StyledButton';
import { withUser } from './UserProvider';
class CreateEvent extends React.Component {
static propTypes = {
parentCollective: PropTypes.object,
createCollective: PropTypes.func,
LoggedInUser: PropTypes.object, // from withUser
refetchLoggedInUser: PropTypes.func.isRequired, // from withUser
router: PropTypes.object,
};
constructor(props) {
super(props);
const timezone = dayjs.tz.guess();
const startsAt = dayjs().tz(timezone).set('hour', 19).set('minute', 0).set('second', 0);
const endsAt = dayjs().tz(timezone).set('hour', 20).set('minute', 0).set('second', 0);
this.state = {
event: {
parentCollective: props.parentCollective,
timezone, // "Europe/Brussels", // "America/New_York"
startsAt: convertDateToApiUtc(startsAt, timezone),
endsAt: convertDateToApiUtc(endsAt, timezone),
},
result: {},
};
this.createEvent = this.createEvent.bind(this);
this.handleTemplateChange = this.handleTemplateChange.bind(this);
this.error = this.error.bind(this);
this.resetError = this.resetError.bind(this);
}
error(msg) {
this.setState({ result: { error: msg } });
}
resetError() {
this.error();
}
async createEvent(EventInputType) {
const { parentCollective } = this.props;
this.setState({ status: 'loading' });
EventInputType.type = 'EVENT';
EventInputType.ParentCollectiveId = parentCollective.id;
EventInputType.tiers = EventInputType.tiers.filter(tier => tier.name);
EventInputType.settings = { disableCustomContributions: true };
try {
const res = await this.props.createCollective(EventInputType);
const event = res.data.createCollective;
this.setState({
status: 'idle',
result: { success: `Event created successfully.` },
});
await this.props.refetchLoggedInUser();
await this.props.router.push({
pathname: `/${parentCollective.slug}/events/${event.slug}`,
query: {
status: 'eventCreated',
},
});
window.scrollTo(0, 0);
} catch (err) {
const errorMsg = getErrorFromGraphqlException(err).message;
this.setState({
status: 'idle',
result: { error: errorMsg },
});
throw new Error(errorMsg);
}
}
async handleTemplateChange(event) {
delete event.id;
delete event.slug;
this.setState({ event, tiers: event.tiers });
}
render() {
const { parentCollective, LoggedInUser } = this.props;
const isAdmin = LoggedInUser && LoggedInUser.canEditCollective(parentCollective);
const collective = parentCollective || {};
const title = `Create a New ${collective.name} Event`;
return (
<div className="CreateEvent">
<Header title={title} className={this.state.status} LoggedInUser={this.props.LoggedInUser} />
<Body>
<CollectiveNavbar collective={collective} isAdmin={isAdmin} />
<div className="content">
{!isAdmin ? (
<Container margin="0 auto" textAlign="center">
<p>
<FormattedMessage
id="events.create.login"
defaultMessage="You need to be logged as a team member of this Collective to create an event."
/>
</p>
<p>
<Link href={`/signin?next=/${collective.slug}/events/new`}>
<StyledButton buttonStyle="primary">
<FormattedMessage id="signIn" defaultMessage="Sign In" />
</StyledButton>
</Link>
</p>
</Container>
) : collective.isFrozen ? (
<MessageBox withIcon type="warning" my={5}>
<FormattedMessage defaultMessage="This account is currently frozen and cannot be used to create events." />{' '}
{isFeatureEnabled(collective.host, FEATURES.CONTACT_FORM) && (
<FormattedMessage
defaultMessage="Please <ContactLink>contact</ContactLink> your fiscal host for more details."
values={{
ContactLink: getI18nLink({ href: `${getCollectivePageRoute(collective.host)}/contact` }),
}}
/>
)}
</MessageBox>
) : (
<div>
<CreateEventForm
event={this.state.event}
onSubmit={this.createEvent}
onChange={this.resetError}
loading={this.state.status === 'loading' || this.state.result.success}
/>
<Container textAlign="center" marginBottom="5rem">
<Container style={{ color: 'green' }}>{this.state.result.success}</Container>
<Container style={{ color: 'red' }}>{this.state.result.error}</Container>
</Container>
</div>
)}
</div>
</Body>
<Footer />
</div>
);
}
}
export default withUser(addCreateCollectiveMutation(withRouter(CreateEvent)));