AdminNotificationContainer.js 4.5 KB

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