-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathCommentCreateWidget.js
More file actions
149 lines (134 loc) · 3.7 KB
/
CommentCreateWidget.js
File metadata and controls
149 lines (134 loc) · 3.7 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
import React, { Component, PropTypes } from "react";
import { connect } from "react-redux";
import styles from "./CommentCreateWidget.css";
import {
addCommentRequest,
cancelEditMode,
updateCommentRequest
} from "../../CommentActions";
class CommentCreateWidget extends Component {
state = {
author: "",
body: ""
};
componentWillReceiveProps(nextProps) {
if (nextProps.editComment) {
this.setState({
author: nextProps.editComment.author,
body: nextProps.editComment.body
});
}
}
createComment = e => {
e.preventDefault();
if (this.state.author && this.state.body) {
this.props.dispatch(
addCommentRequest(
{ author: this.state.author, body: this.state.body },
this.props.ownerId
)
);
this.setState({ author: "", body: "" });
}
};
saveEditComment = e => {
e.preventDefault();
if (!this.state.body) {
return;
}
if (this.props.editComment.body === this.state.body) {
this.cancelEditComment(e);
return;
}
this.props.dispatch(
updateCommentRequest(this.state.body, this.props.editComment.cuid)
);
this.cancelEditComment(e);
};
cancelEditComment = e => {
e.preventDefault();
this.props.dispatch(cancelEditMode());
this.setState({ author: "", body: "" });
};
renderCreateForm() {
return (
<div className={styles["form"]}>
<div className={styles["form-content"]}>
<h2 className={styles["form-title"]}>Write a new comment</h2>
<input
placeholder="Author"
className={styles["form-field"]}
value={this.state.author}
onChange={e => this.setState({ author: e.target.value })}
/>
<textarea
placeholder="Write your comment here"
className={styles["form-field"]}
value={this.state.body}
onChange={e => this.setState({ body: e.target.value })}
/>
<a
className={styles["comment-submit-button"]}
href="#"
onClick={this.createComment}
>
Create
</a>
</div>
</div>
);
}
renderEditForm = () => {
return (
<div className={styles["form"]}>
<div className={styles["form-content"]}>
<h2 className={styles["form-title"]}>Edit existing comment</h2>
<h3 className={styles["comment-author"]}>By {this.state.author}</h3>
<textarea
placeholder="Write your comment here"
className={styles["form-field"]}
value={this.state.body}
onChange={e => this.setState({ body: e.target.value })}
/>
<a
className={styles["comment-submit-button"]}
href="#"
onClick={this.saveEditComment}
>
Save Edit
</a>
<a
className={styles["comment-cancel-button"]}
href="#"
onClick={this.cancelEditComment}
>
Cancel
</a>
</div>
</div>
);
};
render() {
if (this.props.editComment) {
return this.renderEditForm();
}
return this.renderCreateForm();
}
}
CommentCreateWidget.propTypes = {
ownerId: PropTypes.string.isRequired,
editComment: PropTypes.shape({
author: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
cuid: PropTypes.string.isRequired,
owner: PropTypes.string.isRequired,
dataAdded: PropTypes.instanceOf(Date)
}),
dispatch: PropTypes.func.isRequired
};
function mapStateToProps({ comments }) {
return {
editComment: comments.editComment
};
}
export default connect(mapStateToProps)(CommentCreateWidget);