AdminNotificationContainer.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. grobalNotifications: [],
  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. });
  41. }
  42. catch (err) {
  43. logger.error(err);
  44. toastError(new Error('Failed to fetch data'));
  45. }
  46. }
  47. /**
  48. * Switch slackOption
  49. */
  50. switchSlackOption(slackOption) {
  51. this.setState({ selectSlackOption: slackOption });
  52. }
  53. /**
  54. * Change webhookUrl
  55. */
  56. changeWebhookUrl(webhookUrl) {
  57. this.setState({ webhookUrl });
  58. }
  59. /**
  60. * Switch incomingWebhookPrioritized
  61. */
  62. switchIsIncomingWebhookPrioritized() {
  63. this.setState({ isIncomingWebhookPrioritized: !this.state.isIncomingWebhookPrioritized });
  64. }
  65. /**
  66. * Change slackToken
  67. */
  68. changeSlackToken(slackToken) {
  69. this.setState({ slackToken });
  70. }
  71. /**
  72. * Update slackAppConfiguration
  73. * @memberOf SlackAppConfiguration
  74. */
  75. async updateSlackAppConfiguration() {
  76. const response = await this.appContainer.apiv3.put('/notification-setting/slack-configuration', {
  77. webhookUrl: this.state.webhookUrl,
  78. isIncomingWebhookPrioritized: this.state.isIncomingWebhookPrioritized,
  79. slackToken: this.state.slackToken,
  80. });
  81. return response;
  82. }
  83. /**
  84. * Add notificationPattern
  85. * @memberOf SlackAppConfiguration
  86. */
  87. async addNotificationPattern(pathPattern, channel) {
  88. return this.appContainer.apiv3.post('/notification-setting/user-notification', {
  89. pathPattern,
  90. channel,
  91. });
  92. }
  93. }