AdminNotificationContainer.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { isServer } from '@growi/core/dist/utils';
  2. import { Container } from 'unstated';
  3. import {
  4. apiv3Delete, apiv3Get, apiv3Post, apiv3Put,
  5. } from '../util/apiv3-client';
  6. /**
  7. * Service container for admin Notification setting page (NotificationSetting.jsx)
  8. * @extends {Container} unstated Container
  9. */
  10. export default class AdminNotificationContainer extends Container {
  11. constructor(appContainer) {
  12. super();
  13. if (isServer()) {
  14. return;
  15. }
  16. this.appContainer = appContainer;
  17. this.state = {
  18. retrieveError: null,
  19. isSlackbotConfigured: null,
  20. isSlackLegacyConfigured: null,
  21. currentBotType: null,
  22. userNotifications: [],
  23. isNotificationForOwnerPageEnabled: false,
  24. isNotificationForGroupPageEnabled: false,
  25. globalNotifications: [],
  26. };
  27. }
  28. /**
  29. * Workaround for the mangling in production build to break constructor.name
  30. */
  31. static getClassName() {
  32. return 'AdminNotificationContainer';
  33. }
  34. /**
  35. * Retrieve notificationData
  36. */
  37. async retrieveNotificationData() {
  38. const response = await apiv3Get('/notification-setting/');
  39. const { notificationParams } = response.data;
  40. this.setState({
  41. isSlackbotConfigured: notificationParams.isSlackbotConfigured,
  42. isSlackLegacyConfigured: notificationParams.isSlackLegacyConfigured,
  43. currentBotType: notificationParams.currentBotType,
  44. userNotifications: notificationParams.userNotifications,
  45. isNotificationForOwnerPageEnabled: notificationParams.isNotificationForOwnerPageEnabled,
  46. isNotificationForGroupPageEnabled: notificationParams.isNotificationForGroupPageEnabled,
  47. globalNotifications: notificationParams.globalNotifications,
  48. });
  49. }
  50. /**
  51. * Update slackAppConfiguration
  52. * @memberOf SlackAppConfiguration
  53. */
  54. async updateSlackAppConfiguration() {
  55. const response = await apiv3Put('/notification-setting/slack-configuration', {
  56. webhookUrl: this.state.webhookUrl,
  57. isIncomingWebhookPrioritized: this.state.isIncomingWebhookPrioritized,
  58. slackToken: this.state.slackToken,
  59. });
  60. return response;
  61. }
  62. /**
  63. * Add notificationPattern
  64. * @memberOf SlackAppConfiguration
  65. */
  66. async addNotificationPattern(pathPattern, channel) {
  67. const response = await apiv3Post('/notification-setting/user-notification', {
  68. pathPattern,
  69. channel,
  70. });
  71. this.setState({ userNotifications: response.data.responseParams.userNotifications });
  72. }
  73. /**
  74. * Delete user trigger notification pattern
  75. */
  76. async deleteUserTriggerNotificationPattern(notificatiionId) {
  77. const response = await apiv3Delete(`/notification-setting/user-notification/${notificatiionId}`);
  78. const deletedNotificaton = response.data;
  79. await this.retrieveNotificationData();
  80. return deletedNotificaton;
  81. }
  82. /**
  83. * Switch isNotificationForOwnerPageEnabled
  84. */
  85. switchIsNotificationForOwnerPageEnabled() {
  86. this.setState({ isNotificationForOwnerPageEnabled: !this.state.isNotificationForOwnerPageEnabled });
  87. }
  88. /**
  89. * Switch isNotificationForGroupPageEnabled
  90. */
  91. switchIsNotificationForGroupPageEnabled() {
  92. this.setState({ isNotificationForGroupPageEnabled: !this.state.isNotificationForGroupPageEnabled });
  93. }
  94. /**
  95. * Update globalNotificationForPages
  96. * @memberOf SlackAppConfiguration
  97. */
  98. async updateGlobalNotificationForPages() {
  99. const response = await apiv3Put('/notification-setting/notify-for-page-grant/', {
  100. isNotificationForOwnerPageEnabled: this.state.isNotificationForOwnerPageEnabled,
  101. isNotificationForGroupPageEnabled: this.state.isNotificationForGroupPageEnabled,
  102. });
  103. return response;
  104. }
  105. /**
  106. * Delete global notification pattern
  107. */
  108. async deleteGlobalNotificationPattern(notificatiionId) {
  109. const response = await apiv3Delete(`/notification-setting/global-notification/${notificatiionId}`);
  110. const deletedNotificaton = response.data;
  111. await this.retrieveNotificationData();
  112. return deletedNotificaton;
  113. }
  114. }