Comment.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import moment from 'moment/src/moment';
  4. import UserPicture from '../User/UserPicture';
  5. export default class Comment extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. this.isCurrentUserIsAuthor = this.isCurrentUserIsAuthor.bind(this);
  9. this.isCurrentRevision = this.isCurrentRevision.bind(this);
  10. this.getRootClassName = this.getRootClassName.bind(this);
  11. }
  12. isCurrentUserIsAuthor() {
  13. return this.props.comment.creator._id === this.props.currentUserId;
  14. }
  15. isCurrentRevision() {
  16. return this.props.comment.revision === this.props.currentRevisionId;
  17. }
  18. getRootClassName() {
  19. return "page-comment "
  20. + (this.isCurrentUserIsAuthor() ? 'page-comment-me' : '')
  21. + (this.isCurrentRevision() ? '': 'page-comment-old');
  22. }
  23. getRevisionLabelClassName() {
  24. return 'page-comment-revision label '
  25. + (this.isCurrentRevision() ? 'label-primary' : 'label-default');
  26. }
  27. render() {
  28. const comment = this.props.comment;
  29. const creator = comment.creator;
  30. const rootClassName = this.getRootClassName();
  31. const commentDate = moment(comment.createdAt).format('YYYY/MM/DD HH:mm:ss');
  32. const revHref = `?revision=${comment.revision}`;
  33. const revFirst8Letters = comment.revision.substr(0,8);
  34. const revisionLavelClassName = this.getRevisionLabelClassName();
  35. return (
  36. <div className={rootClassName}>
  37. <UserPicture user={creator} />
  38. <div className="page-comment-main">
  39. <div className="page-comment-creator">{creator.username}</div>
  40. <div className="page-comment-body">{comment.comment.replace(/(\r\n|\r|\n)/g, '<br>')}</div>
  41. <div className="page-comment-meta">
  42. {commentDate}&nbsp;
  43. <a className={revisionLavelClassName} href={revHref}>{revFirst8Letters}</a>
  44. </div>
  45. </div>
  46. </div>
  47. );
  48. }
  49. }
  50. Comment.propTypes = {
  51. comment: PropTypes.object.isRequired,
  52. currentRevisionId: PropTypes.string.isRequired,
  53. currentUserId: PropTypes.string.isRequired,
  54. };