通用评论

index.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 } = 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. replyId={item.id}
  54. key={item.id}
  55. content={item}
  56. user_id={item.user_id}
  57. action="replyToReply" // 回复的回复
  58. page={this.state.page}
  59. />,
  60. <div className="comment-more-box" key="show_more_button">
  61. {!isNoMoreReply && replyCount !== len && (
  62. <span
  63. className="comment-show-more"
  64. onClick={() => this.handleGetMoreReply(commentId)}
  65. >
  66. {intl.get("reply.moreReply")}
  67. </span>
  68. )}
  69. <a
  70. style={{ float: "right" }}
  71. onClick={this.handleToggleReply}
  72. >
  73. <Icon type="up" /> {intl.get("reply.collapse")}
  74. </a>
  75. </div>
  76. ];
  77. }
  78. return (
  79. <ContentItem
  80. commentId={commentId}
  81. replyId={item.id}
  82. user_id={item.user_id}
  83. key={item.id}
  84. content={item}
  85. action="replyToReply" // 评论的回复
  86. page={this.state.page}
  87. />
  88. );
  89. })}
  90. </div>
  91. );
  92. }
  93. return null;
  94. }
  95. render() {
  96. const { content, user_id } = this.props;
  97. const { showReply } = this.state;
  98. return (
  99. <div>
  100. <ContentItem
  101. content={content}
  102. onShowReply={this.handleToggleReply}
  103. showReply={showReply}
  104. commentId={content.id}
  105. user_id={user_id}
  106. action="reply" // 评论的回复
  107. page={this.state.page}
  108. />
  109. {this.renderReplies(
  110. content.replies,
  111. content.reply_count,
  112. content.isNoMoreReply
  113. )}
  114. </div>
  115. );
  116. }
  117. }
  118. CommentBox.propTypes = {
  119. commentId: PropTypes.string.isRequired
  120. };
  121. CommentBox.defaultProps = {};
  122. export default Comment(CommentBox);