GlobalNotificationList.jsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import urljoin from 'url-join';
  5. import AdminNotificationContainer from '~/client/services/AdminNotificationContainer';
  6. import AppContainer from '~/client/services/AppContainer';
  7. import { toastSuccess, toastError } from '~/client/util/apiNotification';
  8. import { apiv3Put } from '~/client/util/apiv3-client';
  9. import loggerFactory from '~/utils/logger';
  10. import { withUnstatedContainers } from '../../UnstatedUtils';
  11. import NotificationDeleteModal from './NotificationDeleteModal';
  12. import NotificationTypeIcon from './NotificationTypeIcon';
  13. const logger = loggerFactory('growi:GolobalNotificationList');
  14. class GlobalNotificationList extends React.Component {
  15. constructor(props) {
  16. super(props);
  17. this.state = {
  18. isConfirmationModalOpen: false,
  19. notificationForConfiguration: null,
  20. };
  21. this.openConfirmationModal = this.openConfirmationModal.bind(this);
  22. this.closeConfirmationModal = this.closeConfirmationModal.bind(this);
  23. this.onClickSubmit = this.onClickSubmit.bind(this);
  24. }
  25. async toggleIsEnabled(notification) {
  26. const { t } = this.props;
  27. const isEnabled = !notification.isEnabled;
  28. try {
  29. await apiv3Put(`/notification-setting/global-notification/${notification._id}/enabled`, {
  30. isEnabled,
  31. });
  32. toastSuccess(t('notification_setting.toggle_notification', { path: notification.triggerPath }));
  33. await this.props.adminNotificationContainer.retrieveNotificationData();
  34. }
  35. catch (err) {
  36. toastError(err);
  37. logger.error(err);
  38. }
  39. }
  40. openConfirmationModal(notification) {
  41. this.setState({ isConfirmationModalOpen: true, notificationForConfiguration: notification });
  42. }
  43. closeConfirmationModal() {
  44. this.setState({ isConfirmationModalOpen: false, notificationForConfiguration: null });
  45. }
  46. async onClickSubmit() {
  47. const { t, adminNotificationContainer } = this.props;
  48. try {
  49. const deletedNotificaton = await adminNotificationContainer.deleteGlobalNotificationPattern(this.state.notificationForConfiguration._id);
  50. toastSuccess(t('notification_setting.delete_notification_pattern', { path: deletedNotificaton.triggerPath }));
  51. }
  52. catch (err) {
  53. toastError(err);
  54. logger.error(err);
  55. }
  56. this.setState({ isConfirmationModalOpen: false });
  57. }
  58. render() {
  59. const { t, adminNotificationContainer } = this.props;
  60. const { globalNotifications } = adminNotificationContainer.state;
  61. return (
  62. <React.Fragment>
  63. {globalNotifications.map((notification) => {
  64. return (
  65. <tr key={notification._id}>
  66. <td className="align-middle td-abs-center">
  67. <div className="custom-control custom-switch custom-checkbox-success">
  68. <input
  69. type="checkbox"
  70. className="custom-control-input"
  71. id={notification._id}
  72. defaultChecked={notification.isEnabled}
  73. onClick={() => this.toggleIsEnabled(notification)}
  74. />
  75. <label className="custom-control-label" htmlFor={notification._id} />
  76. </div>
  77. </td>
  78. <td>
  79. {notification.triggerPath}
  80. </td>
  81. <td>
  82. <ul className="list-inline mb-0">
  83. {notification.triggerEvents.includes('pageCreate') && (
  84. <li className="list-inline-item badge badge-pill badge-success">
  85. <i className="icon-doc"></i> CREATE
  86. </li>
  87. )}
  88. {notification.triggerEvents.includes('pageEdit') && (
  89. <li className="list-inline-item badge badge-pill badge-warning">
  90. <i className="icon-pencil"></i> EDIT
  91. </li>
  92. )}
  93. {notification.triggerEvents.includes('pageMove') && (
  94. <li className="list-inline-item badge badge-pill badge-pink">
  95. <i className="icon-action-redo"></i> MOVE
  96. </li>
  97. )}
  98. {notification.triggerEvents.includes('pageDelete') && (
  99. <li className="list-inline-item badge badge-pill badge-danger">
  100. <i className="icon-fire"></i> DELETE
  101. </li>
  102. )}
  103. {notification.triggerEvents.includes('pageLike') && (
  104. <li className="list-inline-item badge badge-pill badge-info">
  105. <i className="fa fa-heart-o"></i> LIKE
  106. </li>
  107. )}
  108. {notification.triggerEvents.includes('comment') && (
  109. <li className="list-inline-item badge badge-pill badge-secondary">
  110. <i className="icon-fw icon-bubble"></i> POST
  111. </li>
  112. )}
  113. </ul>
  114. </td>
  115. <td>
  116. <NotificationTypeIcon notification={notification} />
  117. { notification.__t === 'mail' && notification.toEmail }
  118. { notification.__t === 'slack' && notification.slackChannels }
  119. </td>
  120. <td className="td-abs-center">
  121. <div className="dropdown">
  122. <button
  123. className="btn btn-outline-secondary dropdown-toggle"
  124. type="button"
  125. id="dropdownMenuButton"
  126. data-toggle="dropdown"
  127. aria-haspopup="true"
  128. aria-expanded="false"
  129. >
  130. <i className="icon-settings"></i> <span className="caret"></span>
  131. </button>
  132. <div className="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuButton">
  133. <a className="dropdown-item" href={urljoin('/admin/global-notification/', notification._id)}>
  134. <i className="icon-fw icon-note"></i> {t('Edit')}
  135. </a>
  136. <button className="dropdown-item" type="button" onClick={() => this.openConfirmationModal(notification)}>
  137. <i className="icon-fw icon-fire text-danger"></i> {t('Delete')}
  138. </button>
  139. </div>
  140. </div>
  141. </td>
  142. </tr>
  143. );
  144. })}
  145. {this.state.notificationForConfiguration != null && (
  146. <NotificationDeleteModal
  147. isOpen={this.state.isConfirmationModalOpen}
  148. onClose={this.closeConfirmationModal}
  149. onClickSubmit={this.onClickSubmit}
  150. notificationForConfiguration={this.state.notificationForConfiguration}
  151. />
  152. )}
  153. </React.Fragment>
  154. );
  155. }
  156. }
  157. const GlobalNotificationListWrapper = withUnstatedContainers(GlobalNotificationList, [AppContainer, AdminNotificationContainer]);
  158. GlobalNotificationList.propTypes = {
  159. t: PropTypes.func.isRequired, // i18next
  160. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  161. adminNotificationContainer: PropTypes.instanceOf(AdminNotificationContainer).isRequired,
  162. };
  163. export default withTranslation()(GlobalNotificationListWrapper);