CommentContainer.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { Container } from 'unstated';
  2. import loggerFactory from '@alias/logger';
  3. const logger = loggerFactory('growi:services:CommentContainer');
  4. /**
  5. *
  6. * @author Yuki Takei <yuki@weseek.co.jp>
  7. *
  8. * @extends {Container} unstated Container
  9. */
  10. export default class CommentContainer extends Container {
  11. constructor(appContainer) {
  12. super();
  13. this.appContainer = appContainer;
  14. this.appContainer.registerContainer(this);
  15. const mainContent = document.querySelector('#content-main');
  16. if (mainContent == null) {
  17. logger.debug('#content-main element is not exists');
  18. return;
  19. }
  20. this.state = {
  21. comments: [],
  22. // settings shared among all of CommentEditor
  23. isSlackEnabled: false,
  24. slackChannels: mainContent.getAttribute('data-slack-channels') || '',
  25. };
  26. this.retrieveComments = this.retrieveComments.bind(this);
  27. }
  28. /**
  29. * Workaround for the mangling in production build to break constructor.name
  30. */
  31. static getClassName() {
  32. return 'CommentContainer';
  33. }
  34. getPageContainer() {
  35. return this.appContainer.getContainer('PageContainer');
  36. }
  37. findAndSplice(comment) {
  38. const comments = this.state.comments;
  39. const index = comments.indexOf(comment);
  40. if (index < 0) {
  41. return;
  42. }
  43. comments.splice(index, 1);
  44. this.setState({ comments });
  45. }
  46. /**
  47. * Load data of comments and store them in state
  48. */
  49. retrieveComments() {
  50. const { pageId } = this.getPageContainer().state;
  51. // get data (desc order array)
  52. return this.appContainer.apiGet('/comments.get', { page_id: pageId })
  53. .then((res) => {
  54. if (res.ok) {
  55. this.setState({ comments: res.comments });
  56. }
  57. });
  58. }
  59. /**
  60. * Load data of comments and rerender <PageComments />
  61. */
  62. postComment(comment, isMarkdown, replyTo, isSlackEnabled, slackChannels) {
  63. const { pageId, revisionId } = this.getPageContainer().state;
  64. return this.appContainer.apiPost('/comments.add', {
  65. commentForm: {
  66. comment,
  67. page_id: pageId,
  68. revision_id: revisionId,
  69. is_markdown: isMarkdown,
  70. replyTo,
  71. },
  72. slackNotificationForm: {
  73. isSlackEnabled,
  74. slackChannels,
  75. },
  76. })
  77. .then((res) => {
  78. if (res.ok) {
  79. return this.retrieveComments();
  80. }
  81. });
  82. }
  83. /**
  84. * Load data of comments and rerender <PageComments />
  85. */
  86. putComment(comment, isMarkdown, commentId, author) {
  87. const { pageId, revisionId } = this.getPageContainer().state;
  88. return this.appContainer.apiPost('/comments.update', {
  89. commentForm: {
  90. comment,
  91. page_id: pageId,
  92. revision_id: revisionId,
  93. is_markdown: isMarkdown,
  94. comment_id: commentId,
  95. author,
  96. },
  97. })
  98. .then((res) => {
  99. if (res.ok) {
  100. return this.retrieveComments();
  101. }
  102. });
  103. }
  104. deleteComment(comment) {
  105. return this.appContainer.apiPost('/comments.remove', { comment_id: comment._id })
  106. .then((res) => {
  107. if (res.ok) {
  108. this.findAndSplice(comment);
  109. }
  110. });
  111. }
  112. uploadAttachment(file) {
  113. const { pageId, pagePath } = this.getPageContainer().state;
  114. const endpoint = '/attachments.add';
  115. const formData = new FormData();
  116. formData.append('file', file);
  117. formData.append('path', pagePath);
  118. formData.append('page_id', pageId);
  119. return this.appContainer.apiPost(endpoint, formData);
  120. }
  121. }