通用评论

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { Icon } from "antd";
  4. import intl from "react-intl-universal";
  5. import Comment from "../../Comment";
  6. import ContentItem from "./../ContentItem";
  7. import "./index.css";
  8. class CommentBox extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. showReply: true,
  13. page: 1
  14. };
  15. this.handleToggleReply = this.handleToggleReply.bind(this);
  16. this.handleGetMoreReply = this.handleGetMoreReply.bind(this);
  17. this.renderReplies = this.renderReplies.bind(this);
  18. }
  19. /**
  20. * 切换是否显示回复列表
  21. */
  22. handleToggleReply() {
  23. this.setState({ showReply: !this.state.showReply });
  24. }
  25. /**
  26. * 获取更多评论
  27. * @param {string} commentId comment id
  28. */
  29. handleGetMoreReply(commentId) {
  30. // 从第一页开始获取评论
  31. const { page } = this.state;
  32. this.props.app.sGetReply({ commentId, page });
  33. this.setState({ page: page + 1 });
  34. }
  35. /**
  36. * 渲染回复 DOM
  37. * @param {array} replies 回复列表
  38. * @param {number} replies 回复的数量
  39. * @param {boolean} isNoMoreReply 是否没有更多回复
  40. */
  41. renderReplies(replies, replyCount, isNoMoreReply) {
  42. const { commentId, isMobile } = this.props;
  43. const { showReply } = this.state;
  44. if (showReply && replies && replies.length) {
  45. const len = replies.length;
  46. return (
  47. <div style={{ marginLeft: 50 }}>
  48. {replies.map((item, index) => {
  49. if (index === len - 1) {
  50. return [
  51. <ContentItem
  52. commentId={commentId}
  53. isMobile={isMobile}
  54. replyId={item.id}
  55. key={item.id}
  56. content={item}
  57. action="replyToReply" // 回复的回复
  58. />,
  59. <div className="comment-more-box" key="show_more_button">
  60. {!isNoMoreReply && replyCount !== len && (
  61. <span
  62. className="comment-show-more"
  63. onClick={() => this.handleGetMoreReply(commentId)}
  64. >
  65. {intl.get("reply.moreReply")}
  66. </span>
  67. )}
  68. <a
  69. style={{ float: "right" }}
  70. onClick={this.handleToggleReply}
  71. >
  72. <Icon type="up" /> {intl.get("reply.collapse")}
  73. </a>
  74. </div>
  75. ];
  76. }
  77. return (
  78. <ContentItem
  79. isMobile={isMobile}
  80. commentId={commentId}
  81. replyId={item.id}
  82. key={item.id}
  83. content={item}
  84. action="replyToReply" // 评论的回复
  85. />
  86. );
  87. })}
  88. </div>
  89. );
  90. }
  91. return null;
  92. }
  93. render() {
  94. const { content, isMobile } = this.props;
  95. const { showReply } = this.state;
  96. return (
  97. <div>
  98. <ContentItem
  99. isMobile={isMobile}
  100. content={content}
  101. onShowReply={this.handleToggleReply}
  102. showReply={showReply}
  103. commentId={content.id}
  104. action="reply" // 评论的回复
  105. />
  106. {this.renderReplies(
  107. content.replies,
  108. content.reply_count,
  109. content.isNoMoreReply
  110. )}
  111. </div>
  112. );
  113. }
  114. }
  115. CommentBox.propTypes = {
  116. commentId: PropTypes.string.isRequired
  117. };
  118. CommentBox.defaultProps = {};
  119. export default Comment(CommentBox);