CommentContainer.jsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { Container } from 'unstated';
  2. /**
  3. *
  4. * @author Yuki Takei <yuki@weseek.co.jp>
  5. *
  6. * @extends {Container} unstated Container
  7. */
  8. export default class CommentContainer extends Container {
  9. constructor(crowi, pageId, revisionId) {
  10. super();
  11. this.crowi = crowi;
  12. this.pageId = pageId;
  13. this.revisionId = revisionId;
  14. this.state = {
  15. comments: [],
  16. };
  17. this.add = this.add.bind(this);
  18. }
  19. init() {
  20. if (!this.props.pageId) {
  21. return;
  22. }
  23. }
  24. add(comment) {
  25. const comments = this.state.comments;
  26. comments.push(comment);
  27. this.setState({ comments });
  28. }
  29. findAndSplice(comment) {
  30. const comments = this.state.comments;
  31. const index = comments.indexOf(comment);
  32. if (index < 0) {
  33. return;
  34. }
  35. comments.splice(index, 1);
  36. this.setState({ comments });
  37. }
  38. /**
  39. * Load data of comments and store them in state
  40. */
  41. retrieveComments() {
  42. // get data (desc order array)
  43. this.crowi.apiGet('/comments.get', { page_id: this.pageId })
  44. .then((res) => {
  45. if (res.ok) {
  46. this.setState({ comments: res.comments });
  47. }
  48. });
  49. }
  50. /**
  51. * Load data of comments and rerender <PageComments />
  52. */
  53. postComment(comment, isMarkdown, replyTo, isSlackEnabled, slackChannels) {
  54. return this.crowi.apiPost('/comments.add', {
  55. commentForm: {
  56. comment,
  57. _csrf: this.crowi.csrfToken,
  58. page_id: this.pageId,
  59. revision_id: this.revisionId,
  60. is_markdown: isMarkdown,
  61. replyTo,
  62. },
  63. slackNotificationForm: {
  64. isSlackEnabled,
  65. slackChannels,
  66. },
  67. })
  68. .then((res) => {
  69. if (res.ok) {
  70. this.add(res.comment);
  71. }
  72. });
  73. }
  74. deleteComment(comment) {
  75. return this.crowi.apiPost('/comments.remove', { comment_id: comment._id })
  76. .then((res) => {
  77. if (res.ok) {
  78. this.findAndSplice(comment);
  79. }
  80. });
  81. }
  82. onUpload(file) {
  83. const endpoint = '/attachments.add';
  84. // // create a FromData instance
  85. // const formData = new FormData();
  86. // formData.append('_csrf', this.props.data.crowi.csrfToken);
  87. // formData.append('file', file);
  88. // formData.append('path', this.props.data.pagePath);
  89. // formData.append('page_id', this.props.data.pageId || 0);
  90. // // post
  91. // this.props.data.crowi.apiPost(endpoint, formData)
  92. // .then((res) => {
  93. // const attachment = res.attachment;
  94. // const fileName = attachment.originalName;
  95. // let insertText = `[${fileName}](${attachment.filePathProxied})`;
  96. // // when image
  97. // if (attachment.fileFormat.startsWith('image/')) {
  98. // // modify to "![fileName](url)" syntax
  99. // insertText = `!${insertText}`;
  100. // }
  101. // this.editor.insertText(insertText);
  102. // })
  103. // .catch(this.apiErrorHandler)
  104. // // finally
  105. // .then(() => {
  106. // this.editor.terminateUploadingState();
  107. // });
  108. }
  109. }