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
|
import { ButtonSubmit } from '@components/Buttons';
import { Form, FormItem, Input, Label, TextArea } from '@components/Form';
import Notice from '@components/Notice/Notice';
import Spinner from '@components/Spinner/Spinner';
import { createComment } from '@services/graphql/mutations';
import { NoticeType } from '@ts/types/app';
import { ForwardedRef, forwardRef, useState } from 'react';
import { useIntl } from 'react-intl';
import styles from './CommentForm.module.scss';
const CommentForm = (
{ articleId, parentId = 0 }: { articleId: number; parentId?: number },
ref: ForwardedRef<HTMLInputElement>
) => {
const intl = useIntl();
const [name, setName] = useState<string>('');
const [email, setEmail] = useState<string>('');
const [website, setWebsite] = useState<string>('');
const [comment, setComment] = useState<string>('');
const [isSubmitting, setIsSubmitting] = useState<boolean>(false);
const [notice, setNotice] = useState<string>();
const [noticeType, setNoticeType] = useState<NoticeType>('success');
const resetForm = () => {
setName('');
setEmail('');
setWebsite('');
setComment('');
setIsSubmitting(false);
};
const isEmptyString = (value: string): boolean => value.trim() === '';
const areRequiredFieldsSet = (): boolean =>
!isEmptyString(name) && !isEmptyString(email) && !isEmptyString(comment);
const sendComment = async () => {
const data = {
author: name,
authorEmail: email,
authorUrl: website,
content: comment,
parent: parentId,
commentOn: articleId,
mutationId: 'createComment',
};
const createdComment = await createComment(data);
if (createdComment.success) {
resetForm();
setNoticeType('success');
if (createdComment.comment?.approved) {
setNotice(
intl.formatMessage({
defaultMessage: 'Thanks for your comment!',
description: 'CommentForm: success notice',
})
);
} else {
setNotice(
intl.formatMessage({
defaultMessage:
'Thanks for your comment! It is now awaiting moderation.',
description: 'CommentForm: success notice but awaiting moderation',
})
);
}
setTimeout(() => {
setNotice(undefined);
}, 10000);
} else {
setNoticeType('error');
setNotice(
intl.formatMessage({
defaultMessage:
'An unexpected error happened. Comment cannot be submitted.',
description: 'CommentForm: error notice',
})
);
}
};
const submitHandler = async (e: SubmitEvent) => {
e.preventDefault();
setNotice(undefined);
setIsSubmitting(true);
if (areRequiredFieldsSet()) {
sendComment();
} else {
setIsSubmitting(false);
setNoticeType('warning');
setNotice(
intl.formatMessage({
defaultMessage:
'Some required fields are empty. Comment cannot be submitted.',
description: 'CommentForm: missing required fields',
})
);
}
};
const isReply = parentId !== 0;
const wrapperClasses = `${styles.wrapper} ${
isReply ? styles['wrapper--reply'] : ''
}`;
const getLabel = (
body: string,
htmlFor: string,
required: boolean = false
) => {
return <Label body={body} htmlFor={htmlFor} required={required} />;
};
const nameLabelBody = intl.formatMessage({
defaultMessage: 'Name',
description: 'CommentForm: Name field label',
});
const emailLabelBody = intl.formatMessage({
defaultMessage: 'Email',
description: 'CommentForm: Email field label',
});
const websiteLabelBody = intl.formatMessage({
defaultMessage: 'Website',
description: 'CommentForm: Website field label',
});
const commentLabelBody = intl.formatMessage({
defaultMessage: 'Comment',
description: 'CommentForm: Comment field label',
});
return (
<div className={wrapperClasses}>
<h2 className={styles.title}>
{intl.formatMessage({
defaultMessage: 'Leave a comment',
description: 'CommentForm: Form title',
})}
</h2>
<Form
submitHandler={submitHandler}
modifier={isReply ? 'centered' : undefined}
>
<FormItem>
<Input
id="commenter-name"
name="commenter-name"
label={getLabel(nameLabelBody, 'commenter-name', true)}
value={name}
setValue={setName}
ref={ref}
/>
</FormItem>
<FormItem>
<Input
id="commenter-email"
name="commenter-email"
type="email"
label={getLabel(emailLabelBody, 'commenter-email', true)}
value={email}
setValue={setEmail}
/>
</FormItem>
<FormItem>
<Input
id="commenter-website"
name="commenter-website"
label={getLabel(websiteLabelBody, 'commenter-website')}
value={website}
setValue={setWebsite}
/>
</FormItem>
<FormItem>
<TextArea
id="commenter-comment"
name="commenter-comment"
label={getLabel(commentLabelBody, 'commenter-comment', true)}
value={comment}
setValue={setComment}
/>
</FormItem>
<FormItem>
<noscript>
{intl.formatMessage({
defaultMessage: 'Javascript is required to post a comment.',
description: 'CommentForm: noscript tag',
})}
</noscript>
<ButtonSubmit>
{intl.formatMessage({
defaultMessage: 'Send',
description: 'CommentForm: Send button',
})}
</ButtonSubmit>
</FormItem>
</Form>
{isSubmitting && (
<Spinner
message={intl.formatMessage({
defaultMessage: 'Submitting...',
description: 'CommentForm: submitting message',
})}
/>
)}
{notice && <Notice type={noticeType}>{notice}</Notice>}
</div>
);
};
export default forwardRef(CommentForm);
|