ManageGlobalNotification.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 AppContainer from '~/client/services/AppContainer';
  6. import { toastError } from '~/client/util/apiNotification';
  7. import { apiv3Post, apiv3Put } from '~/client/util/apiv3-client';
  8. import loggerFactory from '~/utils/logger';
  9. import { withUnstatedContainers } from '../../UnstatedUtils';
  10. import AdminUpdateButtonRow from '../Common/AdminUpdateButtonRow';
  11. import TriggerEventCheckBox from './TriggerEventCheckBox';
  12. const logger = loggerFactory('growi:manageGlobalNotification');
  13. class ManageGlobalNotification extends React.Component {
  14. constructor() {
  15. super();
  16. let globalNotification;
  17. try {
  18. globalNotification = JSON.parse(document.getElementById('admin-global-notification-setting').getAttribute('data-global-notification'));
  19. }
  20. catch (err) {
  21. toastError(err);
  22. logger.error(err);
  23. }
  24. this.state = {
  25. globalNotificationId: globalNotification._id || null,
  26. triggerPath: globalNotification.triggerPath || '',
  27. notifyToType: globalNotification.__t || 'mail',
  28. emailToSend: globalNotification.toEmail || '',
  29. slackChannelToSend: globalNotification.slackChannels || '',
  30. triggerEvents: new Set(globalNotification.triggerEvents),
  31. };
  32. this.submitHandler = this.submitHandler.bind(this);
  33. }
  34. onChangeTriggerPath(inputValue) {
  35. this.setState({ triggerPath: inputValue });
  36. }
  37. onChangeNotifyToType(notifyToType) {
  38. this.setState({ notifyToType });
  39. }
  40. onChangeEmailToSend(inputValue) {
  41. this.setState({ emailToSend: inputValue });
  42. }
  43. onChangeSlackChannelToSend(inputValue) {
  44. this.setState({ slackChannelToSend: inputValue });
  45. }
  46. onChangeTriggerEvents(triggerEvent) {
  47. const { triggerEvents } = this.state;
  48. if (triggerEvents.has(triggerEvent)) {
  49. triggerEvents.delete(triggerEvent);
  50. this.setState({ triggerEvents });
  51. }
  52. else {
  53. triggerEvents.add(triggerEvent);
  54. this.setState({ triggerEvents });
  55. }
  56. }
  57. async submitHandler() {
  58. const requestParams = {
  59. triggerPath: this.state.triggerPath,
  60. notifyToType: this.state.notifyToType,
  61. toEmail: this.state.emailToSend,
  62. slackChannels: this.state.slackChannelToSend,
  63. triggerEvents: [...this.state.triggerEvents],
  64. };
  65. try {
  66. if (this.state.globalNotificationId != null) {
  67. await apiv3Put(`/notification-setting/global-notification/${this.state.globalNotificationId}`, requestParams);
  68. }
  69. else {
  70. await apiv3Post('/notification-setting/global-notification', requestParams);
  71. }
  72. window.location.href = urljoin(window.location.origin, '/admin/notification#global-notification');
  73. }
  74. catch (err) {
  75. toastError(err);
  76. logger.error(err);
  77. }
  78. }
  79. render() {
  80. const { t, appContainer } = this.props;
  81. const { isMailerSetup } = appContainer.config;
  82. return (
  83. <React.Fragment>
  84. <div className="my-3">
  85. <a href="/admin/notification#global-notification" className="btn btn-outline-secondary">
  86. <i className="icon-fw ti-arrow-left" aria-hidden="true"></i>
  87. {t('notification_setting.back_to_list')}
  88. </a>
  89. </div>
  90. <div className="row">
  91. <div className="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. <>
  143. <div className="input-group notify-to-option" id="mail-input">
  144. <div className="input-group-prepend">
  145. <span className="input-group-text" id="mail-addon"><i className="ti-email" /></span>
  146. </div>
  147. <input
  148. className="form-control"
  149. type="text"
  150. aria-describedby="mail-addon"
  151. name="toEmail"
  152. placeholder="Email"
  153. value={this.state.emailToSend}
  154. onChange={(e) => { this.onChangeEmailToSend(e.target.value) }}
  155. />
  156. </div>
  157. <p className="p-2">
  158. {/* eslint-disable-next-line react/no-danger */}
  159. {!isMailerSetup && <span className="form-text text-muted" dangerouslySetInnerHTML={{ __html: t('admin:mailer_setup_required') }} />}
  160. <b>Hint: </b>
  161. <a href="https://ifttt.com/create" target="blank">{t('notification_setting.email.ifttt_link')}
  162. <i className="icon-share-alt" />
  163. </a>
  164. </p>
  165. </>
  166. )
  167. : (
  168. <>
  169. <div className="input-group notify-to-option" id="slack-input">
  170. <div className="input-group-prepend">
  171. <span className="input-group-text" id="slack-channel-addon"><i className="fa fa-hashtag" /></span>
  172. </div>
  173. <input
  174. className="form-control"
  175. type="text"
  176. aria-describedby="slack-channel-addon"
  177. name="notificationGlobal[slackChannels]"
  178. placeholder="Slack Channel"
  179. value={this.state.slackChannelToSend}
  180. onChange={(e) => { this.onChangeSlackChannelToSend(e.target.value) }}
  181. />
  182. </div>
  183. <p className="p-2">
  184. {/* eslint-disable-next-line react/no-danger */}
  185. <span dangerouslySetInnerHTML={{ __html: t('notification_setting.channel_desc') }} />
  186. </p>
  187. </>
  188. )}
  189. </div>
  190. <div className="offset-1 col-sm-5">
  191. <div className="form-group">
  192. <h3>{t('notification_setting.trigger_events')}</h3>
  193. <div className="my-1">
  194. <TriggerEventCheckBox
  195. checkbox="success"
  196. event="pageCreate"
  197. checked={this.state.triggerEvents.has('pageCreate')}
  198. onChange={() => this.onChangeTriggerEvents('pageCreate')}
  199. >
  200. <span className="badge badge-pill badge-success">
  201. <i className="icon-doc mr-1" /> CREATE
  202. </span>
  203. </TriggerEventCheckBox>
  204. </div>
  205. <div className="my-1">
  206. <TriggerEventCheckBox
  207. checkbox="warning"
  208. event="pageEdit"
  209. checked={this.state.triggerEvents.has('pageEdit')}
  210. onChange={() => this.onChangeTriggerEvents('pageEdit')}
  211. >
  212. <span className="badge badge-pill badge-warning">
  213. <i className="icon-pencil mr-1" />EDIT
  214. </span>
  215. </TriggerEventCheckBox>
  216. </div>
  217. <div className="my-1">
  218. <TriggerEventCheckBox
  219. checkbox="pink"
  220. event="pageMove"
  221. checked={this.state.triggerEvents.has('pageMove')}
  222. onChange={() => this.onChangeTriggerEvents('pageMove')}
  223. >
  224. <span className="badge badge-pill badge-pink">
  225. <i className="icon-action-redo mr-1" />MOVE
  226. </span>
  227. </TriggerEventCheckBox>
  228. </div>
  229. <div className="my-1">
  230. <TriggerEventCheckBox
  231. checkbox="danger"
  232. event="pageDelete"
  233. checked={this.state.triggerEvents.has('pageDelete')}
  234. onChange={() => this.onChangeTriggerEvents('pageDelete')}
  235. >
  236. <span className="badge badge-pill badge-danger">
  237. <i className="icon-fire mr-1" />DELETE
  238. </span>
  239. </TriggerEventCheckBox>
  240. </div>
  241. <div className="my-1">
  242. <TriggerEventCheckBox
  243. checkbox="info"
  244. event="pageLike"
  245. checked={this.state.triggerEvents.has('pageLike')}
  246. onChange={() => this.onChangeTriggerEvents('pageLike')}
  247. >
  248. <span className="badge badge-pill badge-info">
  249. <i className="fa fa-heart-o mr-1" />LIKE
  250. </span>
  251. </TriggerEventCheckBox>
  252. </div>
  253. <div className="my-1">
  254. <TriggerEventCheckBox
  255. checkbox="secondary"
  256. event="comment"
  257. checked={this.state.triggerEvents.has('comment')}
  258. onChange={() => this.onChangeTriggerEvents('comment')}
  259. >
  260. <span className="badge badge-pill badge-secondary">
  261. <i className="icon-bubble mr-1" />POST
  262. </span>
  263. </TriggerEventCheckBox>
  264. </div>
  265. </div>
  266. </div>
  267. </div>
  268. <AdminUpdateButtonRow
  269. onClick={this.submitHandler}
  270. disabled={this.state.retrieveError != null}
  271. />
  272. </React.Fragment>
  273. );
  274. }
  275. }
  276. const ManageGlobalNotificationWrapper = withUnstatedContainers(ManageGlobalNotification, [AppContainer]);
  277. ManageGlobalNotification.propTypes = {
  278. t: PropTypes.func.isRequired, // i18next
  279. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  280. };
  281. export default withTranslation()(ManageGlobalNotificationWrapper);