NotificationSetting.jsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. href="#slack-configuration"
  52. >
  53. <i className="icon-settings"></i> Slack configuration
  54. </NavLink>
  55. </NavItem>
  56. <NavItem>
  57. <NavLink
  58. className={`${activeTab === 'user-trigger-notification' && 'active'} `}
  59. onClick={() => { this.toggleActiveTab('user-trigger-notification') }}
  60. href="#user-trigger-notification"
  61. >
  62. <i className="icon-settings"></i> User trigger notification
  63. </NavLink>
  64. </NavItem>
  65. <NavItem>
  66. <NavLink
  67. className={`${activeTab === 'global-notification' && 'active'} `}
  68. onClick={() => { this.toggleActiveTab('global-notification') }}
  69. href="#global-notification"
  70. >
  71. <i className="icon-settings"></i> Global notification
  72. </NavLink>
  73. </NavItem>
  74. </Nav>
  75. <TabContent activeTab={activeTab}>
  76. <TabPane tabId="slack-configuration">
  77. {activeComponents.has('slack-configuration') && <SlackAppConfiguration />}
  78. </TabPane>
  79. <TabPane tabId="user-trigger-notification">
  80. {activeComponents.has('user-trigger-notification') && <UserTriggerNotification />}
  81. </TabPane>
  82. <TabPane tabId="global-notification">
  83. {activeComponents.has('global-notification') && <GlobalNotification />}
  84. </TabPane>
  85. </TabContent>
  86. </React.Fragment>
  87. );
  88. }
  89. }
  90. const NotificationSettingWrapper = (props) => {
  91. return createSubscribedElement(NotificationSetting, props, [AppContainer, AdminNotificationContainer]);
  92. };
  93. NotificationSetting.propTypes = {
  94. t: PropTypes.func.isRequired, // i18next
  95. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  96. adminNotificationContainer: PropTypes.instanceOf(AdminNotificationContainer).isRequired,
  97. };
  98. export default withTranslation()(NotificationSettingWrapper);