NotificationSetting.jsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import {
  5. TabContent, TabPane, Nav, NavItem, NavLink,
  6. } from 'reactstrap';
  7. import loggerFactory from '@alias/logger';
  8. import { createSubscribedElement } from '../../UnstatedUtils';
  9. import { toastError } from '../../../util/apiNotification';
  10. import AppContainer from '../../../services/AppContainer';
  11. import AdminNotificationContainer from '../../../services/AdminNotificationContainer';
  12. import SlackAppConfiguration from './SlackAppConfiguration';
  13. import UserTriggerNotification from './UserTriggerNotification';
  14. import GlobalNotification from './GlobalNotification';
  15. const logger = loggerFactory('growi:NotificationSetting');
  16. class NotificationSetting extends React.Component {
  17. constructor() {
  18. super();
  19. this.state = {
  20. activeTab: 'slack-configuration',
  21. // Prevent unnecessary rendering
  22. activeComponents: new Set(['slack-configuration']),
  23. };
  24. this.toggleActiveTab = this.toggleActiveTab.bind(this);
  25. }
  26. async componentDidMount() {
  27. const { adminNotificationContainer } = this.props;
  28. try {
  29. await adminNotificationContainer.retrieveNotificationData();
  30. }
  31. catch (err) {
  32. toastError(err);
  33. adminNotificationContainer.setState({ retrieveError: err });
  34. logger.error(err);
  35. }
  36. }
  37. toggleActiveTab(activeTab) {
  38. this.setState({
  39. activeTab, activeComponents: this.state.activeComponents.add(activeTab),
  40. });
  41. }
  42. render() {
  43. const { activeTab, activeComponents } = this.state;
  44. return (
  45. <React.Fragment>
  46. <Nav tabs>
  47. <NavItem>
  48. <NavLink
  49. className={`${activeTab === 'slack-configuration' && 'active'} `}
  50. onClick={() => { this.toggleActiveTab('slack-configuration') }}
  51. >
  52. <i className="icon-settings"></i> Slack Configuration
  53. </NavLink>
  54. </NavItem>
  55. <NavItem>
  56. <NavLink
  57. className={`${activeTab === 'user-trigger-notification' && 'active'} `}
  58. onClick={() => { this.toggleActiveTab('user-trigger-notification') }}
  59. >
  60. <i className="icon-settings"></i> User Trigger Notification
  61. </NavLink>
  62. </NavItem>
  63. <NavItem>
  64. <NavLink
  65. className={`${activeTab === 'global-notification' && 'active'} `}
  66. onClick={() => { this.toggleActiveTab('global-notification') }}
  67. >
  68. <i className="icon-settings"></i> Global Notification
  69. </NavLink>
  70. </NavItem>
  71. </Nav>
  72. <TabContent activeTab={activeTab}>
  73. <TabPane tabId="slack-configuration">
  74. {activeComponents.has('slack-configuration') && <SlackAppConfiguration />}
  75. </TabPane>
  76. <TabPane tabId="user-trigger-notification">
  77. {activeComponents.has('user-trigger-notification') && <UserTriggerNotification />}
  78. </TabPane>
  79. <TabPane tabId="global-notification">
  80. {activeComponents.has('global-notification') && <GlobalNotification />}
  81. </TabPane>
  82. </TabContent>
  83. </React.Fragment>
  84. );
  85. }
  86. }
  87. const NotificationSettingWrapper = (props) => {
  88. return createSubscribedElement(NotificationSetting, props, [AppContainer, AdminNotificationContainer]);
  89. };
  90. NotificationSetting.propTypes = {
  91. t: PropTypes.func.isRequired, // i18next
  92. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  93. adminNotificationContainer: PropTypes.instanceOf(AdminNotificationContainer).isRequired,
  94. };
  95. export default withTranslation()(NotificationSettingWrapper);