GlobalNotificationList.jsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import React from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import PropTypes from 'prop-types';
  4. import urljoin from 'url-join';
  5. import AdminNotificationContainer from '~/client/services/AdminNotificationContainer';
  6. import { apiv3Put } from '~/client/util/apiv3-client';
  7. import { toastSuccess, toastError } from '~/client/util/toastr';
  8. import loggerFactory from '~/utils/logger';
  9. import { withUnstatedContainers } from '../../UnstatedUtils';
  10. import NotificationDeleteModal from './NotificationDeleteModal';
  11. import { NotificationTypeIcon } from './NotificationTypeIcon';
  12. const logger = loggerFactory('growi:GolobalNotificationList');
  13. class GlobalNotificationList extends React.Component {
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. isConfirmationModalOpen: false,
  18. notificationForConfiguration: null,
  19. };
  20. this.openConfirmationModal = this.openConfirmationModal.bind(this);
  21. this.closeConfirmationModal = this.closeConfirmationModal.bind(this);
  22. this.onClickSubmit = this.onClickSubmit.bind(this);
  23. }
  24. async toggleIsEnabled(notification) {
  25. const { t } = this.props;
  26. const isEnabled = !notification.isEnabled;
  27. try {
  28. await apiv3Put(`/notification-setting/global-notification/${notification._id}/enabled`, {
  29. isEnabled,
  30. });
  31. toastSuccess(t('notification_settings.toggle_notification', { path: notification.triggerPath }));
  32. await this.props.adminNotificationContainer.retrieveNotificationData();
  33. }
  34. catch (err) {
  35. toastError(err);
  36. logger.error(err);
  37. }
  38. }
  39. openConfirmationModal(notification) {
  40. this.setState({ isConfirmationModalOpen: true, notificationForConfiguration: notification });
  41. }
  42. closeConfirmationModal() {
  43. this.setState({ isConfirmationModalOpen: false, notificationForConfiguration: null });
  44. }
  45. async onClickSubmit() {
  46. const { t, adminNotificationContainer } = this.props;
  47. try {
  48. const deletedNotificaton = await adminNotificationContainer.deleteGlobalNotificationPattern(this.state.notificationForConfiguration._id);
  49. toastSuccess(t('notification_settings.delete_notification_pattern', { path: deletedNotificaton.triggerPath }));
  50. }
  51. catch (err) {
  52. toastError(err);
  53. logger.error(err);
  54. }
  55. this.setState({ isConfirmationModalOpen: false });
  56. }
  57. render() {
  58. const { t, adminNotificationContainer } = this.props;
  59. const { globalNotifications } = adminNotificationContainer.state;
  60. return (
  61. <React.Fragment>
  62. {globalNotifications.map((notification) => {
  63. return (
  64. <tr key={notification._id}>
  65. <td className="align-middle td-abs-center">
  66. <div className="form-check form-switch form-check-success">
  67. <input
  68. type="checkbox"
  69. className="form-check-input"
  70. id={notification._id}
  71. defaultChecked={notification.isEnabled}
  72. onClick={() => this.toggleIsEnabled(notification)}
  73. />
  74. <label className="form-label form-check-label" htmlFor={notification._id} />
  75. </div>
  76. </td>
  77. <td>
  78. {notification.triggerPath}
  79. </td>
  80. <td>
  81. <ul className="list-inline mb-0">
  82. {notification.triggerEvents.includes('pageCreate') && (
  83. <li className="list-inline-item badge rounded-pill bg-success">
  84. <span className=" material-symbols-outlined">description</span> CREATE
  85. </li>
  86. )}
  87. {notification.triggerEvents.includes('pageEdit') && (
  88. <li className="list-inline-item badge rounded-pill bg-warning text-dark">
  89. <span className="material-symbols-outlined">edit</span> EDIT
  90. </li>
  91. )}
  92. {notification.triggerEvents.includes('pageMove') && (
  93. <li className="list-inline-item badge rounded-pill bg-pink">
  94. <span className="material-symbols-outlined">redo</span> MOVE
  95. </li>
  96. )}
  97. {notification.triggerEvents.includes('pageDelete') && (
  98. <li className="list-inline-item badge rounded-pill bg-danger">
  99. <span className="material-symbols-outlined">delete_forever</span>DELETE
  100. </li>
  101. )}
  102. {notification.triggerEvents.includes('pageLike') && (
  103. <li className="list-inline-item badge rounded-pill bg-info">
  104. <span className="material-symbols-outlined">favorite</span> LIKE
  105. </li>
  106. )}
  107. {notification.triggerEvents.includes('comment') && (
  108. <li className="list-inline-item badge rounded-pill bg-primary">
  109. <span className="material-symbols-outlined">bubble_chart</span> POST
  110. </li>
  111. )}
  112. </ul>
  113. </td>
  114. <td>
  115. <NotificationTypeIcon notification={notification} />
  116. { notification.__t === 'mail' && notification.toEmail }
  117. { notification.__t === 'slack' && notification.slackChannels }
  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-bs-toggle="dropdown"
  126. aria-haspopup="true"
  127. aria-expanded="false"
  128. >
  129. <span className="material-symbols-outlined">settings</span> <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. <span className="material-symbols-outlined">note</span> {t('Edit')}
  134. </a>
  135. <button className="dropdown-item" type="button" onClick={() => this.openConfirmationModal(notification)}>
  136. <span className="material-symbols-outlined text-danger">delete_forever</span> {t('Delete')}
  137. </button>
  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. GlobalNotificationList.propTypes = {
  157. t: PropTypes.func.isRequired, // i18next
  158. adminNotificationContainer: PropTypes.instanceOf(AdminNotificationContainer).isRequired,
  159. };
  160. const GlobalNotificationListWrapperFC = (props) => {
  161. const { t } = useTranslation('admin');
  162. return <GlobalNotificationList t={t} {...props} />;
  163. };
  164. const GlobalNotificationListWrapper = withUnstatedContainers(GlobalNotificationListWrapperFC, [AdminNotificationContainer]);
  165. export default GlobalNotificationListWrapper;