-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathpostCommentForm.js
More file actions
294 lines (284 loc) · 8.19 KB
/
postCommentForm.js
File metadata and controls
294 lines (284 loc) · 8.19 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import React, { Component, PropTypes } from 'react'
import { reduxForm } from 'redux-form'
import Textarea from 'react-textarea-autosize'
import { Motion, spring, presets } from 'react-motion'
import Measure from 'react-measure'
import Spinner from 'react-spinner'
import '!style!css!react-spinner/react-spinner.css'
import { autobind } from 'core-decorators'
import { isEmail, isURL } from 'validator'
import store from 'store'
import gitHubSvg from 'octicons/svg/mark-github.svg'
import Comment from './comment'
import {
postCommentForm,
hasError,
errorMessage,
postCommentFormHeader,
markdownNote,
commentCounter,
previewWrapper,
preview,
spinnerButton,
buttonContent,
buttonText,
spinnerWrapper,
footer,
marketing,
} from './comments.css'
import { FORM_NAME, FORM_FIELDS } from '../actions/comments'
const authorNameRequired = __CONFIG__.authorNameRequired
const authorEmailRequired = __CONFIG__.authorEmailRequired
const contentSizeLimit = __CONFIG__.contentSizeLimit
const disallowEmptyContent = __CONFIG__.disallowEmptyContent
let contentRegex = /\w+/
function validate (values) {
const errors = {}
const { commentContent, authorName, authorEmail, authorUrl } = values
if (!commentContent) {
errors.commentContent = 'Required'
}
if (commentContent && commentContent.length < 3) {
errors.commentContent = 'Must be at least 3 characters'
}
if (commentContent && contentSizeLimit && (commentContent.length > parseInt(contentSizeLimit))) {
errors.commentContent = 'Comment has exceeded content length of: ' + parseInt(contentSizeLimit) + ' characters.'
}
if (commentContent && disallowEmptyContent && !commentContent.match(contentRegex)) {
errors.commentContent = disallowEmptyContent
}
if (authorNameRequired && !authorName) {
errors.authorName = authorNameRequired
}
if (authorEmailRequired && !authorEmail) {
errors.authorEmail = authorEmailRequired
}
if (authorEmail && !isEmail(authorEmail)) {
errors.authorEmail = 'Email format not valid'
}
if (authorUrl && !isURL(authorUrl)) {
errors.authorUrl = 'URL format not valid'
}
return errors
}
@reduxForm({
form: FORM_NAME,
fields: FORM_FIELDS,
validate,
})
export default class PostCommentForm extends Component {
static propTypes = {
pathname: PropTypes.string.isRequired,
fields: PropTypes.object.isRequired,
error: PropTypes.string,
handleSubmit: PropTypes.func.isRequired,
resetCommentForm: PropTypes.func.isRequired,
submitting: PropTypes.bool.isRequired,
}
constructor (props) {
super(props)
this.state = { height: 0 }
}
componentWillMount () {
const { resetCommentForm, pathname } = this.props
resetCommentForm({ pathname })
}
componentWillReceiveProps (nextProps) {
const {
fields: {
commentContent: {
value: commentContent,
},
authorName: {
value: authorName,
},
authorEmail: {
value: authorEmail,
},
authorUrl: {
value: authorUrl,
},
},
pathname,
} = nextProps
if (store.enabled) {
if (pathname !== null) {
store.set('pathname', pathname)
}
if (commentContent !== null) {
store.set('commentContent', commentContent)
}
if (authorName !== null) {
store.set('authorName', authorName)
}
if (authorEmail !== null) {
store.set('authorEmail', authorEmail)
}
if (authorUrl !== null) {
store.set('authorUrl', authorUrl)
}
}
}
@autobind
getStyle () {
const { fields: { commentContent } } = this.props
const { height } = this.state
if (!commentContent.value || !height) {
return { height: spring(5, presets.gentle) }
}
return { height: spring(height + 20, presets.gentle) }
}
render () {
const {
fields: {
commentContent,
authorName,
authorEmail,
authorUrl,
},
error,
handleSubmit,
submitting,
} = this.props
const gitHubUrl =
'https://help.github.com/articles/basic-writing-and-formatting-syntax/'
const previewComment = {
authorName: authorName.value,
authorUrl: authorUrl.value,
date: new Date(),
commentContent: commentContent.value,
}
const length = commentContent.value ? commentContent.value.length : 0
return (
<form
className={postCommentForm}
onSubmit={handleSubmit}
>
<div className={postCommentFormHeader}>
<strong>Add your comment</strong>
<span className={commentCounter}>{length}</span>
<span className={markdownNote}>
<a
target="_blank"
rel="noopener"
href={gitHubUrl}
>
GitHub-style markdown is supported
</a>
</span>
</div>
{error &&
<div className={errorMessage}>
{error}
</div>
}
<Textarea
placeholder="Type Comment Here"
className={
commentContent.touched && commentContent.error && hasError
}
value={commentContent.value || ''}
{...commentContent}
/>
{commentContent.touched && commentContent.error &&
<div className={errorMessage}>
{commentContent.error}
</div>
}
<input
type="text"
placeholder={authorNameRequired ? "Name (required)" : "Name"}
className={
authorName.touched && authorName.error && hasError
}
spellCheck="false"
{...authorName}
/>
{authorName.touched && authorName.error &&
<div className={errorMessage}>
{authorName.error}
</div>
}
<input
type="email"
placeholder={authorEmailRequired ? "Email (required - not shown)" : "Email (not shown)"}
className={
authorEmail.touched && authorEmail.error && hasError
}
spellCheck="false"
{...authorEmail}
/>
{authorEmail.touched && authorEmail.error &&
<div className={errorMessage}>
{authorEmail.error}
</div>
}
<input
type="text"
placeholder="Website (optional)"
className={
authorUrl.touched && authorUrl.error && hasError
}
spellCheck="false"
{...authorUrl}
/>
{authorUrl.touched && authorUrl.error &&
<div className={errorMessage}>
{authorUrl.error}
</div>
}
<Motion
style={this.getStyle()}
>
{interpolatingStyle =>
<div className={previewWrapper} style={interpolatingStyle}>
<Measure
whitelist={['height']}
config={{ subtree: true, childList: true, attributes: true }}
onMeasure={({ height }) => this.setState({ height })}
>
<div>
<div className={postCommentFormHeader}>
<strong>Preview your comment</strong>
</div>
<div className={preview}>
<Comment comment={previewComment} />
</div>
</div>
</Measure>
</div>
}
</Motion>
<div className={footer}>
<button
type="submit"
className={spinnerButton}
disabled={submitting}
>
<div className={buttonContent}>
<div className={buttonText}>
{submitting ? 'Submitting Comment' : 'Submit Comment'}
</div>
{submitting &&
<div className={spinnerWrapper}>
<Spinner />
</div>
}
</div>
</button>
<div className={marketing}>
Powered by {' '}
<a
target="_blank"
rel="noopener"
href="https://github.com/jimpick/lambda-comments"
>
lambda-comments
<img src={gitHubSvg} />
</a>
</div>
</div>
</form>
)
}
}