| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- import { Container } from 'unstated';
- import loggerFactory from '@alias/logger';
- import { toastError } from '../util/apiNotification';
- const logger = loggerFactory('growi:services:AdminNotificationContainer');
- /**
- * Service container for admin Notification setting page (NotificationSetting.jsx)
- * @extends {Container} unstated Container
- */
- export default class AdminNotificationContainer extends Container {
- constructor(appContainer) {
- super();
- this.appContainer = appContainer;
- this.state = {
- retrieveError: null,
- selectSlackOption: 'incoming webhooks',
- webhookUrl: '',
- isIncomingWebhookPrioritized: false,
- slackToken: '',
- userNotifications: [],
- isNotificationForOwnerPageEnabled: false,
- isNotificationForGroupPageEnabled: false,
- globalNotifications: [],
- };
- }
- /**
- * Workaround for the mangling in production build to break constructor.name
- */
- static getClassName() {
- return 'AdminNotificationContainer';
- }
- /**
- * Retrieve notificationData
- */
- async retrieveNotificationData() {
- try {
- const response = await this.appContainer.apiv3.get('/notification-setting/');
- const { notificationParams } = response.data;
- this.setState({
- webhookUrl: notificationParams.webhookUrl,
- isIncomingWebhookPrioritized: notificationParams.isIncomingWebhookPrioritized,
- slackToken: notificationParams.slackToken,
- userNotifications: notificationParams.userNotifications,
- isNotificationForOwnerPageEnabled: notificationParams.isNotificationForOwnerPageEnabled,
- isNotificationForGroupPageEnabled: notificationParams.isNotificationForGroupPageEnabled,
- globalNotifications: notificationParams.globalNotifications,
- });
- }
- catch (err) {
- logger.error(err);
- toastError(new Error('Failed to fetch data'));
- }
- }
- /**
- * Switch slackOption
- */
- switchSlackOption(slackOption) {
- this.setState({ selectSlackOption: slackOption });
- }
- /**
- * Change webhookUrl
- */
- changeWebhookUrl(webhookUrl) {
- this.setState({ webhookUrl });
- }
- /**
- * Switch incomingWebhookPrioritized
- */
- switchIsIncomingWebhookPrioritized() {
- this.setState({ isIncomingWebhookPrioritized: !this.state.isIncomingWebhookPrioritized });
- }
- /**
- * Change slackToken
- */
- changeSlackToken(slackToken) {
- this.setState({ slackToken });
- }
- /**
- * Update slackAppConfiguration
- * @memberOf SlackAppConfiguration
- */
- async updateSlackAppConfiguration() {
- const response = await this.appContainer.apiv3.put('/notification-setting/slack-configuration', {
- webhookUrl: this.state.webhookUrl,
- isIncomingWebhookPrioritized: this.state.isIncomingWebhookPrioritized,
- slackToken: this.state.slackToken,
- });
- return response;
- }
- /**
- * Add notificationPattern
- * @memberOf SlackAppConfiguration
- */
- async addNotificationPattern(pathPattern, channel) {
- const response = await this.appContainer.apiv3.post('/notification-setting/user-notification', {
- pathPattern,
- channel,
- });
- this.setState({ userNotifications: response.data.responseParams.userNotifications });
- }
- /**
- * Delete user trigger notification pattern
- */
- async deleteUserTriggerNotificationPattern(notificatiionId) {
- const response = await this.appContainer.apiv3.delete(`/notification-setting/user-notification/${notificatiionId}`);
- const deletedNotificaton = response.data;
- await this.retrieveNotificationData();
- return deletedNotificaton;
- }
- /**
- * Switch isNotificationForOwnerPageEnabled
- */
- switchIsNotificationForOwnerPageEnabled() {
- this.setState({ isNotificationForOwnerPageEnabled: !this.state.isNotificationForOwnerPageEnabled });
- }
- /**
- * Switch isNotificationForGroupPageEnabled
- */
- switchIsNotificationForGroupPageEnabled() {
- this.setState({ isNotificationForGroupPageEnabled: !this.state.isNotificationForGroupPageEnabled });
- }
- /**
- * Update globalNotificationForPages
- * @memberOf SlackAppConfiguration
- */
- async updateGlobalNotificationForPages() {
- const response = await this.appContainer.apiv3.put('/notification-setting/notify-for-page-grant/', {
- isNotificationForOwnerPageEnabled: this.state.isNotificationForOwnerPageEnabled,
- isNotificationForGroupPageEnabled: this.state.isNotificationForGroupPageEnabled,
- });
- return response;
- }
- /**
- * Delete global notification pattern
- */
- async deleteGlobalNotificationPattern(notificatiionId) {
- const response = await this.appContainer.apiv3.delete(`/notification-setting/global-notification/${notificatiionId}`);
- const deletedNotificaton = response.data;
- await this.retrieveNotificationData();
- return deletedNotificaton;
- }
- }
|