CommentContainer.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. // [TODO][GW - 1942] add method for updating imageUrlCached
  51. const { pageId } = this.getPageContainer().state;
  52. // get data (desc order array)
  53. return this.appContainer.apiGet('/comments.get', { page_id: pageId })
  54. .then((res) => {
  55. if (res.ok) {
  56. this.setState({ comments: res.comments });
  57. }
  58. });
  59. }
  60. /**
  61. * Load data of comments and rerender <PageComments />
  62. */
  63. postComment(comment, isMarkdown, replyTo, isSlackEnabled, slackChannels) {
  64. const { pageId, revisionId } = this.getPageContainer().state;
  65. return this.appContainer.apiPost('/comments.add', {
  66. commentForm: {
  67. comment,
  68. page_id: pageId,
  69. revision_id: revisionId,
  70. is_markdown: isMarkdown,
  71. replyTo,
  72. },
  73. slackNotificationForm: {
  74. isSlackEnabled,
  75. slackChannels,
  76. },
  77. })
  78. .then((res) => {
  79. if (res.ok) {
  80. return this.retrieveComments();
  81. }
  82. });
  83. }
  84. /**
  85. * Load data of comments and rerender <PageComments />
  86. */
  87. putComment(comment, isMarkdown, commentId, author) {
  88. const { pageId, revisionId } = this.getPageContainer().state;
  89. return this.appContainer.apiPost('/comments.update', {
  90. commentForm: {
  91. comment,
  92. page_id: pageId,
  93. revision_id: revisionId,
  94. is_markdown: isMarkdown,
  95. comment_id: commentId,
  96. author,
  97. },
  98. })
  99. .then((res) => {
  100. if (res.ok) {
  101. return this.retrieveComments();
  102. }
  103. });
  104. }
  105. deleteComment(comment) {
  106. return this.appContainer.apiPost('/comments.remove', { comment_id: comment._id })
  107. .then((res) => {
  108. if (res.ok) {
  109. this.findAndSplice(comment);
  110. }
  111. });
  112. }
  113. uploadAttachment(file) {
  114. const { pageId, pagePath } = this.getPageContainer().state;
  115. const endpoint = '/attachments.add';
  116. const formData = new FormData();
  117. formData.append('_csrf', this.appContainer.csrfToken);
  118. formData.append('file', file);
  119. formData.append('path', pagePath);
  120. formData.append('page_id', pageId);
  121. return this.appContainer.apiPost(endpoint, formData);
  122. }
  123. }