UserTriggerNotification.jsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. this.onClickDeleteBtn = this.onClickDeleteBtn.bind(this);
  23. }
  24. /**
  25. * Change pathPattern
  26. */
  27. changePathPattern(pathPattern) {
  28. this.setState({ pathPattern });
  29. }
  30. /**
  31. * Change channel
  32. */
  33. changeChannel(channel) {
  34. this.setState({ channel });
  35. }
  36. validateForm() {
  37. return this.state.pathPattern !== '' && this.state.channel !== '';
  38. }
  39. async onClickSubmit() {
  40. const { t, adminNotificationContainer } = this.props;
  41. try {
  42. await adminNotificationContainer.addNotificationPattern(this.state.pathPattern, this.state.channel);
  43. toastSuccess(t('notification_setting.add_notification_pattern'));
  44. this.setState({ pathPattern: '', channel: '' });
  45. }
  46. catch (err) {
  47. toastError(err);
  48. logger.error(err);
  49. }
  50. }
  51. async onClickDeleteBtn(notificationIdForDelete) {
  52. const { t, adminNotificationContainer } = this.props;
  53. try {
  54. const deletedNotificaton = await adminNotificationContainer.deleteUserTriggerNotificationPattern(notificationIdForDelete);
  55. toastSuccess(t('notification_setting.delete_notification_pattern', { path: deletedNotificaton.pathPattern }));
  56. }
  57. catch (err) {
  58. toastError(err);
  59. logger.error(err);
  60. }
  61. }
  62. render() {
  63. const { t, adminNotificationContainer } = this.props;
  64. return (
  65. <React.Fragment>
  66. <h2 className="border-bottom mb-5">{t('notification_setting.user_trigger_notification_header')}</h2>
  67. <table className="table table-bordered">
  68. <thead>
  69. <tr>
  70. <th>{t('notification_setting.pattern')}</th>
  71. <th>{t('notification_setting.channel')}</th>
  72. <th />
  73. </tr>
  74. </thead>
  75. <tbody className="admin-notif-list">
  76. <tr>
  77. <td>
  78. <input
  79. className="form-control"
  80. type="text"
  81. name="pathPattern"
  82. value={this.state.pathPattern}
  83. placeholder="e.g. /projects/xxx/MTG/*"
  84. onChange={(e) => { this.changePathPattern(e.target.value) }}
  85. />
  86. {/* eslint-disable-next-line react/no-danger */}
  87. <p className="help-block" dangerouslySetInnerHTML={{ __html: t('notification_setting.pattern_desc') }} />
  88. </td>
  89. <td>
  90. <input
  91. className="form-control form-inline"
  92. type="text"
  93. name="channel"
  94. value={this.state.channel}
  95. placeholder="e.g. project-xxx"
  96. onChange={(e) => { this.changeChannel(e.target.value) }}
  97. />
  98. {/* eslint-disable-next-line react/no-danger */}
  99. <p className="help-block" dangerouslySetInnerHTML={{ __html: t('notification_setting.channel_desc') }} />
  100. </td>
  101. <td>
  102. <button type="button" className="btn btn-primary" disabled={!this.validateForm()} onClick={this.onClickSubmit}>{t('add')}</button>
  103. </td>
  104. </tr>
  105. {adminNotificationContainer.state.userNotifications.map((notification) => {
  106. return <UserNotificationRow notification={notification} onClickDeleteBtn={this.onClickDeleteBtn} key={notification._id} />;
  107. })
  108. }
  109. </tbody>
  110. </table>
  111. </React.Fragment>
  112. );
  113. }
  114. }
  115. const UserTriggerNotificationWrapper = (props) => {
  116. return createSubscribedElement(UserTriggerNotification, props, [AppContainer, AdminNotificationContainer]);
  117. };
  118. UserTriggerNotification.propTypes = {
  119. t: PropTypes.func.isRequired, // i18next
  120. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  121. adminNotificationContainer: PropTypes.instanceOf(AdminNotificationContainer).isRequired,
  122. };
  123. export default withTranslation()(UserTriggerNotificationWrapper);