AdminNotificationContainer.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { Container } from 'unstated';
  2. import loggerFactory from '@alias/logger';
  3. import { toastError } from '../util/apiNotification';
  4. const logger = loggerFactory('growi:services:AdminNotificationContainer');
  5. /**
  6. * Service container for admin Notification setting page (NotificationSetting.jsx)
  7. * @extends {Container} unstated Container
  8. */
  9. export default class AdminNotificationContainer extends Container {
  10. constructor(appContainer) {
  11. super();
  12. this.appContainer = appContainer;
  13. this.state = {
  14. retrieveError: null,
  15. selectSlackOption: 'incoming webhooks',
  16. webhookUrl: '',
  17. isIncomingWebhookPrioritized: false,
  18. slackToken: '',
  19. userNotifications: [],
  20. isNotificationForOwnerPageEnabled: false,
  21. isNotificationForGroupPageEnabled: false,
  22. globalNotifications: [],
  23. };
  24. }
  25. /**
  26. * Workaround for the mangling in production build to break constructor.name
  27. */
  28. static getClassName() {
  29. return 'AdminNotificationContainer';
  30. }
  31. /**
  32. * Retrieve notificationData
  33. */
  34. async retrieveNotificationData() {
  35. try {
  36. const response = await this.appContainer.apiv3.get('/notification-setting/');
  37. const { notificationParams } = response.data;
  38. this.setState({
  39. webhookUrl: notificationParams.webhookUrl,
  40. isIncomingWebhookPrioritized: notificationParams.isIncomingWebhookPrioritized,
  41. slackToken: notificationParams.slackToken,
  42. userNotifications: notificationParams.userNotifications,
  43. isNotificationForOwnerPageEnabled: notificationParams.isNotificationForOwnerPageEnabled,
  44. isNotificationForGroupPageEnabled: notificationParams.isNotificationForGroupPageEnabled,
  45. globalNotifications: notificationParams.globalNotifications,
  46. });
  47. }
  48. catch (err) {
  49. logger.error(err);
  50. toastError(new Error('Failed to fetch data'));
  51. }
  52. }
  53. /**
  54. * Switch slackOption
  55. */
  56. switchSlackOption(slackOption) {
  57. this.setState({ selectSlackOption: slackOption });
  58. }
  59. /**
  60. * Change webhookUrl
  61. */
  62. changeWebhookUrl(webhookUrl) {
  63. this.setState({ webhookUrl });
  64. }
  65. /**
  66. * Switch incomingWebhookPrioritized
  67. */
  68. switchIsIncomingWebhookPrioritized() {
  69. this.setState({ isIncomingWebhookPrioritized: !this.state.isIncomingWebhookPrioritized });
  70. }
  71. /**
  72. * Change slackToken
  73. */
  74. changeSlackToken(slackToken) {
  75. this.setState({ slackToken });
  76. }
  77. /**
  78. * Update slackAppConfiguration
  79. * @memberOf SlackAppConfiguration
  80. */
  81. async updateSlackAppConfiguration() {
  82. const response = await this.appContainer.apiv3.put('/notification-setting/slack-configuration', {
  83. webhookUrl: this.state.webhookUrl,
  84. isIncomingWebhookPrioritized: this.state.isIncomingWebhookPrioritized,
  85. slackToken: this.state.slackToken,
  86. });
  87. return response;
  88. }
  89. /**
  90. * Add notificationPattern
  91. * @memberOf SlackAppConfiguration
  92. */
  93. async addNotificationPattern(pathPattern, channel) {
  94. const response = await this.appContainer.apiv3.post('/notification-setting/user-notification', {
  95. pathPattern,
  96. channel,
  97. });
  98. this.setState({ userNotifications: response.data.responseParams.userNotifications });
  99. }
  100. /**
  101. * Delete user trigger notification pattern
  102. */
  103. async deleteUserTriggerNotificationPattern(notificatiionId) {
  104. const response = await this.appContainer.apiv3.delete(`/notification-setting/user-notification/${notificatiionId}`);
  105. const deletedNotificaton = response.data;
  106. await this.retrieveNotificationData();
  107. return deletedNotificaton;
  108. }
  109. /**
  110. * Switch isNotificationForOwnerPageEnabled
  111. */
  112. switchIsNotificationForOwnerPageEnabled() {
  113. this.setState({ isNotificationForOwnerPageEnabled: !this.state.isNotificationForOwnerPageEnabled });
  114. }
  115. /**
  116. * Switch isNotificationForGroupPageEnabled
  117. */
  118. switchIsNotificationForGroupPageEnabled() {
  119. this.setState({ isNotificationForGroupPageEnabled: !this.state.isNotificationForGroupPageEnabled });
  120. }
  121. /**
  122. * Update globalNotificationForPages
  123. * @memberOf SlackAppConfiguration
  124. */
  125. async updateGlobalNotificationForPages() {
  126. const response = await this.appContainer.apiv3.put('/notification-setting/notify-for-page-grant/', {
  127. isNotificationForOwnerPageEnabled: this.state.isNotificationForOwnerPageEnabled,
  128. isNotificationForGroupPageEnabled: this.state.isNotificationForGroupPageEnabled,
  129. });
  130. return response;
  131. }
  132. /**
  133. * Delete global notification pattern
  134. */
  135. async deleteGlobalNotificationPattern(notificatiionId) {
  136. const response = await this.appContainer.apiv3.delete(`/notification-setting/global-notification/${notificatiionId}`);
  137. const deletedNotificaton = response.data;
  138. await this.retrieveNotificationData();
  139. return deletedNotificaton;
  140. }
  141. }