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
|
import { type FC, useCallback, useId } from 'react';
import { useIntl } from 'react-intl';
import type { Nullable } from '../../../../types';
import {
type FormSubmitHandler,
useForm,
type FormSubmitStatus,
type FormSubmitMessages,
} from '../../../../utils/hooks';
import {
Button,
Form,
type FormProps,
Spinner,
Input,
TextArea,
Label,
Notice,
} from '../../../atoms';
import { LabelledField } from '../../../molecules';
import styles from './comment-form.module.scss';
export type CommentFormData = {
author: string;
comment: string;
email: string;
parentId?: number;
website?: string;
};
export type CommentFormSubmit = FormSubmitHandler<CommentFormData>;
export type CommentFormProps = Omit<FormProps, 'children' | 'onSubmit'> & {
/**
* A callback function to handle form submit.
*/
onSubmit?: CommentFormSubmit;
/**
* The comment parent id.
*/
parentId?: number;
};
export const CommentForm: FC<CommentFormProps> = ({
className = '',
onSubmit,
parentId,
...props
}) => {
const formId = useId();
const formClass = `${styles.form} ${className}`;
const intl = useIntl();
const { messages, submit, submitStatus, update, values } =
useForm<CommentFormData>({
initialValues:
/* The order matter: it will be reused to generate the fields in the right
* order. */
{
parentId,
author: '',
email: '',
website: '',
comment: '',
},
submitHandler: onSubmit,
});
const renderFields = useCallback(() => {
const entries = Object.entries(values) as [
keyof CommentFormData,
CommentFormData[keyof CommentFormData],
][];
const labels: Record<Exclude<keyof CommentFormData, 'parentId'>, string> = {
author: intl.formatMessage({
defaultMessage: 'Name:',
description: 'CommentForm: name label',
id: 'ZIrTee',
}),
comment: intl.formatMessage({
defaultMessage: 'Comment:',
description: 'CommentForm: comment label',
id: 'A8hGaK',
}),
email: intl.formatMessage({
defaultMessage: 'Email:',
description: 'CommentForm: email label',
id: 'Bh7z5v',
}),
website: intl.formatMessage({
defaultMessage: 'Website:',
description: 'CommentForm: website label',
id: 'u41qSk',
}),
};
return entries.map(([field, value]) => {
const isRequired = field !== 'website';
const inputType = field === 'email' ? 'email' : 'text';
const fieldId = `${formId}-${field}`;
return field === 'parentId' ? null : (
<LabelledField
className={styles.field}
field={
field === 'comment' ? (
<TextArea
id={fieldId}
isRequired
name={field}
onChange={update}
value={value}
/>
) : (
<Input
id={fieldId}
isRequired={isRequired}
name={field}
onChange={update}
type={inputType}
value={value}
/>
)
}
key={field}
label={
<Label htmlFor={fieldId} isRequired={isRequired}>
{labels[field]}
</Label>
}
/>
);
});
}, [values, formId, intl, update]);
const btnLabel = intl.formatMessage({
defaultMessage: 'Publish',
description: 'CommentForm: submit button',
id: 'OL0Yzx',
});
const loadingMsg = intl.formatMessage({
defaultMessage: 'Submitting...',
description: 'CommentForm: spinner message on submit',
id: 'IY5ew6',
});
const renderNotice = useCallback(
(
currentStatus: FormSubmitStatus,
msg: Nullable<Partial<FormSubmitMessages>>
) => {
switch (currentStatus) {
case 'FAILED':
return msg?.error ? (
<Notice
// eslint-disable-next-line react/jsx-no-literals
kind="error"
>
{msg.error}
</Notice>
) : null;
case 'PENDING':
return (
<Notice
// eslint-disable-next-line react/jsx-no-literals
kind="info"
>
<Spinner className={styles.spinner}>{loadingMsg}</Spinner>
</Notice>
);
case 'SUCCEEDED':
return msg?.success ? (
<Notice
// eslint-disable-next-line react/jsx-no-literals
kind="success"
>
{msg.success}
</Notice>
) : null;
default:
return null;
}
},
[loadingMsg]
);
return (
<Form {...props} className={formClass} onSubmit={submit}>
{renderFields()}
<Button
className={styles.btn}
isLoading={submitStatus === 'PENDING'}
// eslint-disable-next-line react/jsx-no-literals
kind="primary"
type="submit"
>
{btnLabel}
</Button>
{renderNotice(submitStatus, messages)}
</Form>
);
};
|