ManageGlobalNotification.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. import React, { useCallback, useState } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import PropTypes from 'prop-types';
  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. const ManageGlobalNotification = (props) => {
  14. // constructor() {
  15. // super();
  16. // const globalNotification = null;
  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. // globalNotificationId: null,
  27. // // triggerPath: globalNotification.triggerPath || '',
  28. // triggerPath: '',
  29. // // notifyToType: globalNotification.__t || 'mail',
  30. // notifyToType: 'mail',
  31. // // emailToSend: globalNotification.toEmail || '',
  32. // emailToSend: '',
  33. // // slackChannelToSend: globalNotification.slackChannels || '',
  34. // slackChannelToSend: '',
  35. // // triggerEvents: new Set(globalNotification.triggerEvents),
  36. // triggerEvents: new Set(),
  37. // };
  38. // this.submitHandler = this.submitHandler.bind(this);
  39. // }
  40. const [globalNotificationId, setGlobalNotificationId] = useState(null);
  41. const [triggerPath, setTriggerPath] = useState('');
  42. const [notifyToType, setNotifyToType] = useState('mail');
  43. const [emailToSend, setEmailToSend] = useState('');
  44. const [slacChannelToSend, setSlackChannelToSend] = useState('');
  45. const [triggerEvents, setTriggerEvents] = useState(new Set());
  46. const [retrieveError] = useState(null);
  47. // onChangeTriggerPath(inputValue) {
  48. // this.setState({ triggerPath: inputValue });
  49. // }
  50. // onChangeNotifyToType(notifyToType) {
  51. // this.setState({ notifyToType });
  52. // }
  53. // onChangeEmailToSend(inputValue) {
  54. // this.setState({ emailToSend: inputValue });
  55. // }
  56. // onChangeSlackChannelToSend(inputValue) {
  57. // this.setState({ slackChannelToSend: inputValue });
  58. // }
  59. const onChangeTriggerEvents = (triggerEvent) => {
  60. if (triggerEvents.has(triggerEvent)) {
  61. triggerEvents.delete(triggerEvent);
  62. this.setState({ triggerEvents });
  63. }
  64. else {
  65. triggerEvents.add(triggerEvent);
  66. this.setState({ triggerEvents });
  67. }
  68. };
  69. const submitHandler = useCallback(async() => {
  70. const requestParams = {
  71. triggerPath,
  72. notifyToType,
  73. emailToSend,
  74. slacChannelToSend,
  75. triggerEvents,
  76. };
  77. try {
  78. if (globalNotificationId != null) {
  79. await apiv3Put(`/notification-setting/global-notification/${globalNotificationId}`, requestParams);
  80. }
  81. else {
  82. await apiv3Post('/notification-setting/global-notification', requestParams);
  83. }
  84. window.location.href = urljoin(window.location.origin, '/admin/notification#global-notification');
  85. }
  86. catch (err) {
  87. // toastError(err);
  88. logger.error(err);
  89. }
  90. }, []);
  91. const { isMailerSetup } = props;
  92. const { t } = useTranslation('admin');
  93. return (
  94. <>
  95. <div className="my-3">
  96. <a href="/admin/notification#global-notification" className="btn btn-outline-secondary">
  97. <i className="icon-fw ti-arrow-left" aria-hidden="true"></i>
  98. {t('notification_settings.back_to_list')}
  99. </a>
  100. </div>
  101. <div className="row">
  102. <div className="form-box col-md-12">
  103. <h2 className="border-bottom mb-5">{t('notification_settings.notification_detail')}</h2>
  104. </div>
  105. <div className="col-sm-4">
  106. <h3 htmlFor="triggerPath">{t('notification_settings.trigger_path')}
  107. {/* eslint-disable-next-line react/no-danger */}
  108. <small dangerouslySetInnerHTML={{ __html: t('notification_settings.trigger_path_help', '<code>*</code>') }} />
  109. </h3>
  110. <div className="form-group">
  111. <input
  112. className="form-control"
  113. type="text"
  114. name="triggerPath"
  115. value={triggerPath}
  116. onChange={(e) => { setTriggerPath(e.target.value) }}
  117. required
  118. />
  119. </div>
  120. <h3>{t('notification_settings.notify_to')}</h3>
  121. <div className="form-group form-inline">
  122. <div className="custom-control custom-radio">
  123. <input
  124. className="custom-control-input"
  125. type="radio"
  126. id="mail"
  127. name="notifyToType"
  128. value="mail"
  129. checked={notifyToType === 'mail'}
  130. onChange={() => { setNotifyToType('mail') }}
  131. />
  132. <label className="custom-control-label" htmlFor="mail">
  133. <p className="font-weight-bold">Email</p>
  134. </label>
  135. </div>
  136. <div className="custom-control custom-radio ml-2">
  137. <input
  138. className="custom-control-input"
  139. type="radio"
  140. id="slack"
  141. name="notifyToType"
  142. value="slack"
  143. checked={notifyToType === 'slack'}
  144. onChange={() => { this.onChangeNotifyToType('slack') }}
  145. />
  146. <label className="custom-control-label" htmlFor="slack">
  147. <p className="font-weight-bold">Slack</p>
  148. </label>
  149. </div>
  150. </div>
  151. {notifyToType === 'mail'
  152. ? (
  153. <>
  154. <div className="input-group notify-to-option" id="mail-input">
  155. <div className="input-group-prepend">
  156. <span className="input-group-text" id="mail-addon"><i className="ti ti-email" /></span>
  157. </div>
  158. <input
  159. className="form-control"
  160. type="text"
  161. aria-describedby="mail-addon"
  162. name="toEmail"
  163. placeholder="Email"
  164. value={emailToSend}
  165. onChange={(e) => { setEmailToSend(e.target.value) }}
  166. />
  167. </div>
  168. <p className="p-2">
  169. {/* eslint-disable-next-line react/no-danger */}
  170. {!isMailerSetup && <span className="form-text text-muted" dangerouslySetInnerHTML={{ __html: t('admin:mailer_setup_required') }} />}
  171. <b>Hint: </b>
  172. <a href="https://ifttt.com/create" target="blank">{t('notification_settings.email.ifttt_link')}
  173. <i className="icon-share-alt" />
  174. </a>
  175. </p>
  176. </>
  177. )
  178. : (
  179. <>
  180. <div className="input-group notify-to-option" id="slack-input">
  181. <div className="input-group-prepend">
  182. <span className="input-group-text" id="slack-channel-addon"><i className="fa fa-hashtag" /></span>
  183. </div>
  184. <input
  185. className="form-control"
  186. type="text"
  187. aria-describedby="slack-channel-addon"
  188. name="notificationGlobal[slackChannels]"
  189. placeholder="Slack Channel"
  190. value={slacChannelToSend}
  191. onChange={(e) => { setSlackChannelToSend(e.target.value) }}
  192. />
  193. </div>
  194. <p className="p-2">
  195. {/* eslint-disable-next-line react/no-danger */}
  196. <span dangerouslySetInnerHTML={{ __html: t('notification_settings.channel_desc') }} />
  197. </p>
  198. </>
  199. )}
  200. </div>
  201. <div className="offset-1 col-sm-5">
  202. <div className="form-group">
  203. <h3>{t('notification_settings.trigger_events')}</h3>
  204. <div className="my-1">
  205. <TriggerEventCheckBox
  206. checkbox="success"
  207. event="pageCreate"
  208. checked={triggerEvents.has('pageCreate')}
  209. onChange={() => onChangeTriggerEvents('pageCreate')}
  210. >
  211. <span className="badge badge-pill badge-success">
  212. <i className="icon-doc mr-1" /> CREATE
  213. </span>
  214. </TriggerEventCheckBox>
  215. </div>
  216. <div className="my-1">
  217. <TriggerEventCheckBox
  218. checkbox="warning"
  219. event="pageEdit"
  220. checked={triggerEvents.has('pageEdit')}
  221. onChange={() => this.onChangeTriggerEvents('pageEdit')}
  222. >
  223. <span className="badge badge-pill badge-warning">
  224. <i className="icon-pencil mr-1" />EDIT
  225. </span>
  226. </TriggerEventCheckBox>
  227. </div>
  228. <div className="my-1">
  229. <TriggerEventCheckBox
  230. checkbox="pink"
  231. event="pageMove"
  232. checked={triggerEvents.has('pageMove')}
  233. onChange={() => this.onChangeTriggerEvents('pageMove')}
  234. >
  235. <span className="badge badge-pill badge-pink">
  236. <i className="icon-action-redo mr-1" />MOVE
  237. </span>
  238. </TriggerEventCheckBox>
  239. </div>
  240. <div className="my-1">
  241. <TriggerEventCheckBox
  242. checkbox="danger"
  243. event="pageDelete"
  244. checked={triggerEvents.has('pageDelete')}
  245. onChange={() => this.onChangeTriggerEvents('pageDelete')}
  246. >
  247. <span className="badge badge-pill badge-danger">
  248. <i className="icon-fire mr-1" />DELETE
  249. </span>
  250. </TriggerEventCheckBox>
  251. </div>
  252. <div className="my-1">
  253. <TriggerEventCheckBox
  254. checkbox="info"
  255. event="pageLike"
  256. checked={triggerEvents.has('pageLike')}
  257. onChange={() => this.onChangeTriggerEvents('pageLike')}
  258. >
  259. <span className="badge badge-pill badge-info">
  260. <i className="fa fa-heart-o mr-1" />LIKE
  261. </span>
  262. </TriggerEventCheckBox>
  263. </div>
  264. <div className="my-1">
  265. <TriggerEventCheckBox
  266. checkbox="secondary"
  267. event="comment"
  268. checked={triggerEvents.has('comment')}
  269. onChange={() => this.onChangeTriggerEvents('comment')}
  270. >
  271. <span className="badge badge-pill badge-secondary">
  272. <i className="icon-bubble mr-1" />POST
  273. </span>
  274. </TriggerEventCheckBox>
  275. </div>
  276. </div>
  277. </div>
  278. </div>
  279. <AdminUpdateButtonRow
  280. onClick={submitHandler}
  281. disabled={retrieveError != null}
  282. />
  283. </>
  284. );
  285. };
  286. ManageGlobalNotification.propTypes = {
  287. isMailerSetup: PropTypes.bool,
  288. };
  289. // const ManageGlobalNotificationWrapperFC = (props) => {
  290. // const { t } = useTranslation();
  291. // return <ManageGlobalNotification t={t} {...props} />;
  292. // };
  293. // const ManageGlobalNotificationWrapper = withUnstatedContainers(ManageGlobalNotificationWrapperFC, [AppContainer]);
  294. export default ManageGlobalNotification;