AdminNotificationContainer.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { Container } from 'unstated';
  2. import {
  3. apiv3Delete, apiv3Get, apiv3Post, apiv3Put,
  4. } from '../util/apiv3-client';
  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. isSlackbotConfigured: null,
  16. isSlackLegacyConfigured: null,
  17. currentBotType: null,
  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 apiv3Get('/notification-setting/');
  35. const { notificationParams } = response.data;
  36. this.setState({
  37. isSlackbotConfigured: notificationParams.isSlackbotConfigured,
  38. isSlackLegacyConfigured: notificationParams.isSlackLegacyConfigured,
  39. currentBotType: notificationParams.currentBotType,
  40. userNotifications: notificationParams.userNotifications,
  41. isNotificationForOwnerPageEnabled: notificationParams.isNotificationForOwnerPageEnabled,
  42. isNotificationForGroupPageEnabled: notificationParams.isNotificationForGroupPageEnabled,
  43. globalNotifications: notificationParams.globalNotifications,
  44. });
  45. }
  46. /**
  47. * Update slackAppConfiguration
  48. * @memberOf SlackAppConfiguration
  49. */
  50. async updateSlackAppConfiguration() {
  51. const response = await apiv3Put('/notification-setting/slack-configuration', {
  52. webhookUrl: this.state.webhookUrl,
  53. isIncomingWebhookPrioritized: this.state.isIncomingWebhookPrioritized,
  54. slackToken: this.state.slackToken,
  55. });
  56. return response;
  57. }
  58. /**
  59. * Add notificationPattern
  60. * @memberOf SlackAppConfiguration
  61. */
  62. async addNotificationPattern(pathPattern, channel) {
  63. const response = await apiv3Post('/notification-setting/user-notification', {
  64. pathPattern,
  65. channel,
  66. });
  67. this.setState({ userNotifications: response.data.responseParams.userNotifications });
  68. }
  69. /**
  70. * Delete user trigger notification pattern
  71. */
  72. async deleteUserTriggerNotificationPattern(notificatiionId) {
  73. const response = await apiv3Delete(`/notification-setting/user-notification/${notificatiionId}`);
  74. const deletedNotificaton = response.data;
  75. await this.retrieveNotificationData();
  76. return deletedNotificaton;
  77. }
  78. /**
  79. * Switch isNotificationForOwnerPageEnabled
  80. */
  81. switchIsNotificationForOwnerPageEnabled() {
  82. this.setState({ isNotificationForOwnerPageEnabled: !this.state.isNotificationForOwnerPageEnabled });
  83. }
  84. /**
  85. * Switch isNotificationForGroupPageEnabled
  86. */
  87. switchIsNotificationForGroupPageEnabled() {
  88. this.setState({ isNotificationForGroupPageEnabled: !this.state.isNotificationForGroupPageEnabled });
  89. }
  90. /**
  91. * Update globalNotificationForPages
  92. * @memberOf SlackAppConfiguration
  93. */
  94. async updateGlobalNotificationForPages() {
  95. const response = await apiv3Put('/notification-setting/notify-for-page-grant/', {
  96. isNotificationForOwnerPageEnabled: this.state.isNotificationForOwnerPageEnabled,
  97. isNotificationForGroupPageEnabled: this.state.isNotificationForGroupPageEnabled,
  98. });
  99. return response;
  100. }
  101. /**
  102. * Delete global notification pattern
  103. */
  104. async deleteGlobalNotificationPattern(notificatiionId) {
  105. const response = await apiv3Delete(`/notification-setting/global-notification/${notificatiionId}`);
  106. const deletedNotificaton = response.data;
  107. await this.retrieveNotificationData();
  108. return deletedNotificaton;
  109. }
  110. }