2016-03-17 17:48:25 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
2016-02-18 17:53:04 +09:00
|
|
|
import Formsy from 'formsy-react';
|
|
|
|
import FRC from 'formsy-react-components';
|
2016-03-23 11:37:15 +09:00
|
|
|
import Actions from '../../actions.js';
|
2016-02-18 17:53:04 +09:00
|
|
|
|
|
|
|
const Textarea = FRC.Textarea;
|
|
|
|
|
2016-03-17 17:48:25 +09:00
|
|
|
class CommentNew extends Component {
|
2016-02-18 17:53:04 +09:00
|
|
|
|
2016-03-17 17:48:25 +09:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.submitComment = this.submitComment.bind(this);
|
|
|
|
}
|
2016-02-18 17:53:04 +09:00
|
|
|
|
|
|
|
submitComment(data) {
|
|
|
|
|
2016-02-23 16:49:56 +09:00
|
|
|
const parentComment = this.props.parentComment;
|
|
|
|
const component = this;
|
|
|
|
|
2016-02-18 17:53:04 +09:00
|
|
|
data = {
|
|
|
|
...data,
|
2016-02-23 16:49:56 +09:00
|
|
|
postId: this.props.postId
|
2016-02-18 17:53:04 +09:00
|
|
|
}
|
|
|
|
|
2016-02-23 16:49:56 +09:00
|
|
|
if (parentComment) { // replying to a comment
|
|
|
|
data = {
|
|
|
|
...data,
|
|
|
|
parentCommentId: parentComment._id,
|
|
|
|
// if parent comment has a topLevelCommentId use it; if it doesn't then it *is* the top level comment
|
|
|
|
topLevelCommentId: parentComment.topLevelCommentId || parentComment._id
|
|
|
|
}
|
|
|
|
}
|
2016-02-18 17:53:04 +09:00
|
|
|
|
2016-03-23 11:37:15 +09:00
|
|
|
Actions.call("comments.new", data, (error, result) => {
|
2016-02-18 21:39:33 +09:00
|
|
|
if (error) {
|
|
|
|
// handle error
|
|
|
|
} else {
|
2016-02-23 16:49:56 +09:00
|
|
|
if (this.props.submitCallback) {
|
|
|
|
this.props.submitCallback();
|
|
|
|
}
|
|
|
|
// console.log(component)
|
|
|
|
// component.refs.commentTextarea.reset(); //TODO: why are refs not working?
|
2016-02-18 17:53:04 +09:00
|
|
|
}
|
|
|
|
});
|
2016-03-17 17:48:25 +09:00
|
|
|
}
|
2016-02-18 17:53:04 +09:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Formsy.Form onSubmit={this.submitComment}>
|
|
|
|
<Textarea
|
|
|
|
name="body"
|
|
|
|
value=""
|
|
|
|
label="Body"
|
|
|
|
type="text"
|
2016-02-19 09:54:13 +09:00
|
|
|
className="textarea"
|
2016-02-18 17:53:04 +09:00
|
|
|
/>
|
2016-02-19 09:54:13 +09:00
|
|
|
<button type="submit" className="button button--primary">Submit</button>
|
2016-02-18 17:53:04 +09:00
|
|
|
</Formsy.Form>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-03-17 17:48:25 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
CommentNew.propTypes = {
|
|
|
|
postId: React.PropTypes.string.isRequired,
|
|
|
|
submitCallback: React.PropTypes.func, // a callback to execute when the submission has been successful
|
|
|
|
type: React.PropTypes.string, // "comment" or "reply"
|
|
|
|
parentComment: React.PropTypes.object, // if reply, the comment being replied to
|
|
|
|
parentCommentId: React.PropTypes.string, // if reply
|
|
|
|
topLevelCommentId: React.PropTypes.string // if reply
|
|
|
|
}
|
2016-02-18 17:53:04 +09:00
|
|
|
|
|
|
|
module.exports = CommentNew;
|