GlobalNotificationList.jsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. {notification.triggerEvents.includes('pageCreate') && (
  81. <span className="badge badge-pill badge-success" data-toggle="tooltip" data-placement="top" title="Page Create">
  82. <i className="icon-doc"></i> CREATE
  83. </span>
  84. )}
  85. {notification.triggerEvents.includes('pageEdit') && (
  86. <span className="badge badge-pill badge-warning" data-toggle="tooltip" data-placement="top" title="Page Edit">
  87. <i className="icon-pencil"></i> EDIT
  88. </span>
  89. )}
  90. {notification.triggerEvents.includes('pageMove') && (
  91. <span className="badge badge-pill badge-warning" data-toggle="tooltip" data-placement="top" title="Page Move">
  92. <i className="icon-action-redo"></i> MOVE
  93. </span>
  94. )}
  95. {notification.triggerEvents.includes('pageDelete') && (
  96. <span className="badge badge-pill badge-danger" data-toggle="tooltip" data-placement="top" title="Page Delte">
  97. <i className="icon-fire"></i> DELETE
  98. </span>
  99. )}
  100. {notification.triggerEvents.includes('pageLike') && (
  101. <span className="badge badge-pill badge-info" data-toggle="tooltip" data-placement="top" title="Page Like">
  102. <i className="icon-like"></i> LIKE
  103. </span>
  104. )}
  105. {notification.triggerEvents.includes('comment') && (
  106. <span className="badge badge-pill badge-light" data-toggle="tooltip" data-placement="top" title="New Comment">
  107. <i className="icon-fw icon-bubble"></i> POST
  108. </span>
  109. )}
  110. </td>
  111. <td>
  112. {notification.__t === 'mail'
  113. && <span data-toggle="tooltip" data-placement="top" title="Email"><i className="ti-email"></i> {notification.toEmail}</span>}
  114. {notification.__t === 'slack'
  115. && <span data-toggle="tooltip" data-placement="top" title="Slack"><i className="fa fa-slack"></i> {notification.slackChannels}</span>}
  116. </td>
  117. <td className="td-abs-center">
  118. <div className="dropdown">
  119. <button
  120. className="btn btn-outline-secondary dropdown-toggle"
  121. type="button"
  122. id="dropdownMenuButton"
  123. data-toggle="dropdown"
  124. aria-haspopup="true"
  125. aria-expanded="false"
  126. >
  127. <i className="icon-settings"></i> <span className="caret"></span>
  128. </button>
  129. <div className="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuButton">
  130. <a className="dropdown-item" href={urljoin('/admin/global-notification/', notification._id)}>
  131. <i className="icon-fw icon-note"></i> {t('Edit')}
  132. </a>
  133. <a className="dropdown-item" onClick={() => this.openConfirmationModal(notification)}>
  134. <i className="icon-fw icon-fire text-danger"></i> {t('Delete')}
  135. </a>
  136. </div>
  137. </div>
  138. </td>
  139. </tr>
  140. );
  141. })}
  142. {this.state.notificationForConfiguration != null && (
  143. <NotificationDeleteModal
  144. isOpen={this.state.isConfirmationModalOpen}
  145. onClose={this.closeConfirmationModal}
  146. onClickSubmit={this.onClickSubmit}
  147. notificationForConfiguration={this.state.notificationForConfiguration}
  148. />
  149. )}
  150. </React.Fragment>
  151. );
  152. }
  153. }
  154. const GlobalNotificationListWrapper = (props) => {
  155. return createSubscribedElement(GlobalNotificationList, props, [AppContainer, AdminNotificationContainer]);
  156. };
  157. GlobalNotificationList.propTypes = {
  158. t: PropTypes.func.isRequired, // i18next
  159. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  160. adminNotificationContainer: PropTypes.instanceOf(AdminNotificationContainer).isRequired,
  161. };
  162. export default withTranslation()(GlobalNotificationListWrapper);