Comment.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. /**
  6. *
  7. * @author Yuki Takei <yuki@weseek.co.jp>
  8. *
  9. * @export
  10. * @class Comment
  11. * @extends {React.Component}
  12. */
  13. export default class Comment extends React.Component {
  14. constructor(props) {
  15. super(props);
  16. this.isCurrentUserIsAuthor = this.isCurrentUserIsAuthor.bind(this);
  17. this.isCurrentRevision = this.isCurrentRevision.bind(this);
  18. this.getRootClassName = this.getRootClassName.bind(this);
  19. }
  20. isCurrentUserIsAuthor() {
  21. return this.props.comment.creator._id === this.props.currentUserId;
  22. }
  23. isCurrentRevision() {
  24. return this.props.comment.revision === this.props.currentRevisionId;
  25. }
  26. getRootClassName() {
  27. return "page-comment "
  28. + (this.isCurrentUserIsAuthor() ? 'page-comment-me' : '')
  29. + (this.isCurrentRevision() ? '': 'page-comment-old');
  30. }
  31. getRevisionLabelClassName() {
  32. return 'page-comment-revision label '
  33. + (this.isCurrentRevision() ? 'label-primary' : 'label-default');
  34. }
  35. render() {
  36. const comment = this.props.comment;
  37. const creator = comment.creator;
  38. const rootClassName = this.getRootClassName();
  39. const commentDate = moment(comment.createdAt).format('YYYY/MM/DD HH:mm');
  40. const revHref = `?revision=${comment.revision}`;
  41. const revFirst8Letters = comment.revision.substr(0,8);
  42. const revisionLavelClassName = this.getRevisionLabelClassName();
  43. return (
  44. <div className={rootClassName}>
  45. <UserPicture user={creator} />
  46. <div className="page-comment-main">
  47. <div className="page-comment-creator">{creator.username}</div>
  48. <div className="page-comment-body">{comment.comment.replace(/(\r\n|\r|\n)/g, '<br>')}</div>
  49. <div className="page-comment-meta">
  50. {commentDate}&nbsp;
  51. <a className={revisionLavelClassName} href={revHref}>{revFirst8Letters}</a>
  52. </div>
  53. <div className="page-comment-control">
  54. <a className="btn btn-link"><i className="fa fa-trash-o"></i></a>
  55. </div>
  56. </div>
  57. </div>
  58. );
  59. }
  60. }
  61. Comment.propTypes = {
  62. comment: PropTypes.object.isRequired,
  63. currentRevisionId: PropTypes.string.isRequired,
  64. currentUserId: PropTypes.string.isRequired,
  65. };