Comments.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import React, {PureComponent} from "react";
  2. import {CommentsWrapper, CommentTextarea} from '../style';
  3. import {getFormatTime, getTime} from '../../../lib/public';
  4. import {Pagination, message} from 'antd';
  5. import axios from "axios";
  6. import {loginGithubHandel} from '../../../lib/auth';
  7. import openWindow from '../../../lib/openWindow';
  8. class Comments extends PureComponent {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. id: props.id,
  13. isComment: props.isComment,
  14. commentsList: [],
  15. pageInfo: {},
  16. value: '',
  17. parentId: '',
  18. preContent: ''
  19. };
  20. this.setValue = this.setValue.bind(this);
  21. this.addComments = this.addComments.bind(this);
  22. this.reply = this.reply.bind(this);
  23. }
  24. render() {
  25. const {pageInfo, id, commentsList, isComment} = this.state;
  26. return (
  27. <CommentsWrapper>
  28. <h3 className='comments-list-title'>Comments | <span className="noticom">{pageInfo.total} 条评论 </span>
  29. </h3>
  30. <ul className='commentwrap'>
  31. {commentsList.map((item, index) => {
  32. return (
  33. <li className='comment' key={index}>
  34. <div className='commentinfo flex-items'>
  35. <img src={item.authorAvatar} alt=""/>
  36. <div className='commeta cell'>
  37. <h2>{item.authorName}</h2>
  38. <h3>{getTime(item.createTime)} | {getFormatTime(item.createTime)}</h3>
  39. </div>
  40. <span onClick={() => this.reply(item.id, item.authorName)}
  41. className='comment-reply-link'>Reply</span>
  42. </div>
  43. <div className='body'>
  44. <p>{item.parentUserName && <span>@{item.parentUserName}</span>}{item.content}</p>
  45. </div>
  46. <hr/>
  47. </li>
  48. )
  49. })}
  50. </ul>
  51. <Pagination
  52. className='pagination'
  53. hideOnSinglePage
  54. size="small"
  55. onChange={(page) => this.getComments(id, page)}
  56. itemRender={this.itemRender}
  57. current={pageInfo.page} pageSize={5} total={pageInfo.total}
  58. />
  59. {isComment === 1 ? <CommentTextarea>
  60. <textarea
  61. placeholder="你是我一生只会遇见一次的惊喜 ..."
  62. name="comment"
  63. className="commentbody"
  64. id="comment"
  65. rows="5" tabIndex="4"
  66. value={this.state.value}
  67. onChange={this.setValue}
  68. />
  69. <div className='form-submit'>
  70. <input
  71. onClick={this.addComments}
  72. name="submit"
  73. type="submit"
  74. id="submit"
  75. className="submit"
  76. value="BiuBiuBiu~"
  77. />
  78. </div>
  79. </CommentTextarea> : <p className='text'>此处评论已关闭</p>}
  80. </CommentsWrapper>
  81. )
  82. }
  83. componentDidMount() {
  84. this.getComments(this.props.id, 1);
  85. }
  86. reply(parentId, authorName) {
  87. this.setState({
  88. value: `@${authorName}:`,
  89. preContent: `@${authorName}:`,
  90. parentId: parentId
  91. });
  92. }
  93. itemRender(current, type, originalElement) {
  94. if (type === 'prev') {
  95. return <span>« Older</span>;
  96. }
  97. if (type === 'next') {
  98. return <span>Newer »</span>;
  99. }
  100. return originalElement;
  101. }
  102. setValue(e) {
  103. const value = e.target.value;
  104. this.setState((prevState) => {
  105. return {
  106. value: value,
  107. parentId: value ? prevState.parentId : '',
  108. preContent: value ? prevState.preContent : '',
  109. }
  110. });
  111. }
  112. addComments() {
  113. const {value, id, parentId, preContent} = this.state;
  114. const data = {content: value, postsId: id};
  115. if (value === '') {
  116. message.warning('please type a comment');
  117. return false
  118. }
  119. if (parentId) {
  120. let content = value.replace(preContent, "");
  121. if (content === '') {
  122. message.warning('please type a comment');
  123. return false
  124. }
  125. if (value.indexOf(preContent, 0) !== -1) {
  126. data["parentId"] = parentId;
  127. data["content"] = content;
  128. }
  129. }
  130. axios({
  131. method: 'post',
  132. url: '/comments/comments/v1/add',
  133. data: data
  134. }).then((res) => {
  135. if (res.success === 1) {
  136. message.success('评论成功');
  137. this.setState({
  138. value: '',
  139. parentId: ''
  140. });
  141. this.getComments(id, 1);
  142. } else {
  143. this.login();
  144. }
  145. });
  146. }
  147. login() {
  148. axios.get('/auth/github/v1/get').then((res) => {
  149. if (res.success === 1) {
  150. openWindow(res.model, "绑定GitHub", 540, 540);
  151. window.addEventListener("message", loginGithubHandel, false);
  152. }
  153. });
  154. }
  155. getComments(id, page) {
  156. axios.get('/comments/comments-posts/v1/list', {
  157. params: {
  158. page: page,
  159. size: 5,
  160. postsId: id
  161. }
  162. }).then((res) => {
  163. if (res.success === 1) {
  164. this.setState({
  165. commentsList: res.models,
  166. pageInfo: res.pageInfo
  167. })
  168. }
  169. });
  170. }
  171. }
  172. export default Comments;