ManageGlobalNotification.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. router.push('/admin/notification');
  71. // await apiv3Put(`/notification-setting/global-notification/${globalNotificationId}`, requestParams);
  72. }
  73. else {
  74. await apiv3Post('/notification-setting/global-notification', requestParams);
  75. router.push('/admin/notification');
  76. }
  77. }
  78. catch (err) {
  79. toastError(err);
  80. logger.error(err);
  81. }
  82. }, [emailToSend, globalNotification, notifyType, router, slackChannelToSend, triggerEvents, triggerPath, updateGlobalNotification]);
  83. const { data: isMailerSetup } = useIsMailerSetup();
  84. const { adminNotificationContainer } = props;
  85. const { t } = useTranslation('admin');
  86. return (
  87. <>
  88. <div className="my-3">
  89. <a href="/admin/notification#global-notification" className="btn btn-outline-secondary">
  90. <i className="icon-fw ti-arrow-left" aria-hidden="true"></i>
  91. {t('notification_settings.back_to_list')}
  92. </a>
  93. </div>
  94. <div className="row">
  95. <div className="form-box col-md-12">
  96. <h2 className="border-bottom mb-5">{t('notification_settings.notification_detail')}</h2>
  97. </div>
  98. <div className="col-sm-4">
  99. <h3 htmlFor="triggerPath">{t('notification_settings.trigger_path')}
  100. {/* eslint-disable-next-line react/no-danger */}
  101. <small dangerouslySetInnerHTML={{ __html: t('notification_settings.trigger_path_help', '<code>*</code>') }} />
  102. </h3>
  103. <div className="form-group">
  104. <input
  105. className="form-control"
  106. type="text"
  107. name="triggerPath"
  108. value={triggerPath}
  109. onChange={(e) => { setTriggerPath(e.target.value) }}
  110. required
  111. />
  112. </div>
  113. <h3>{t('notification_settings.notify_to')}</h3>
  114. <div className="form-group form-inline">
  115. <div className="custom-control custom-radio">
  116. <input
  117. className="custom-control-input"
  118. type="radio"
  119. id="mail"
  120. name="notifyType"
  121. value="mail"
  122. checked={notifyType === 'mail'}
  123. onChange={() => { setNotifyType('mail') }}
  124. />
  125. <label className="custom-control-label" htmlFor="mail">
  126. <p className="font-weight-bold">Email</p>
  127. </label>
  128. </div>
  129. <div className="custom-control custom-radio ml-2">
  130. <input
  131. className="custom-control-input"
  132. type="radio"
  133. id="slack"
  134. name="notifyType"
  135. value="slack"
  136. checked={notifyType === 'slack'}
  137. onChange={() => { setNotifyType('slack') }}
  138. />
  139. <label className="custom-control-label" htmlFor="slack">
  140. <p className="font-weight-bold">Slack</p>
  141. </label>
  142. </div>
  143. </div>
  144. {notifyType === 'mail'
  145. ? (
  146. <>
  147. <div className="input-group notify-to-option" id="mail-input">
  148. <div className="input-group-prepend">
  149. <span className="input-group-text" id="mail-addon"><i className="ti ti-email" /></span>
  150. </div>
  151. <input
  152. className="form-control"
  153. type="text"
  154. aria-describedby="mail-addon"
  155. name="toEmail"
  156. placeholder="Email"
  157. value={emailToSend}
  158. onChange={(e) => { setEmailToSend(e.target.value) }}
  159. />
  160. </div>
  161. <p className="p-2">
  162. {/* eslint-disable-next-line react/no-danger */}
  163. {!isMailerSetup && <span className="form-text text-muted" dangerouslySetInnerHTML={{ __html: t('admin:mailer_setup_required') }} />}
  164. <b>Hint: </b>
  165. <a href="https://ifttt.com/create" target="blank">{t('notification_settings.email.ifttt_link')}
  166. <i className="icon-share-alt" />
  167. </a>
  168. </p>
  169. </>
  170. )
  171. : (
  172. <>
  173. <div className="input-group notify-to-option" id="slack-input">
  174. <div className="input-group-prepend">
  175. <span className="input-group-text" id="slack-channel-addon"><i className="fa fa-hashtag" /></span>
  176. </div>
  177. <input
  178. className="form-control"
  179. type="text"
  180. aria-describedby="slack-channel-addon"
  181. name="notificationGlobal[slackChannels]"
  182. placeholder="Slack Channel"
  183. value={slackChannelToSend}
  184. onChange={(e) => { setSlackChannelToSend(e.target.value) }}
  185. />
  186. </div>
  187. <p className="p-2">
  188. {/* eslint-disable-next-line react/no-danger */}
  189. <span dangerouslySetInnerHTML={{ __html: t('notification_settings.channel_desc') }} />
  190. </p>
  191. </>
  192. )}
  193. </div>
  194. <div className="offset-1 col-sm-5">
  195. <div className="form-group">
  196. <h3>{t('notification_settings.trigger_events')}</h3>
  197. <div className="my-1">
  198. <TriggerEventCheckBox
  199. checkbox="success"
  200. event="pageCreate"
  201. checked={triggerEvents.has('pageCreate')}
  202. onChange={() => onChangeTriggerEvents('pageCreate')}
  203. >
  204. <span className="badge badge-pill badge-success">
  205. <i className="icon-doc mr-1" /> CREATE
  206. </span>
  207. </TriggerEventCheckBox>
  208. </div>
  209. <div className="my-1">
  210. <TriggerEventCheckBox
  211. checkbox="warning"
  212. event="pageEdit"
  213. checked={triggerEvents.has('pageEdit')}
  214. onChange={() => onChangeTriggerEvents('pageEdit')}
  215. >
  216. <span className="badge badge-pill badge-warning">
  217. <i className="icon-pencil mr-1" />EDIT
  218. </span>
  219. </TriggerEventCheckBox>
  220. </div>
  221. <div className="my-1">
  222. <TriggerEventCheckBox
  223. checkbox="pink"
  224. event="pageMove"
  225. checked={triggerEvents.has('pageMove')}
  226. onChange={() => onChangeTriggerEvents('pageMove')}
  227. >
  228. <span className="badge badge-pill badge-pink">
  229. <i className="icon-action-redo mr-1" />MOVE
  230. </span>
  231. </TriggerEventCheckBox>
  232. </div>
  233. <div className="my-1">
  234. <TriggerEventCheckBox
  235. checkbox="danger"
  236. event="pageDelete"
  237. checked={triggerEvents.has('pageDelete')}
  238. onChange={() => onChangeTriggerEvents('pageDelete')}
  239. >
  240. <span className="badge badge-pill badge-danger">
  241. <i className="icon-fire mr-1" />DELETE
  242. </span>
  243. </TriggerEventCheckBox>
  244. </div>
  245. <div className="my-1">
  246. <TriggerEventCheckBox
  247. checkbox="info"
  248. event="pageLike"
  249. checked={triggerEvents.has('pageLike')}
  250. onChange={() => onChangeTriggerEvents('pageLike')}
  251. >
  252. <span className="badge badge-pill badge-info">
  253. <i className="fa fa-heart-o mr-1" />LIKE
  254. </span>
  255. </TriggerEventCheckBox>
  256. </div>
  257. <div className="my-1">
  258. <TriggerEventCheckBox
  259. checkbox="secondary"
  260. event="comment"
  261. checked={triggerEvents.has('comment')}
  262. onChange={() => onChangeTriggerEvents('comment')}
  263. >
  264. <span className="badge badge-pill badge-secondary">
  265. <i className="icon-bubble mr-1" />POST
  266. </span>
  267. </TriggerEventCheckBox>
  268. </div>
  269. </div>
  270. </div>
  271. </div>
  272. <AdminUpdateButtonRow
  273. onClick={updateButtonClickedHandler}
  274. disabled={adminNotificationContainer.state.retrieveError != null}
  275. />
  276. </>
  277. );
  278. };
  279. ManageGlobalNotification.propTypes = {
  280. adminNotificationContainer: PropTypes.instanceOf(AdminNotificationContainer).isRequired,
  281. globalNotificationId: PropTypes.string,
  282. };
  283. const ManageGlobalNotificationWrapper = withUnstatedContainers(ManageGlobalNotification, [AdminNotificationContainer]);
  284. export default ManageGlobalNotificationWrapper;