forked from opencollective/opencollective-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectiveContactForm.js
161 lines (152 loc) · 5.04 KB
/
CollectiveContactForm.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
import React from 'react';
import PropTypes from 'prop-types';
import { gql, useMutation } from '@apollo/client';
import { get } from 'lodash';
import { FormattedMessage } from 'react-intl';
import { getErrorFromGraphqlException } from '../lib/errors';
import Container from './Container';
import { Box } from './Grid';
import MessageBox from './MessageBox';
import StyledButton from './StyledButton';
import StyledInput from './StyledInput';
import StyledInputField from './StyledInputField';
import StyledTextarea from './StyledTextarea';
import { H2, P, Span } from './Text';
import { TOAST_TYPE, useToasts } from './ToastProvider';
const sendMessageMutation = gql`
mutation SendMessage($collectiveId: Int!, $message: String!, $subject: String) {
sendMessageToCollective(collectiveId: $collectiveId, message: $message, subject: $subject) {
success
}
}
`;
const CollectiveContactForm = ({ collective, isModal = false, onClose }) => {
const [subject, setSubject] = React.useState('');
const [message, setMessage] = React.useState('');
const [error, setError] = React.useState(null);
const [submit, { data, loading }] = useMutation(sendMessageMutation);
const { addToast } = useToasts();
if (get(data, 'sendMessageToCollective.success') && !isModal) {
return (
<MessageBox type="success" withIcon maxWidth={400} m="32px auto">
<FormattedMessage id="MessageSent" defaultMessage="Message sent" />
</MessageBox>
);
}
const messageLabel = (
<Span fontWeight={700}>
<FormattedMessage id="Contact.Message" defaultMessage="Message" />
</Span>
);
const subjectLabel = (
<FormattedMessage
id="OptionalFieldLabel"
defaultMessage="{field} (optional)"
values={{
field: (
<Span fontWeight={700}>
<FormattedMessage id="Contact.Subject" defaultMessage="Subject" />
</Span>
),
}}
/>
);
return (
<Box flexDirection="column" alignItems={['center', 'flex-start']} maxWidth={1160} m="0 auto">
<H2 mb={2} fontSize={isModal ? '28px' : '40px'}>
<FormattedMessage
id="ContactCollective"
defaultMessage="Contact {collective}"
values={{ collective: collective.name }}
/>
</H2>
<P mb={4}>
<FormattedMessage
id="CollectiveContactForm.Disclaimer"
defaultMessage="Your email address will be shared with the admins who will receive this message."
/>
</P>
<StyledInputField label={subjectLabel} htmlFor="subject" mb={4} width="100%">
{inputProps => (
<StyledInput
{...inputProps}
width="100%"
maxWidth={500}
maxLength={60}
onChange={e => setSubject(e.target.value)}
data-cy="subject"
/>
)}
</StyledInputField>
<StyledInputField label={messageLabel} htmlFor="message" width="100%" maxWidth={800}>
{inputProps => (
<StyledTextarea
{...inputProps}
width="100%"
minHeight={200}
onChange={e => setMessage(e.target.value)}
minLength={10}
maxLength={4000}
value={message}
showCount
data-cy="message"
/>
)}
</StyledInputField>
{error && (
<MessageBox type="error" withIcon mt={3}>
{error.message}
</MessageBox>
)}
<Container mt={2}>
<FormattedMessage defaultMessage="Message needs to be at least 10 characters long" />
</Container>
{isModal && <hr />}
<Box textAlign={isModal ? 'right' : ''}>
<StyledButton
mt={isModal ? 0 : 4}
minWidth={200}
buttonStyle="primary"
disabled={message.length < 10}
loading={loading}
data-cy="submit"
onClick={async () => {
try {
setError(null);
await submit({
variables: {
collectiveId: typeof collective.id === 'string' ? collective.legacyId : collective.id,
subject,
message,
},
});
if (isModal) {
addToast({
type: TOAST_TYPE.SUCCESS,
message: <FormattedMessage id="MessageSent" defaultMessage="Message sent" />,
});
onClose();
}
} catch (e) {
setError(getErrorFromGraphqlException(e));
}
}}
>
<FormattedMessage defaultMessage="Contact Collective" />
</StyledButton>
</Box>
</Box>
);
};
CollectiveContactForm.propTypes = {
collective: PropTypes.shape({
id: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
legacyId: PropTypes.number,
name: PropTypes.string.isRequired,
}),
/* Defines whether this form is displayed as a modal */
isModal: PropTypes.bool,
/* Specifies close behaviour is this form is part of a modal */
onClose: PropTypes.func,
};
export default CollectiveContactForm;