Comment.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import moment from '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 creatorsPage = `/user/${creator.username}`;
  48. const revHref = `?revision=${comment.revision}`;
  49. const revFirst8Letters = comment.revision.substr(0,8);
  50. const revisionLavelClassName = this.getRevisionLabelClassName();
  51. return (
  52. <div className={rootClassName}>
  53. <a href={creatorsPage}>
  54. <UserPicture user={creator} />
  55. </a>
  56. <div className="page-comment-main">
  57. <div className="page-comment-creator">
  58. <a href={creatorsPage}>{creator.username}</a>
  59. </div>
  60. <div className="page-comment-body">{commentBody}</div>
  61. <div className="page-comment-meta">
  62. {commentDate}&nbsp;
  63. <a className={revisionLavelClassName} href={revHref}>{revFirst8Letters}</a>
  64. </div>
  65. <div className="page-comment-control">
  66. <a className="btn btn-link" onClick={this.deleteBtnClickedHandler}>
  67. <i className="fa fa-trash-o"></i>
  68. </a>
  69. </div>
  70. </div>
  71. </div>
  72. );
  73. }
  74. }
  75. Comment.propTypes = {
  76. comment: PropTypes.object.isRequired,
  77. currentRevisionId: PropTypes.string.isRequired,
  78. currentUserId: PropTypes.string.isRequired,
  79. deleteBtnClicked: PropTypes.func.isRequired,
  80. };