import React, { Component } from 'react';
import Websocket from 'react-websocket';
import dispatcher from '100notes-dispatcher';
import commentsRepository from './CommentsRepository';
import Comment from './Comment';
import Summary from './Summary';
import Form from './Form';
import style from './Comments.css';
class Comments extends Component {
constructor(props) {
super(props);
this.state = { group: { comments: [] }, sort: props.sort ? props.sort.toLowerCase() : 'asc' };
dispatcher.register(this, ['COMMENT-CREATE', 'COMMENT-UPDATE', 'COMMENT-DELETE', 'COMMENTS', 'LOAD_COMMENTS']);
}
componentWillMount() {
commentsRepository.load(this.props.commentsId, 0, this.props.initialSize || this.props.size);
}
handleData(response) {
response = JSON.parse(response);
dispatcher.dispatch('COMMENT-' + response.eventType, response.data);
}
update(eventType, response) {
if (this.props.commentsId !== response._id) {
return;
}
var insertFunction = 'push';
var group = this.state.group;
if (eventType === 'COMMENTS') {
if (this.state.sort === 'asc') {
response.comments.reverse();
}
this.setState({group: response});
} else if (eventType === 'LOAD_COMMENTS') {
if (this.state.sort === 'asc') {
insertFunction = 'unshift';
}
response.comments.forEach((comment) => {
group.comments[insertFunction](comment);
});
group.count = response.count;
this.setState({group: group});
} else if (eventType === 'COMMENT-CREATE') {
var comment = response.comments[0];
if (this.state.sort === 'desc') {
insertFunction = 'unshift';
}
group.count = response.count;
group.comments[insertFunction](comment);
this.setState({group: group});
} else if (eventType === 'COMMENT-UPDATE') {
var comment = response.comments[0];
var exist = group.comments.filter((item) => {
if (item._id === comment._id) {
item.text = comment.text;
item.updatedAt = comment.updatedAt;
return true;
}
return false;
}).length > 0;
if (exist) {
this.setState({group: group});
}
} else Eif (eventType === 'COMMENT-DELETE') {
var comment = response.comments[0];
group.comments = group.comments.filter((item) => {
return item._id !== comment._id;
});
group.count = response.count;
this.setState({group: group});
}
}
render() {
var commentNew = { group: { id: this.props.commentsId }, text: "" };
var commentList = [];
var comments = this.state.group.comments || [];
comments.forEach((comment) => {
var key = 'comment-' + comment._id;
commentList.push(<Comment key={key} commentsId={this.props.commentsId} comment={comment} />);
});
var summaryKey = 'summary-' + this.props.commentsId;
var summary = [<Summary key={summaryKey} commentsId={this.props.commentsId} size={this.props.size} group={this.state.group} />];
var formKey = 'form-' + this.props.commentsId;
var form = <Form key={formKey} commentsId={this.props.commentsId} comment={commentNew} />;
var view = [form, commentList, summary];
if (this.state.sort === 'asc') {
view.reverse();
}
var wsUrl = 'ws://localhost:8080/ws/comment-groups/' + this.props.commentsId;
return (
<div className={style.comments} data-comments-id={this.props.commentsId}>
{view}
<Websocket url={wsUrl} onMessage={this.handleData} reconnect={true} />
</div>
);
}
componentWillUnmount() {
dispatcher.unregister(this);
}
}
export default Comments;
|