ManageGlobalNotification.jsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 { toastError } from '../../../util/apiNotification';
  7. import TriggerEventCheckBox from './TriggerEventCheckBox';
  8. import AdminUpdateButtonRow from '../Common/AdminUpdateButtonRow';
  9. import AppContainer from '../../../services/AppContainer';
  10. import { createSubscribedElement } from '../../UnstatedUtils';
  11. const logger = loggerFactory('growi:manageGlobalNotification');
  12. class ManageGlobalNotification extends React.Component {
  13. constructor() {
  14. super();
  15. let globalNotification;
  16. try {
  17. globalNotification = JSON.parse(document.getElementById('admin-global-notification-setting').getAttribute('data-global-notification'));
  18. }
  19. catch (err) {
  20. toastError(err);
  21. logger.error(err);
  22. }
  23. this.state = {
  24. globalNotificationId: globalNotification._id || null,
  25. triggerPath: globalNotification.triggerPath || '',
  26. notifyToType: globalNotification.__t || 'mail',
  27. emailToSend: globalNotification.toEmail || '',
  28. slackChannelToSend: globalNotification.slackChannels || '',
  29. triggerEvents: new Set(globalNotification.triggerEvents),
  30. };
  31. this.submitHandler = this.submitHandler.bind(this);
  32. }
  33. onChangeTriggerPath(inputValue) {
  34. this.setState({ triggerPath: inputValue });
  35. }
  36. onChangeNotifyToType(notifyToType) {
  37. this.setState({ notifyToType });
  38. }
  39. onChangeEmailToSend(inputValue) {
  40. this.setState({ emailToSend: inputValue });
  41. }
  42. onChangeSlackChannelToSend(inputValue) {
  43. this.setState({ slackChannelToSend: inputValue });
  44. }
  45. onChangeTriggerEvents(triggerEvent) {
  46. const { triggerEvents } = this.state;
  47. if (triggerEvents.has(triggerEvent)) {
  48. triggerEvents.delete(triggerEvent);
  49. this.setState({ triggerEvents });
  50. }
  51. else {
  52. triggerEvents.add(triggerEvent);
  53. this.setState({ triggerEvents });
  54. }
  55. }
  56. async submitHandler() {
  57. const requestParams = {
  58. triggerPath: this.state.triggerPath,
  59. notifyToType: this.state.notifyToType,
  60. toEmail: this.state.emailToSend,
  61. slackChannels: this.state.slackChannelToSend,
  62. triggerEvents: [...this.state.triggerEvents],
  63. };
  64. try {
  65. if (this.state.globalNotificationId != null) {
  66. await this.props.appContainer.apiv3.put(`/notification-setting/global-notification/${this.state.globalNotificationId}`, requestParams);
  67. }
  68. else {
  69. await this.props.appContainer.apiv3.post('/notification-setting/global-notification', requestParams);
  70. }
  71. window.location.href = urljoin(window.location.origin, '/admin/notification#global-notification');
  72. }
  73. catch (err) {
  74. toastError(err);
  75. logger.error(err);
  76. }
  77. }
  78. render() {
  79. const { t } = this.props;
  80. return (
  81. <React.Fragment>
  82. <div className="my-3">
  83. <a href="/admin/notification#global-notification">
  84. <button type="button" className="btn page-link text-dark d-inline-block">
  85. <i className="icon-fw ti-arrow-left" aria-hidden="true"></i>
  86. {t('notification_setting.back_to_list')}
  87. </button>
  88. </a>
  89. </div>
  90. <div className="row">
  91. <div className="m-t-20 form-box col-md-12">
  92. <h2 className="border-bottom mb-5">{t('notification_setting.notification_detail')}</h2>
  93. </div>
  94. <div className="col-sm-4">
  95. <h3 htmlFor="triggerPath">{t('notification_setting.trigger_path')}
  96. {/* eslint-disable-next-line react/no-danger */}
  97. <small dangerouslySetInnerHTML={{ __html: t('notification_setting.trigger_path_help', '<code>*</code>') }} />
  98. </h3>
  99. <div className="form-group">
  100. <input
  101. className="form-control"
  102. type="text"
  103. name="triggerPath"
  104. value={this.state.triggerPath}
  105. onChange={(e) => { this.onChangeTriggerPath(e.target.value) }}
  106. required
  107. />
  108. </div>
  109. <h3>{t('notification_setting.notify_to')}</h3>
  110. <div className="form-group form-inline">
  111. <div className="custom-control custom-radio">
  112. <input
  113. className="custom-control-input"
  114. type="radio"
  115. id="mail"
  116. name="notifyToType"
  117. value="mail"
  118. checked={this.state.notifyToType === 'mail'}
  119. onChange={() => { this.onChangeNotifyToType('mail') }}
  120. />
  121. <label className="custom-control-label" htmlFor="mail">
  122. <p className="font-weight-bold">Email</p>
  123. </label>
  124. </div>
  125. <div className="custom-control custom-radio ml-2">
  126. <input
  127. className="custom-control-input"
  128. type="radio"
  129. id="slack"
  130. name="notifyToType"
  131. value="slack"
  132. checked={this.state.notifyToType === 'slack'}
  133. onChange={() => { this.onChangeNotifyToType('slack') }}
  134. />
  135. <label className="custom-control-label" htmlFor="slack">
  136. <p className="font-weight-bold">Slack</p>
  137. </label>
  138. </div>
  139. </div>
  140. {this.state.notifyToType === 'mail'
  141. ? (
  142. <div className="form-group notify-to-option" id="mail-input">
  143. <input
  144. className="form-control"
  145. type="text"
  146. name="toEmail"
  147. placeholder="Email"
  148. value={this.state.emailToSend}
  149. onChange={(e) => { this.onChangeEmailToSend(e.target.value) }}
  150. />
  151. <p className="help">
  152. <b>Hint: </b>
  153. <a href="https://ifttt.com/create" target="blank">{t('notification_setting.email.ifttt_link')}
  154. <i className="icon-share-alt" />
  155. </a>
  156. </p>
  157. </div>
  158. )
  159. : (
  160. <div className="form-group notify-to-option" id="slack-input">
  161. <input
  162. className="form-control"
  163. type="text"
  164. name="notificationGlobal[slackChannels]"
  165. placeholder="Slack Channel"
  166. value={this.state.slackChannelToSend}
  167. onChange={(e) => { this.onChangeSlackChannelToSend(e.target.value) }}
  168. />
  169. </div>
  170. )}
  171. </div>
  172. <div className="offset-1 col-sm-5">
  173. <div className="form-group">
  174. <h3>{t('notification_setting.trigger_events')}</h3>
  175. <TriggerEventCheckBox
  176. event="pageCreate"
  177. checked={this.state.triggerEvents.has('pageCreate')}
  178. onChange={() => this.onChangeTriggerEvents('pageCreate')}
  179. >
  180. <span className="badge badge-pill badge-success">
  181. <i className="icon-doc"></i> CREATE
  182. </span>
  183. </TriggerEventCheckBox>
  184. <TriggerEventCheckBox
  185. event="pageEdit"
  186. checked={this.state.triggerEvents.has('pageEdit')}
  187. onChange={() => this.onChangeTriggerEvents('pageEdit')}
  188. >
  189. <span className="badge badge-pill badge-warning">
  190. <i className="icon-pencil"></i>EDIT
  191. </span>
  192. </TriggerEventCheckBox>
  193. <TriggerEventCheckBox
  194. event="pageMove"
  195. checked={this.state.triggerEvents.has('pageMove')}
  196. onChange={() => this.onChangeTriggerEvents('pageMove')}
  197. >
  198. <span className="badge badge-pill badge-warning">
  199. <i className="icon-action-redo"></i>MOVE
  200. </span>
  201. </TriggerEventCheckBox>
  202. <TriggerEventCheckBox
  203. event="pageDelete"
  204. checked={this.state.triggerEvents.has('pageDelete')}
  205. onChange={() => this.onChangeTriggerEvents('pageDelete')}
  206. >
  207. <span className="badge badge-pill badge-danger">
  208. <i className="icon-fire"></i>DELETE
  209. </span>
  210. </TriggerEventCheckBox>
  211. <TriggerEventCheckBox
  212. event="pageLike"
  213. checked={this.state.triggerEvents.has('pageLike')}
  214. onChange={() => this.onChangeTriggerEvents('pageLike')}
  215. >
  216. <span className="badge badge-pill badge-info">
  217. <i className="icon-like"></i>LIKE
  218. </span>
  219. </TriggerEventCheckBox>
  220. <TriggerEventCheckBox
  221. event="comment"
  222. checked={this.state.triggerEvents.has('comment')}
  223. onChange={() => this.onChangeTriggerEvents('comment')}
  224. >
  225. <span className="badge badge-pill badge-light">
  226. <i className="icon-bubble"></i>POST
  227. </span>
  228. </TriggerEventCheckBox>
  229. </div>
  230. </div>
  231. </div>
  232. <AdminUpdateButtonRow
  233. onClick={this.submitHandler}
  234. disabled={this.state.retrieveError != null}
  235. />
  236. </React.Fragment>
  237. );
  238. }
  239. }
  240. const ManageGlobalNotificationWrapper = (props) => {
  241. return createSubscribedElement(ManageGlobalNotification, props, [AppContainer]);
  242. };
  243. ManageGlobalNotification.propTypes = {
  244. t: PropTypes.func.isRequired, // i18next
  245. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  246. };
  247. export default withTranslation()(ManageGlobalNotificationWrapper);