GlobalNotificationList.jsx 7.6 KB

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