-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathCommentFormWidget.js
More file actions
110 lines (93 loc) · 3.08 KB
/
CommentFormWidget.js
File metadata and controls
110 lines (93 loc) · 3.08 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
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { FormattedMessage, injectIntl } from 'react-intl';
// Import styles
import styles from './CommentFormWidget.css';
// Import components
import CommentForm from '../components/CommentForm/CommentForm';
// Import actions
import { addCommentRequest, deleteCommentRequest, editCommentRequest } from '../CommentsActions';
import { withRouter } from 'react-router';
export class CommentFormWidget extends React.Component {
constructor(props) {
super(props);
this.state = {
editMode: !!props.initialValues,
isFormShown: false,
comment: props.initialValues,
};
}
componentDidUpdate(prevProps) {
if (
prevProps.initialValues && this.props.initialValues &&
(prevProps.initialValues.author !== this.props.initialValues.author ||
prevProps.initialValues.content !== this.props.initialValues.content)
) {
this.refreshComment();
}
}
showForm = () => this.setState({ isFormShown: true });
closeForm = () => this.setState(
{ isFormShown: false },
() => {
if (this.props.onClose) {
this.props.onClose();
}
},
);
refreshComment = () => this.setState({ comment: this.props.initialValues });
handleFormSubmit = (formData) => {
const { editComment, addComment, submitCallback, params } = this.props;
const { cuid: postId } = params;
if (!postId) {
formData.reject('PostId should be specified.');
return;
}
const payload = { ...formData, postId };
if (this.state.editMode) {
editComment(payload);
} else {
addComment(payload);
}
if (submitCallback) {
submitCallback();
}
this.closeForm();
};
render() {
const initialValues = this.state.comment || { author: '', content: '' };
return this.props.inline || this.state.isFormShown ? (
<div className={`${styles.container} ${this.props.inline ? styles.inline : ''}`}>
<CommentForm
inline={this.props.inline}
onClose={this.closeForm}
initialValues={initialValues}
onSubmit={this.handleFormSubmit}
/>
</div>
) : <button className={`btn ${styles['show-btn']}`} onClick={this.showForm}>
<FormattedMessage id={this.state.editMode ? 'editComment' : 'addComment'} />
</button>;
}
}
CommentFormWidget.propTypes = {
initialValues: PropTypes.shape({
author: PropTypes.string,
content: PropTypes.string,
commentId: PropTypes.string,
}),
onClose: PropTypes.func,
inline: PropTypes.bool,
addComment: PropTypes.func,
editComment: PropTypes.func,
submitCallback: PropTypes.func,
params: PropTypes.object,
};
const mapStateToProps = () => ({});
const mapDispatchToProps = dispatch => ({
addComment: (comment) => dispatch(addCommentRequest(comment)),
editComment: (comment) => dispatch(editCommentRequest(comment)),
deleteComment: (commentId) => dispatch(deleteCommentRequest(commentId)),
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(withRouter(CommentFormWidget)));