Comment.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import moment from 'moment/src/moment';
  4. import ReactUtils from '../ReactUtils';
  5. import UserPicture from '../User/UserPicture';
  6. /**
  7. *
  8. * @author Yuki Takei <yuki@weseek.co.jp>
  9. *
  10. * @export
  11. * @class Comment
  12. * @extends {React.Component}
  13. */
  14. export default class Comment extends React.Component {
  15. constructor(props) {
  16. super(props);
  17. this.isCurrentUserIsAuthor = this.isCurrentUserEqualsToAuthor.bind(this);
  18. this.isCurrentRevision = this.isCurrentRevision.bind(this);
  19. this.getRootClassName = this.getRootClassName.bind(this);
  20. this.getRevisionLabelClassName = this.getRevisionLabelClassName.bind(this);
  21. this.deleteBtnClickedHandler = this.deleteBtnClickedHandler.bind(this);
  22. }
  23. isCurrentUserEqualsToAuthor() {
  24. return this.props.comment.creator.username === this.props.currentUserId;
  25. }
  26. isCurrentRevision() {
  27. return this.props.comment.revision === this.props.currentRevisionId;
  28. }
  29. getRootClassName() {
  30. return "page-comment "
  31. + (this.isCurrentUserEqualsToAuthor() ? 'page-comment-me' : '')
  32. + (this.isCurrentRevision() ? '': 'page-comment-old');
  33. }
  34. getRevisionLabelClassName() {
  35. return 'page-comment-revision label '
  36. + (this.isCurrentRevision() ? 'label-primary' : 'label-default');
  37. }
  38. deleteBtnClickedHandler() {
  39. this.props.deleteBtnClicked(this.props.comment);
  40. }
  41. render() {
  42. const comment = this.props.comment;
  43. const creator = comment.creator;
  44. const rootClassName = this.getRootClassName();
  45. const commentDate = moment(comment.createdAt).format('YYYY/MM/DD HH:mm');
  46. const commentBody = ReactUtils.nl2br(comment.comment);
  47. const revHref = `?revision=${comment.revision}`;
  48. const revFirst8Letters = comment.revision.substr(0,8);
  49. const revisionLavelClassName = this.getRevisionLabelClassName();
  50. return (
  51. <div className={rootClassName}>
  52. <UserPicture user={creator} />
  53. <div className="page-comment-main">
  54. <div className="page-comment-creator">{creator.username}</div>
  55. <div className="page-comment-body">{commentBody}</div>
  56. <div className="page-comment-meta">
  57. {commentDate}&nbsp;
  58. <a className={revisionLavelClassName} href={revHref}>{revFirst8Letters}</a>
  59. </div>
  60. <div className="page-comment-control">
  61. <a className="btn btn-link" onClick={this.deleteBtnClickedHandler}>
  62. <i className="fa fa-trash-o"></i>
  63. </a>
  64. </div>
  65. </div>
  66. </div>
  67. );
  68. }
  69. }
  70. Comment.propTypes = {
  71. comment: PropTypes.object.isRequired,
  72. currentRevisionId: PropTypes.string.isRequired,
  73. currentUserId: PropTypes.string.isRequired,
  74. deleteBtnClicked: PropTypes.func.isRequired,
  75. };