ManageGlobalNotification.jsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. <a href="/admin/notification#global-notification" className="btn btn-default">
  83. <i className="icon-fw ti-arrow-left" aria-hidden="true"></i>
  84. {t('notification_setting.back_to_list')}
  85. </a>
  86. <div className="row">
  87. <div className="m-t-20 form-box col-md-12">
  88. <h2 className="border-bottom mb-5">{t('notification_setting.notification_detail')}</h2>
  89. </div>
  90. <div className="col-sm-4">
  91. <div className="form-group">
  92. <h3 htmlFor="triggerPath">{t('notification_setting.trigger_path')}
  93. {/* eslint-disable-next-line react/no-danger */}
  94. <small dangerouslySetInnerHTML={{ __html: t('notification_setting.trigger_path_help', '<code>*</code>') }} />
  95. <input
  96. className="form-control"
  97. type="text"
  98. name="triggerPath"
  99. value={this.state.triggerPath}
  100. onChange={(e) => { this.onChangeTriggerPath(e.target.value) }}
  101. required
  102. />
  103. </h3>
  104. </div>
  105. <div className="form-group form-inline">
  106. <h3>{t('notification_setting.notify_to')}</h3>
  107. <div className="radio radio-primary">
  108. <input
  109. type="radio"
  110. id="mail"
  111. name="notifyToType"
  112. value="mail"
  113. checked={this.state.notifyToType === 'mail'}
  114. onChange={() => { this.onChangeNotifyToType('mail') }}
  115. />
  116. <label htmlFor="mail">
  117. <p className="font-weight-bold">Email</p>
  118. </label>
  119. </div>
  120. <div className="radio radio-primary">
  121. <input
  122. type="radio"
  123. id="slack"
  124. name="notifyToType"
  125. value="slack"
  126. checked={this.state.notifyToType === 'slack'}
  127. onChange={() => { this.onChangeNotifyToType('slack') }}
  128. />
  129. <label htmlFor="slack">
  130. <p className="font-weight-bold">Slack</p>
  131. </label>
  132. </div>
  133. </div>
  134. {this.state.notifyToType === 'mail'
  135. ? (
  136. <div className="form-group notify-to-option" id="mail-input">
  137. <input
  138. className="form-control"
  139. type="text"
  140. name="toEmail"
  141. placeholder="Email"
  142. value={this.state.emailToSend}
  143. onChange={(e) => { this.onChangeEmailToSend(e.target.value) }}
  144. />
  145. <p className="help">
  146. <b>Hint: </b>
  147. <a href="https://ifttt.com/create" target="blank">{t('notification_setting.email.ifttt_link')}
  148. <i className="icon-share-alt" />
  149. </a>
  150. </p>
  151. </div>
  152. )
  153. : (
  154. <div className="form-group notify-to-option" id="slack-input">
  155. <input
  156. className="form-control"
  157. type="text"
  158. name="notificationGlobal[slackChannels]"
  159. placeholder="Slack Channel"
  160. value={this.state.slackChannelToSend}
  161. onChange={(e) => { this.onChangeSlackChannelToSend(e.target.value) }}
  162. />
  163. </div>
  164. )}
  165. </div>
  166. <div className="col-sm-offset-1 col-sm-5">
  167. <div className="form-group">
  168. <h3>{t('notification_setting.trigger_events')}</h3>
  169. <TriggerEventCheckBox
  170. event="pageCreate"
  171. checked={this.state.triggerEvents.has('pageCreate')}
  172. onChange={() => this.onChangeTriggerEvents('pageCreate')}
  173. >
  174. <span className="label label-success">
  175. <i className="icon-doc"></i> CREATE
  176. </span>
  177. </TriggerEventCheckBox>
  178. <TriggerEventCheckBox
  179. event="pageEdit"
  180. checked={this.state.triggerEvents.has('pageEdit')}
  181. onChange={() => this.onChangeTriggerEvents('pageEdit')}
  182. >
  183. <span className="label label-warning">
  184. <i className="icon-pencil"></i>EDIT
  185. </span>
  186. </TriggerEventCheckBox>
  187. <TriggerEventCheckBox
  188. event="pageMove"
  189. checked={this.state.triggerEvents.has('pageMove')}
  190. onChange={() => this.onChangeTriggerEvents('pageMove')}
  191. >
  192. <span className="label label-warning">
  193. <i className="icon-action-redo"></i>MOVE
  194. </span>
  195. </TriggerEventCheckBox>
  196. <TriggerEventCheckBox
  197. event="pageDelete"
  198. checked={this.state.triggerEvents.has('pageDelete')}
  199. onChange={() => this.onChangeTriggerEvents('pageDelete')}
  200. >
  201. <span className="label label-danger">
  202. <i className="icon-fire"></i>DELETE
  203. </span>
  204. </TriggerEventCheckBox>
  205. <TriggerEventCheckBox
  206. event="pageLike"
  207. checked={this.state.triggerEvents.has('pageLike')}
  208. onChange={() => this.onChangeTriggerEvents('pageLike')}
  209. >
  210. <span className="label label-info">
  211. <i className="icon-like"></i>LIKE
  212. </span>
  213. </TriggerEventCheckBox>
  214. <TriggerEventCheckBox
  215. event="comment"
  216. checked={this.state.triggerEvents.has('comment')}
  217. onChange={() => this.onChangeTriggerEvents('comment')}
  218. >
  219. <span className="label label-default">
  220. <i className="icon-bubble"></i>POST
  221. </span>
  222. </TriggerEventCheckBox>
  223. </div>
  224. </div>
  225. <AdminUpdateButtonRow
  226. onClick={this.submitHandler}
  227. disabled={this.state.retrieveError != null}
  228. />
  229. </div>
  230. </React.Fragment>
  231. );
  232. }
  233. }
  234. const ManageGlobalNotificationWrapper = (props) => {
  235. return createSubscribedElement(ManageGlobalNotification, props, [AppContainer]);
  236. };
  237. ManageGlobalNotification.propTypes = {
  238. t: PropTypes.func.isRequired, // i18next
  239. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  240. };
  241. export default withTranslation()(ManageGlobalNotificationWrapper);