AdminNotificationContainer.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. globalNotifications: [],
  21. };
  22. }
  23. /**
  24. * Workaround for the mangling in production build to break constructor.name
  25. */
  26. static getClassName() {
  27. return 'AdminNotificationContainer';
  28. }
  29. /**
  30. * Retrieve notificationData
  31. */
  32. async retrieveNotificationData() {
  33. try {
  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 || false,
  39. slackToken: notificationParams.slackToken || '',
  40. userNotifications: notificationParams.userNotifications || [],
  41. globalNotifications: notificationParams.globalNotifications || [],
  42. });
  43. }
  44. catch (err) {
  45. logger.error(err);
  46. toastError(new Error('Failed to fetch data'));
  47. }
  48. }
  49. /**
  50. * Switch slackOption
  51. */
  52. switchSlackOption(slackOption) {
  53. this.setState({ selectSlackOption: slackOption });
  54. }
  55. /**
  56. * Change webhookUrl
  57. */
  58. changeWebhookUrl(webhookUrl) {
  59. this.setState({ webhookUrl });
  60. }
  61. /**
  62. * Switch incomingWebhookPrioritized
  63. */
  64. switchIsIncomingWebhookPrioritized() {
  65. this.setState({ isIncomingWebhookPrioritized: !this.state.isIncomingWebhookPrioritized });
  66. }
  67. /**
  68. * Change slackToken
  69. */
  70. changeSlackToken(slackToken) {
  71. this.setState({ slackToken });
  72. }
  73. /**
  74. * Update slackAppConfiguration
  75. * @memberOf SlackAppConfiguration
  76. */
  77. async updateSlackAppConfiguration() {
  78. const response = await this.appContainer.apiv3.put('/notification-setting/slack-configuration', {
  79. webhookUrl: this.state.webhookUrl,
  80. isIncomingWebhookPrioritized: this.state.isIncomingWebhookPrioritized,
  81. slackToken: this.state.slackToken,
  82. });
  83. return response;
  84. }
  85. /**
  86. * Add notificationPattern
  87. * @memberOf SlackAppConfiguration
  88. */
  89. async addNotificationPattern(pathPattern, channel) {
  90. const response = await this.appContainer.apiv3.post('/notification-setting/user-notification', {
  91. pathPattern,
  92. channel,
  93. });
  94. this.setState({ userNotifications: response.data.responseParams.userNotifications });
  95. }
  96. /**
  97. * Delete user trigger notification pattern
  98. */
  99. async deleteUserTriggerNotificationPattern(notificatiionId) {
  100. const response = await this.appContainer.apiv3.delete(`/notification-setting/user-notification/${notificatiionId}`);
  101. const deletedNotificaton = response.data;
  102. await this.retrieveNotificationData();
  103. return deletedNotificaton;
  104. }
  105. /**
  106. * Delete global notification pattern
  107. */
  108. async deleteGlobalNotificationPattern(notificatiionId) {
  109. const response = await this.appContainer.apiv3.delete(`/notification-setting/global-notification/${notificatiionId}`);
  110. const deletedNotificaton = response.data;
  111. await this.retrieveNotificationData();
  112. return deletedNotificaton;
  113. }
  114. }