AdminNotificationContainer.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.state = {
  11. retrieveError: null,
  12. isSlackbotConfigured: null,
  13. isSlackLegacyConfigured: null,
  14. currentBotType: null,
  15. userNotifications: [],
  16. isNotificationForOwnerPageEnabled: false,
  17. isNotificationForGroupPageEnabled: false,
  18. globalNotifications: [],
  19. };
  20. }
  21. /**
  22. * Workaround for the mangling in production build to break constructor.name
  23. */
  24. static getClassName() {
  25. return 'AdminNotificationContainer';
  26. }
  27. /**
  28. * Retrieve notificationData
  29. */
  30. async retrieveNotificationData() {
  31. const response = await this.appContainer.apiv3.get('/notification-setting/');
  32. const { notificationParams } = response.data;
  33. this.setState({
  34. isSlackbotConfigured: notificationParams.isSlackbotConfigured,
  35. isSlackLegacyConfigured: notificationParams.isSlackLegacyConfigured,
  36. currentBotType: notificationParams.currentBotType,
  37. userNotifications: notificationParams.userNotifications,
  38. isNotificationForOwnerPageEnabled: notificationParams.isNotificationForOwnerPageEnabled,
  39. isNotificationForGroupPageEnabled: notificationParams.isNotificationForGroupPageEnabled,
  40. globalNotifications: notificationParams.globalNotifications,
  41. });
  42. }
  43. /**
  44. * Update slackAppConfiguration
  45. * @memberOf SlackAppConfiguration
  46. */
  47. async updateSlackAppConfiguration() {
  48. const response = await this.appContainer.apiv3.put('/notification-setting/slack-configuration', {
  49. webhookUrl: this.state.webhookUrl,
  50. isIncomingWebhookPrioritized: this.state.isIncomingWebhookPrioritized,
  51. slackToken: this.state.slackToken,
  52. });
  53. return response;
  54. }
  55. /**
  56. * Add notificationPattern
  57. * @memberOf SlackAppConfiguration
  58. */
  59. async addNotificationPattern(pathPattern, channel) {
  60. const response = await this.appContainer.apiv3.post('/notification-setting/user-notification', {
  61. pathPattern,
  62. channel,
  63. });
  64. this.setState({ userNotifications: response.data.responseParams.userNotifications });
  65. }
  66. /**
  67. * Delete user trigger notification pattern
  68. */
  69. async deleteUserTriggerNotificationPattern(notificatiionId) {
  70. const response = await this.appContainer.apiv3.delete(`/notification-setting/user-notification/${notificatiionId}`);
  71. const deletedNotificaton = response.data;
  72. await this.retrieveNotificationData();
  73. return deletedNotificaton;
  74. }
  75. /**
  76. * Switch isNotificationForOwnerPageEnabled
  77. */
  78. switchIsNotificationForOwnerPageEnabled() {
  79. this.setState({ isNotificationForOwnerPageEnabled: !this.state.isNotificationForOwnerPageEnabled });
  80. }
  81. /**
  82. * Switch isNotificationForGroupPageEnabled
  83. */
  84. switchIsNotificationForGroupPageEnabled() {
  85. this.setState({ isNotificationForGroupPageEnabled: !this.state.isNotificationForGroupPageEnabled });
  86. }
  87. /**
  88. * Update globalNotificationForPages
  89. * @memberOf SlackAppConfiguration
  90. */
  91. async updateGlobalNotificationForPages() {
  92. const response = await this.appContainer.apiv3.put('/notification-setting/notify-for-page-grant/', {
  93. isNotificationForOwnerPageEnabled: this.state.isNotificationForOwnerPageEnabled,
  94. isNotificationForGroupPageEnabled: this.state.isNotificationForGroupPageEnabled,
  95. });
  96. return response;
  97. }
  98. /**
  99. * Delete global notification pattern
  100. */
  101. async deleteGlobalNotificationPattern(notificatiionId) {
  102. const response = await this.appContainer.apiv3.delete(`/notification-setting/global-notification/${notificatiionId}`);
  103. const deletedNotificaton = response.data;
  104. await this.retrieveNotificationData();
  105. return deletedNotificaton;
  106. }
  107. }