UserTriggerNotification.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import loggerFactory from '@alias/logger';
  5. import { createSubscribedElement } from '../../UnstatedUtils';
  6. import { toastSuccess, toastError } from '../../../util/apiNotification';
  7. import AppContainer from '../../../services/AppContainer';
  8. import AdminNotificationContainer from '../../../services/AdminNotificationContainer';
  9. import UserNotificationRow from './UserNotificationRow';
  10. const logger = loggerFactory('growi:slackAppConfiguration');
  11. class UserTriggerNotification extends React.Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. pathPattern: '',
  16. channel: '',
  17. };
  18. this.changePathPattern = this.changePathPattern.bind(this);
  19. this.changeChannel = this.changeChannel.bind(this);
  20. this.validateForm = this.validateForm.bind(this);
  21. this.onClickSubmit = this.onClickSubmit.bind(this);
  22. }
  23. /**
  24. * Change pathPattern
  25. */
  26. changePathPattern(pathPattern) {
  27. this.setState({ pathPattern });
  28. }
  29. /**
  30. * Change channel
  31. */
  32. changeChannel(channel) {
  33. this.setState({ channel });
  34. }
  35. validateForm() {
  36. return this.state.pathPattern !== '' && this.state.channel !== '';
  37. }
  38. async onClickSubmit() {
  39. const { t, adminNotificationContainer } = this.props;
  40. try {
  41. await adminNotificationContainer.addNotificationPattern(this.state.pathPattern, this.state.channel);
  42. toastSuccess(t('notification_setting.add_notification_pattern'));
  43. this.setState({ pathPattern: '', channel: '' });
  44. }
  45. catch (err) {
  46. toastError(err);
  47. logger.error(err);
  48. }
  49. }
  50. // TODO GW-788 i18n
  51. render() {
  52. const { t, adminNotificationContainer } = this.props;
  53. return (
  54. <React.Fragment>
  55. <h2 className="border-bottom mb-5">Default Notification Settings for Patterns</h2>
  56. <table className="table table-bordered">
  57. <thead>
  58. <tr>
  59. <th>Pattern</th>
  60. <th>Channel</th>
  61. <th>Operation</th>
  62. </tr>
  63. </thead>
  64. <tbody className="admin-notif-list">
  65. <tr>
  66. <td>
  67. <input
  68. className="form-control"
  69. type="text"
  70. name="pathPattern"
  71. value={this.state.pathPattern}
  72. placeholder="e.g. /projects/xxx/MTG/*"
  73. onChange={(e) => { this.changePathPattern(e.target.value) }}
  74. />
  75. <p className="help-block">
  76. Path name of wiki. Pattern expression with <code>*</code> can be used.
  77. </p>
  78. </td>
  79. <td>
  80. <input
  81. className="form-control form-inline"
  82. type="text"
  83. name="channel"
  84. value={this.state.channel}
  85. placeholder="e.g. project-xxx"
  86. onChange={(e) => { this.changeChannel(e.target.value) }}
  87. />
  88. <p className="help-block">
  89. Slack channel name. Without <code>#</code>.
  90. </p>
  91. </td>
  92. <td>
  93. <button type="button" className="btn btn-primary" disabled={!this.validateForm()} onClick={this.onClickSubmit}>{t('add')}</button>
  94. </td>
  95. </tr>
  96. {adminNotificationContainer.state.userNotifications.map((notification) => {
  97. return <UserNotificationRow notification={notification} />;
  98. })
  99. }
  100. </tbody>
  101. </table>
  102. </React.Fragment>
  103. );
  104. }
  105. }
  106. const UserTriggerNotificationWrapper = (props) => {
  107. return createSubscribedElement(UserTriggerNotification, props, [AppContainer, AdminNotificationContainer]);
  108. };
  109. UserTriggerNotification.propTypes = {
  110. t: PropTypes.func.isRequired, // i18next
  111. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  112. adminNotificationContainer: PropTypes.instanceOf(AdminNotificationContainer).isRequired,
  113. };
  114. export default withTranslation()(UserTriggerNotificationWrapper);