ManageGlobalNotification.jsx 11 KB

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