通用评论

index.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { OSS_LINK } from "../../constant";
  4. import Comment from "../../Comment";
  5. import Editor from "../Editor";
  6. const PLACEHOLDER = {
  7. normal: "说点什么吧...",
  8. default: "说点什么吧..."
  9. };
  10. class CommentInput extends Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. value: "",
  15. fileList: [], // 图片列表
  16. fileMap: {} // 已经上传的图片路径和 uid 的映射 { uid: path }
  17. };
  18. this.handleChange = this.handleChange.bind(this);
  19. this.handleSubmit = this.handleSubmit.bind(this);
  20. this.handleChangeFileList = this.handleChangeFileList.bind(this);
  21. this.handleChangeEmoji = this.handleChangeEmoji.bind(this);
  22. this.handleUpload = this.handleUpload.bind(this);
  23. }
  24. handleChange(e) {
  25. this.setState({ value: e.target.value });
  26. }
  27. handleChangeFileList(fileList) {
  28. this.setState({ fileList });
  29. }
  30. handleChangeEmoji(emojiId) {
  31. let { value } = this.state;
  32. value += `[${emojiId}]`;
  33. this.setState({ value });
  34. }
  35. handleUpload({ uid, path }) {
  36. const { fileMap } = this.state;
  37. fileMap[uid] = path;
  38. this.setState({ fileMap });
  39. }
  40. handleSubmit() {
  41. let { value, fileMap, fileList } = this.state;
  42. if (fileList.length) {
  43. fileList.forEach(item => {
  44. value += `[${OSS_LINK}${fileMap[item.uid]}]`;
  45. });
  46. }
  47. const { type, commentId, replyId, handleToggleInput } = this.props;
  48. if (type === "normal") {
  49. this.props.app.sCreateComment({
  50. type: 1,
  51. business_id: "1",
  52. content: value
  53. });
  54. } else if (type === "comment") {
  55. this.props.app.sCreateReply(
  56. {
  57. comment_id: commentId,
  58. content: value
  59. },
  60. () => handleToggleInput()
  61. );
  62. } else if (type === "reply") {
  63. this.props.app.sCreateReply(
  64. {
  65. comment_id: commentId,
  66. content: value,
  67. reply_id: replyId
  68. },
  69. () => handleToggleInput()
  70. );
  71. }
  72. }
  73. render() {
  74. const { type } = this.props;
  75. const { value, fileList } = this.state;
  76. return (
  77. <div>
  78. {type === "normal" ? (
  79. <div>
  80. <span
  81. style={{
  82. border: "1px solid #CECECE",
  83. color: "#666",
  84. padding: "2px 3px"
  85. }}
  86. >
  87. 回复
  88. </span>
  89. <span style={{ marginLeft: "20px", color: "#5198EB" }}>
  90. 口碑
  91. <span style={{ marginLeft: "20px", color: "#666666" }}>
  92. (全站挑出毛病或提出合理建议,奖励10到100元红包)
  93. </span>
  94. </span>
  95. </div>
  96. ) : null}
  97. <div style={{ marginTop: 40 }}>
  98. <Editor
  99. value={value}
  100. placeholder={PLACEHOLDER[type] || PLACEHOLDER.default}
  101. fileList={fileList}
  102. onChange={this.handleChange}
  103. onSubmit={this.handleSubmit}
  104. onChangeFileList={this.handleChangeFileList}
  105. onChangeEmoji={this.handleChangeEmoji}
  106. onUpload={this.handleUpload}
  107. loading={this.props.app.loading.sCreateComment}
  108. />
  109. </div>
  110. </div>
  111. );
  112. }
  113. }
  114. CommentInput.propTypes = {
  115. // normal 有切换回复/口碑的 header ; comment 评论输入框 / reply 回复输入框
  116. type: PropTypes.oneOf(["normal", "comment", "reply"])
  117. };
  118. CommentInput.defaultProps = {
  119. type: "normal"
  120. };
  121. export default Comment(CommentInput);