ManageGlobalNotification.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. <>
  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. <b>Hint: </b>
  159. <a href="https://ifttt.com/create" target="blank">{t('notification_setting.email.ifttt_link')}
  160. <i className="icon-share-alt" />
  161. </a>
  162. </p>
  163. </>
  164. )
  165. : (
  166. <>
  167. <div className="input-group notify-to-option" id="slack-input">
  168. <div className="input-group-prepend">
  169. <span className="input-group-text" id="slack-channel-addon"><i className="fa fa-slack" /></span>
  170. </div>
  171. <input
  172. className="form-control"
  173. type="text"
  174. aria-describedby="slack-channel-addon"
  175. name="notificationGlobal[slackChannels]"
  176. placeholder="Slack Channel"
  177. value={this.state.slackChannelToSend}
  178. onChange={(e) => { this.onChangeSlackChannelToSend(e.target.value) }}
  179. />
  180. </div>
  181. <p className="p-2">
  182. {/* eslint-disable-next-line react/no-danger */}
  183. <span dangerouslySetInnerHTML={{ __html: t('notification_setting.channel_desc') }} />
  184. </p>
  185. </>
  186. )}
  187. </div>
  188. <div className="offset-1 col-sm-5">
  189. <div className="form-group">
  190. <h3>{t('notification_setting.trigger_events')}</h3>
  191. <div className="my-1">
  192. <TriggerEventCheckBox
  193. checkbox="success"
  194. event="pageCreate"
  195. checked={this.state.triggerEvents.has('pageCreate')}
  196. onChange={() => this.onChangeTriggerEvents('pageCreate')}
  197. >
  198. <span className="badge badge-pill badge-success">
  199. <i className="icon-doc mr-1" /> CREATE
  200. </span>
  201. </TriggerEventCheckBox>
  202. </div>
  203. <div className="my-1">
  204. <TriggerEventCheckBox
  205. checkbox="warning"
  206. event="pageEdit"
  207. checked={this.state.triggerEvents.has('pageEdit')}
  208. onChange={() => this.onChangeTriggerEvents('pageEdit')}
  209. >
  210. <span className="badge badge-pill badge-warning">
  211. <i className="icon-pencil mr-1" />EDIT
  212. </span>
  213. </TriggerEventCheckBox>
  214. </div>
  215. <div className="my-1">
  216. <TriggerEventCheckBox
  217. checkbox="warning"
  218. event="pageMove"
  219. checked={this.state.triggerEvents.has('pageMove')}
  220. onChange={() => this.onChangeTriggerEvents('pageMove')}
  221. >
  222. <span className="badge badge-pill badge-warning">
  223. <i className="icon-action-redo mr-1" />MOVE
  224. </span>
  225. </TriggerEventCheckBox>
  226. </div>
  227. <div className="my-1">
  228. <TriggerEventCheckBox
  229. checkbox="danger"
  230. event="pageDelete"
  231. checked={this.state.triggerEvents.has('pageDelete')}
  232. onChange={() => this.onChangeTriggerEvents('pageDelete')}
  233. >
  234. <span className="badge badge-pill badge-danger">
  235. <i className="icon-fire mr-1" />DELETE
  236. </span>
  237. </TriggerEventCheckBox>
  238. </div>
  239. <div className="my-1">
  240. <TriggerEventCheckBox
  241. checkbox="info"
  242. event="pageLike"
  243. checked={this.state.triggerEvents.has('pageLike')}
  244. onChange={() => this.onChangeTriggerEvents('pageLike')}
  245. >
  246. <span className="badge badge-pill badge-info">
  247. <i className="icon-like mr-1" />LIKE
  248. </span>
  249. </TriggerEventCheckBox>
  250. </div>
  251. <div className="my-1">
  252. <TriggerEventCheckBox
  253. checkbox="secondary"
  254. event="comment"
  255. checked={this.state.triggerEvents.has('comment')}
  256. onChange={() => this.onChangeTriggerEvents('comment')}
  257. >
  258. <span className="badge badge-pill badge-secondary">
  259. <i className="icon-bubble mr-1" />POST
  260. </span>
  261. </TriggerEventCheckBox>
  262. </div>
  263. </div>
  264. </div>
  265. </div>
  266. <AdminUpdateButtonRow
  267. onClick={this.submitHandler}
  268. disabled={this.state.retrieveError != null}
  269. />
  270. </React.Fragment>
  271. );
  272. }
  273. }
  274. const ManageGlobalNotificationWrapper = (props) => {
  275. return createSubscribedElement(ManageGlobalNotification, props, [AppContainer]);
  276. };
  277. ManageGlobalNotification.propTypes = {
  278. t: PropTypes.func.isRequired, // i18next
  279. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  280. };
  281. export default withTranslation()(ManageGlobalNotificationWrapper);