ManageGlobalNotification.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 { NotifyType, TriggerEventType } from '~/client/interfaces/global-notification';
  7. import { toastError } from '~/client/util/apiNotification';
  8. import { apiv3Post } from '~/client/util/apiv3-client';
  9. import { useIsMailerSetup } from '~/stores/context';
  10. import { useSWRxGlobalNotification } from '~/stores/global-notification';
  11. import loggerFactory from '~/utils/logger';
  12. import AdminUpdateButtonRow from '../Common/AdminUpdateButtonRow';
  13. import TriggerEventCheckBox from './TriggerEventCheckBox';
  14. const logger = loggerFactory('growi:manageGlobalNotification');
  15. type Props = {
  16. globalNotificationId?: string,
  17. }
  18. const ManageGlobalNotification = (props: Props): JSX.Element => {
  19. const [triggerPath, setTriggerPath] = useState('');
  20. const [notifyType, setNotifyType] = useState<NotifyType>(NotifyType.Email);
  21. const [emailToSend, setEmailToSend] = useState('');
  22. const [slackChannelToSend, setSlackChannelToSend] = useState('');
  23. const [triggerEvents, setTriggerEvents] = useState(new Set());
  24. const { data: globalNotificationData, update: updateGlobalNotification } = useSWRxGlobalNotification(props.globalNotificationId || '');
  25. const globalNotification = useMemo(() => globalNotificationData?.globalNotification, [globalNotificationData?.globalNotification]);
  26. const router = useRouter();
  27. useEffect(() => {
  28. if (globalNotification != null) {
  29. const notifyType = globalNotification.__t;
  30. setNotifyType(notifyType);
  31. setTriggerPath(globalNotification.triggerPath);
  32. setTriggerEvents(new Set(globalNotification.triggerEvents));
  33. if (notifyType === NotifyType.Email) {
  34. setEmailToSend(globalNotification.toEmail);
  35. }
  36. else {
  37. setSlackChannelToSend(globalNotification.slackChannels);
  38. }
  39. }
  40. }, [globalNotification]);
  41. const isLoading = globalNotificationData === undefined;
  42. const notExistsGlobalNotification = !isLoading && globalNotificationData == null;
  43. useEffect(() => {
  44. if (notExistsGlobalNotification) {
  45. router.push('/admin/notification');
  46. }
  47. }, [notExistsGlobalNotification, router]);
  48. const onChangeTriggerEvents = useCallback((triggerEvent) => {
  49. let newTriggerEvents;
  50. if (triggerEvents.has(triggerEvent)) {
  51. newTriggerEvents = ([...triggerEvents].filter(item => item !== triggerEvent));
  52. setTriggerEvents(new Set(newTriggerEvents));
  53. }
  54. else {
  55. newTriggerEvents = [...triggerEvents, triggerEvent];
  56. setTriggerEvents(new Set(newTriggerEvents));
  57. }
  58. }, [triggerEvents]);
  59. const updateButtonClickedHandler = useCallback(async() => {
  60. const requestParams = {
  61. triggerPath,
  62. notifyType,
  63. toEmail: emailToSend,
  64. slackChannels: slackChannelToSend,
  65. triggerEvents: [...triggerEvents],
  66. };
  67. try {
  68. if (props.globalNotificationId != null) {
  69. await updateGlobalNotification(requestParams);
  70. router.push('/admin/notification');
  71. }
  72. else {
  73. await apiv3Post('/notification-setting/global-notification', requestParams);
  74. router.push('/admin/notification');
  75. }
  76. }
  77. catch (err) {
  78. toastError(err);
  79. logger.error(err);
  80. }
  81. }, [emailToSend, notifyType, props.globalNotificationId, router, slackChannelToSend, triggerEvents, triggerPath, updateGlobalNotification]);
  82. const { data: isMailerSetup } = useIsMailerSetup();
  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 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>
  98. <label htmlFor="triggerPath">{t('notification_settings.trigger_path')}
  99. {/* eslint-disable-next-line react/no-danger */}
  100. <small dangerouslySetInnerHTML={{ __html: t('notification_settings.trigger_path_help', '<code>*</code>') }} />
  101. </label>
  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 === NotifyType.Email}
  123. onChange={() => { setNotifyType(NotifyType.Email) }}
  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 === NotifyType.SLACK}
  137. onChange={() => { setNotifyType(NotifyType.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 === NotifyType.Email
  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={TriggerEventType.CREATE}
  201. checked={triggerEvents.has(TriggerEventType.CREATE)}
  202. onChange={() => onChangeTriggerEvents(TriggerEventType.CREATE)}
  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={TriggerEventType.EDIT}
  213. checked={triggerEvents.has(TriggerEventType.EDIT)}
  214. onChange={() => onChangeTriggerEvents(TriggerEventType.EDIT)}
  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={TriggerEventType.MOVE}
  225. checked={triggerEvents.has(TriggerEventType.MOVE)}
  226. onChange={() => onChangeTriggerEvents(TriggerEventType.MOVE)}
  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(TriggerEventType.DELETE)}
  238. onChange={() => onChangeTriggerEvents(TriggerEventType.DELETE)}
  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={TriggerEventType.LIKE}
  249. checked={triggerEvents.has(TriggerEventType.LIKE)}
  250. onChange={() => onChangeTriggerEvents(TriggerEventType.LIKE)}
  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={TriggerEventType.POST}
  261. checked={triggerEvents.has(TriggerEventType.POST)}
  262. onChange={() => onChangeTriggerEvents(TriggerEventType.POST)}
  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={false}
  275. />
  276. </>
  277. );
  278. };
  279. export default ManageGlobalNotification;