import 'datejs';
import React, { Component } from 'react';
import Markdown from 'react-remarkable';
import Header from './Header';
import Form from './Form';
import style from './Comments.css';
class Comment extends Component {
constructor(props) {
super(props);
this.state = { editionMode: false, comment: props.comment };
this.editComment = this.editComment.bind(this);
}
componentWillReceiveProps(nextProps) {
this.setState({comment: nextProps.comment, editionMode: false});
}
editComment(e) {
e.preventDefault();
this.setState({ editionMode: !this.state.editionMode, comment: this.state.comment });
}
render() {
var view;
if (this.state.editionMode) {
var key = 'comment-form-' + this.state.comment._id;
view = <Form key={key} commentsId={this.props.commentsId} comment={this.state.comment} cancel={this.editComment} />
} else {
view = (
<div className={style.comment} data-comment-id={this.state.comment._id}>
<a href="#" onClick={this.editComment} className={style.edit}>Edit</a>
<span className={style.date}>{new Date(this.state.comment.updatedAt).toString('MMM d, hh:mmtt')}</span>
<Header comment={this.state.comment} />
<div className={style.text}>
<Markdown>{this.state.comment.text}</Markdown>
</div>
</div>
);
}
return view;
}
}
export default Comment;
|