UserTriggerNotification.jsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. const userNotifications = adminNotificationContainer.state.userNotifications || [];
  65. return (
  66. <React.Fragment>
  67. <h2 className="border-bottom my-4">{t('notification_setting.user_trigger_notification_header')}</h2>
  68. <table className="table table-bordered">
  69. <thead>
  70. <tr>
  71. <th>{t('notification_setting.pattern')}</th>
  72. <th>{t('notification_setting.channel')}</th>
  73. <th />
  74. </tr>
  75. </thead>
  76. <tbody className="admin-notif-list">
  77. <tr>
  78. <td>
  79. <input
  80. className="form-control"
  81. type="text"
  82. name="pathPattern"
  83. value={this.state.pathPattern}
  84. placeholder="e.g. /projects/xxx/MTG/*"
  85. onChange={(e) => { this.changePathPattern(e.target.value) }}
  86. />
  87. <p className="p-2 mb-0">
  88. {/* eslint-disable-next-line react/no-danger */}
  89. <span dangerouslySetInnerHTML={{ __html: t('notification_setting.pattern_desc') }} />
  90. </p>
  91. </td>
  92. <td>
  93. <div className="input-group notify-to-option" id="slack-input">
  94. <div className="input-group-prepend">
  95. <span className="input-group-text"><i className="fa fa-hashtag" /></span>
  96. </div>
  97. <input
  98. className="form-control form-inline"
  99. type="text"
  100. name="channel"
  101. value={this.state.channel}
  102. placeholder="e.g. project-xxx"
  103. onChange={(e) => { this.changeChannel(e.target.value) }}
  104. />
  105. </div>
  106. <p className="p-2 mb-0">
  107. {/* eslint-disable-next-line react/no-danger */}
  108. <span dangerouslySetInnerHTML={{ __html: t('notification_setting.channel_desc') }} />
  109. </p>
  110. </td>
  111. <td>
  112. <button type="button" className="btn btn-primary" disabled={!this.validateForm()} onClick={this.onClickSubmit}>{t('add')}</button>
  113. </td>
  114. </tr>
  115. {userNotifications.length > 0 && userNotifications.map((notification) => {
  116. return <UserNotificationRow notification={notification} onClickDeleteBtn={this.onClickDeleteBtn} key={notification._id} />;
  117. })}
  118. </tbody>
  119. </table>
  120. </React.Fragment>
  121. );
  122. }
  123. }
  124. const UserTriggerNotificationWrapper = (props) => {
  125. return createSubscribedElement(UserTriggerNotification, props, [AppContainer, AdminNotificationContainer]);
  126. };
  127. UserTriggerNotification.propTypes = {
  128. t: PropTypes.func.isRequired, // i18next
  129. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  130. adminNotificationContainer: PropTypes.instanceOf(AdminNotificationContainer).isRequired,
  131. };
  132. export default withTranslation()(UserTriggerNotificationWrapper);