ManageGlobalNotification.jsx 12 KB

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