forked from opencollective/opencollective-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateUpdate.js
241 lines (219 loc) · 7.62 KB
/
createUpdate.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
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
import React from 'react';
import PropTypes from 'prop-types';
import { graphql } from '@apollo/client/react/hoc';
import { ArrowBack } from '@styled-icons/boxicons-regular';
import { withRouter } from 'next/router';
import { defineMessages, FormattedMessage, injectIntl } from 'react-intl';
import styled from 'styled-components';
import { API_V2_CONTEXT, gqlV2 } from '../lib/graphql/helpers';
import { addCollectiveNavbarData } from '../lib/graphql/queries';
import { compose } from '../lib/utils';
import Body from '../components/Body';
import CollectiveNavbar from '../components/collective-navbar';
import { getUpdatesSectionQueryVariables, updatesSectionQuery } from '../components/collective-page/sections/Updates';
import Container from '../components/Container';
import EditUpdateForm from '../components/EditUpdateForm';
import ErrorPage from '../components/ErrorPage';
import Footer from '../components/Footer';
import { Box, Flex } from '../components/Grid';
import Header from '../components/Header';
import Link from '../components/Link';
import MessageBox from '../components/MessageBox';
import StyledButton from '../components/StyledButton';
import StyledButtonSet from '../components/StyledButtonSet';
import { H1 } from '../components/Text';
import { getUpdatesVariables, UPDATES_PER_PAGE, updatesQuery } from '../components/UpdatesWithData';
import { withUser } from '../components/UserProvider';
const BackButtonWrapper = styled(Container)`
position: relative;
color: #71757a;
margin-right: 62px;
margin-left: 20px;
@media (max-width: 600px) {
margin-left: 0;
}
`;
const CreateUpdateWrapper = styled(Flex)`
@media (max-width: 600px) {
flex-direction: column;
}
`;
const UPDATE_TYPE_MSGS = defineMessages({
normal: {
id: 'update.type.normal',
defaultMessage: 'Normal Update',
},
changelog: { id: 'update.type.changelog', defaultMessage: 'Changelog Entry' },
});
const UPDATE_TYPES = Object.keys(UPDATE_TYPE_MSGS);
class CreateUpdatePage extends React.Component {
static getInitialProps({ query: { collectiveSlug, action } }) {
return { slug: collectiveSlug, action };
}
static propTypes = {
slug: PropTypes.string, // for addCollectiveNavbarData
action: PropTypes.string, // not used atm, not clear where it's coming from, not in the route
createUpdate: PropTypes.func, // from addMutation/createUpdateQuery
data: PropTypes.shape({
account: PropTypes.object,
}).isRequired, // from withData
LoggedInUser: PropTypes.object,
router: PropTypes.object,
intl: PropTypes.object.isRequired,
};
constructor(props) {
super(props);
this.state = {
update: {},
status: '',
error: '',
updateType: props.data?.account?.slug === 'opencollective' ? UPDATE_TYPES[1] : UPDATE_TYPES[0],
};
}
createUpdate = async update => {
const { data } = this.props;
const { account } = data;
this.setState({ error: '', status: 'submitting' });
try {
update.account = { id: account.id };
update.isChangelog = this.isChangelog();
if (update.isChangelog) {
update.isPrivate = false;
}
const res = await this.props.createUpdate({
variables: { update },
refetchQueries: [
{
query: updatesQuery,
context: API_V2_CONTEXT,
variables: getUpdatesVariables(this.props.slug, UPDATES_PER_PAGE, true),
},
{ query: updatesSectionQuery, variables: getUpdatesSectionQueryVariables(this.props.slug, true) },
],
});
this.setState({ isModified: false });
return this.props.router.push(`/${account.slug}/updates/${res.data.createUpdate.slug}`);
} catch (e) {
this.setState({ status: 'error', error: e.message });
}
};
handleChange = (attr, value) => {
const update = this.state.update;
update[attr] = value;
this.setState({ update, isModified: true });
};
isChangelog = () => {
return this.state.updateType === UPDATE_TYPES[1];
};
render() {
const { data, LoggedInUser, intl } = this.props;
if (!data.account) {
return <ErrorPage data={data} />;
}
const collective = data.account;
const isAdmin = LoggedInUser && LoggedInUser.canEditCollective(collective);
return (
<div>
<Header collective={collective} LoggedInUser={LoggedInUser} />
<Body>
<CollectiveNavbar collective={collective} isAdmin={isAdmin} />
<CreateUpdateWrapper className="content" mt={4} alignItems="baseline">
<BackButtonWrapper>
<Link href={`/${collective.slug}/updates`}>
<Container display="flex" color="#71757A" fontSize="14px" alignItems="center">
<ArrowBack size={18} />
<Box as="span" mx={2}>
Back
</Box>
</Container>
</Link>
</BackButtonWrapper>
<Container width={1} maxWidth={650}>
{!isAdmin && (
<div className="login">
<p>
<FormattedMessage
id="updates.create.login"
defaultMessage="You need to be logged in as an admin of this collective to be able to create an update."
/>
</p>
<p>
<StyledButton buttonStyle="primary" href={`/signin?next=/${collective.slug}/updates/new`}>
<FormattedMessage id="signIn" defaultMessage="Sign In" />
</StyledButton>
</p>
</div>
)}
{isAdmin && (
<Container my={3}>
<H1 textAlign="left" fontSize="34px">
<FormattedMessage id="updates.new.title" defaultMessage="New update" />
</H1>
</Container>
)}
{collective.slug === 'opencollective' && isAdmin && (
<StyledButtonSet
size="medium"
items={UPDATE_TYPES}
selected={this.state.updateType}
onChange={value => this.setState({ updateType: value })}
>
{({ item }) => intl.formatMessage(UPDATE_TYPE_MSGS[item])}
</StyledButtonSet>
)}
{isAdmin && (
<EditUpdateForm collective={collective} onSubmit={this.createUpdate} isChangelog={this.isChangelog()} />
)}
{this.state.status === 'error' && (
<MessageBox type="error" withIcon>
<FormattedMessage
id="updates.new.error"
defaultMessage="Update failed: {err}"
values={{ err: this.state.error }}
/>
</MessageBox>
)}
</Container>
</CreateUpdateWrapper>
</Body>
<Footer />
</div>
);
}
}
const createUpdateMutation = gqlV2/* GraphQL */ `
mutation CreateUpdate($update: UpdateCreateInput!) {
createUpdate(update: $update) {
id
slug
title
summary
html
createdAt
publishedAt
updatedAt
tags
isPrivate
isChangelog
makePublicOn
account {
id
slug
}
fromAccount {
id
type
name
slug
}
}
}
`;
const addCreateUpdateMutation = graphql(createUpdateMutation, {
name: 'createUpdate',
options: {
context: API_V2_CONTEXT,
},
});
const addGraphql = compose(addCollectiveNavbarData, addCreateUpdateMutation);
export default withUser(addGraphql(withRouter(injectIntl(CreateUpdatePage))));