ManageGlobalNotification.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import React, {
  2. useCallback, useMemo, useEffect, useState,
  3. } from 'react';
  4. import { useTranslation } from 'next-i18next';
  5. import Link from 'next/link';
  6. import { useRouter } from 'next/router';
  7. import { NotifyType, TriggerEventType } from '~/client/interfaces/global-notification';
  8. import { apiv3Post } from '~/client/util/apiv3-client';
  9. import { toastError } from '~/client/util/toastr';
  10. import { useIsMailerSetup } from '~/stores/context';
  11. import { useSWRxGlobalNotification } from '~/stores/global-notification';
  12. import loggerFactory from '~/utils/logger';
  13. import AdminUpdateButtonRow from '../Common/AdminUpdateButtonRow';
  14. import TriggerEventCheckBox from './TriggerEventCheckBox';
  15. const logger = loggerFactory('growi:manageGlobalNotification');
  16. type Props = {
  17. globalNotificationId?: string,
  18. }
  19. const ManageGlobalNotification = (props: Props): JSX.Element => {
  20. const [triggerPath, setTriggerPath] = useState('');
  21. const [notifyType, setNotifyType] = useState<NotifyType>(NotifyType.Email);
  22. const [emailToSend, setEmailToSend] = useState('');
  23. const [slackChannelToSend, setSlackChannelToSend] = useState('');
  24. const [triggerEvents, setTriggerEvents] = useState(new Set());
  25. const { data: globalNotificationData, update: updateGlobalNotification } = useSWRxGlobalNotification(props.globalNotificationId || '');
  26. const globalNotification = useMemo(() => globalNotificationData?.globalNotification, [globalNotificationData?.globalNotification]);
  27. const router = useRouter();
  28. useEffect(() => {
  29. if (globalNotification != null) {
  30. const notifyType = globalNotification.__t;
  31. setNotifyType(notifyType);
  32. setTriggerPath(globalNotification.triggerPath);
  33. setTriggerEvents(new Set(globalNotification.triggerEvents));
  34. if (notifyType === NotifyType.Email) {
  35. setEmailToSend(globalNotification.toEmail);
  36. }
  37. else {
  38. setSlackChannelToSend(globalNotification.slackChannels);
  39. }
  40. }
  41. }, [globalNotification]);
  42. const isLoading = globalNotificationData === undefined;
  43. const notExistsGlobalNotification = !isLoading && globalNotificationData == null;
  44. useEffect(() => {
  45. if (notExistsGlobalNotification) {
  46. router.push('/admin/notification');
  47. }
  48. }, [notExistsGlobalNotification, router]);
  49. const onChangeTriggerEvents = useCallback((triggerEvent) => {
  50. let newTriggerEvents;
  51. if (triggerEvents.has(triggerEvent)) {
  52. newTriggerEvents = ([...triggerEvents].filter(item => item !== triggerEvent));
  53. setTriggerEvents(new Set(newTriggerEvents));
  54. }
  55. else {
  56. newTriggerEvents = [...triggerEvents, triggerEvent];
  57. setTriggerEvents(new Set(newTriggerEvents));
  58. }
  59. }, [triggerEvents]);
  60. const updateButtonClickedHandler = useCallback(async() => {
  61. const requestParams = {
  62. triggerPath,
  63. notifyType,
  64. toEmail: emailToSend,
  65. slackChannels: slackChannelToSend,
  66. triggerEvents: [...triggerEvents],
  67. };
  68. try {
  69. if (props.globalNotificationId != null) {
  70. await updateGlobalNotification(requestParams);
  71. router.push('/admin/notification');
  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, notifyType, props.globalNotificationId, router, slackChannelToSend, triggerEvents, triggerPath, updateGlobalNotification]);
  83. const { data: isMailerSetup } = useIsMailerSetup();
  84. const { t } = useTranslation('admin');
  85. return (
  86. <>
  87. <div className="my-3">
  88. <Link href="/admin/notification" className="btn btn-outline-secondary">
  89. <div className="d-flex me-1">
  90. <span className="material-symbols-outlined" aria-hidden="true">arrow_left_alt</span>
  91. {t('notification_settings.back_to_list')}
  92. </div>
  93. </Link>
  94. </div>
  95. <div className="row">
  96. <div className="form-box col-md-12">
  97. <h2 className="border-bottom mb-5">{t('notification_settings.notification_detail')}</h2>
  98. </div>
  99. <div className="col-sm-4">
  100. <h3>
  101. <label htmlFor="triggerPath" className="form-label">{t('notification_settings.trigger_path')}
  102. {/* eslint-disable-next-line react/no-danger */}
  103. <small dangerouslySetInnerHTML={{ __html: t('notification_settings.trigger_path_help', '<code>*</code>') }} />
  104. </label>
  105. </h3>
  106. <div>
  107. <input
  108. className="form-control"
  109. type="text"
  110. name="triggerPath"
  111. value={triggerPath}
  112. onChange={(e) => { setTriggerPath(e.target.value) }}
  113. required
  114. />
  115. </div>
  116. <h3>{t('notification_settings.notify_to')}</h3>
  117. <div>
  118. <div className="form-check">
  119. <input
  120. className="form-check-input"
  121. type="radio"
  122. id="mail"
  123. name="notifyType"
  124. value="mail"
  125. checked={notifyType === NotifyType.Email}
  126. onChange={() => { setNotifyType(NotifyType.Email) }}
  127. />
  128. <label className="form-label form-check-label" htmlFor="mail">
  129. <p className="fw-bold">Email</p>
  130. </label>
  131. </div>
  132. <div className="form-check ms-2">
  133. <input
  134. className="form-check-input"
  135. type="radio"
  136. id="slack"
  137. name="notifyType"
  138. value="slack"
  139. checked={notifyType === NotifyType.SLACK}
  140. onChange={() => { setNotifyType(NotifyType.SLACK) }}
  141. />
  142. <label className="form-label form-check-label" htmlFor="slack">
  143. <p className="fw-bold">Slack</p>
  144. </label>
  145. </div>
  146. </div>
  147. {notifyType === NotifyType.Email
  148. ? (
  149. <>
  150. <div className="input-group notify-to-option" id="mail-input">
  151. <div>
  152. <span className="input-group-text" id="mail-addon">
  153. <span className="material-symbols-outlined">mail</span>
  154. </span>
  155. </div>
  156. <input
  157. className="form-control"
  158. type="text"
  159. aria-describedby="mail-addon"
  160. name="toEmail"
  161. placeholder="Email"
  162. value={emailToSend}
  163. onChange={(e) => { setEmailToSend(e.target.value) }}
  164. />
  165. </div>
  166. <p className="p-2">
  167. {/* eslint-disable-next-line react/no-danger */}
  168. {!isMailerSetup && <span className="form-text text-muted" dangerouslySetInnerHTML={{ __html: t('admin:mailer_setup_required') }} />}
  169. <b>Hint: </b>
  170. <a href="https://ifttt.com/create" target="blank">{t('notification_settings.email.ifttt_link')}
  171. {/* TODO: 137284 */}
  172. <span className="material-symbols-outlined">share</span>
  173. </a>
  174. </p>
  175. </>
  176. )
  177. : (
  178. <>
  179. <div className="input-group notify-to-option" id="slack-input">
  180. <div>
  181. <span className="input-group-text" id="slack-channel-addon">
  182. <span className="material-symbols-outlined">tag</span>
  183. </span>
  184. </div>
  185. <input
  186. className="form-control"
  187. type="text"
  188. aria-describedby="slack-channel-addon"
  189. name="notificationGlobal[slackChannels]"
  190. placeholder="Slack Channel"
  191. value={slackChannelToSend}
  192. onChange={(e) => { setSlackChannelToSend(e.target.value) }}
  193. />
  194. </div>
  195. <p className="p-2">
  196. {/* eslint-disable-next-line react/no-danger */}
  197. <span dangerouslySetInnerHTML={{ __html: t('notification_settings.channel_desc') }} />
  198. </p>
  199. </>
  200. )}
  201. </div>
  202. <div className="offset-1 col-sm-5">
  203. <div>
  204. <h3>{t('notification_settings.trigger_events')}</h3>
  205. <div className="my-1">
  206. <TriggerEventCheckBox
  207. checkbox="success"
  208. event={TriggerEventType.CREATE}
  209. checked={triggerEvents.has(TriggerEventType.CREATE)}
  210. onChange={() => onChangeTriggerEvents(TriggerEventType.CREATE)}
  211. >
  212. <span className="badge rounded-pill bg-success">
  213. <div className="d-flex align-items-center">
  214. <span className=" material-symbols-outlined me-1">description</span>CREATE
  215. </div>
  216. </span>
  217. </TriggerEventCheckBox>
  218. </div>
  219. <div className="my-1">
  220. <TriggerEventCheckBox
  221. checkbox="warning"
  222. event={TriggerEventType.EDIT}
  223. checked={triggerEvents.has(TriggerEventType.EDIT)}
  224. onChange={() => onChangeTriggerEvents(TriggerEventType.EDIT)}
  225. >
  226. <span className="badge rounded-pill bg-warning text-dark">
  227. <div className="d-flex align-items-center">
  228. <span className="material-symbols-outlined me-1">edit</span>EDIT
  229. </div>
  230. </span>
  231. </TriggerEventCheckBox>
  232. </div>
  233. <div className="my-1">
  234. <TriggerEventCheckBox
  235. checkbox="pink"
  236. event={TriggerEventType.MOVE}
  237. checked={triggerEvents.has(TriggerEventType.MOVE)}
  238. onChange={() => onChangeTriggerEvents(TriggerEventType.MOVE)}
  239. >
  240. <span className="badge rounded-pill bg-secondary">
  241. <div className="d-flex align-items-center">
  242. <span className="material-symbols-outlined me-1">redo</span>MOVE
  243. </div>
  244. </span>
  245. </TriggerEventCheckBox>
  246. </div>
  247. <div className="my-1">
  248. <TriggerEventCheckBox
  249. checkbox="danger"
  250. event="pageDelete"
  251. checked={triggerEvents.has(TriggerEventType.DELETE)}
  252. onChange={() => onChangeTriggerEvents(TriggerEventType.DELETE)}
  253. >
  254. <span className="badge rounded-pill bg-danger">
  255. <div className="d-flex align-items-center">
  256. <span className="material-symbols-outlined me-1">delete_forever</span>DELETE
  257. </div>
  258. </span>
  259. </TriggerEventCheckBox>
  260. </div>
  261. <div className="my-1">
  262. <TriggerEventCheckBox
  263. checkbox="info"
  264. event={TriggerEventType.LIKE}
  265. checked={triggerEvents.has(TriggerEventType.LIKE)}
  266. onChange={() => onChangeTriggerEvents(TriggerEventType.LIKE)}
  267. >
  268. <span className="badge rounded-pill bg-info">
  269. <div className="d-flex align-items-center">
  270. <span className="material-symbols-outlined me-1">favorite</span>LIKE
  271. </div>
  272. </span>
  273. </TriggerEventCheckBox>
  274. </div>
  275. <div className="my-1">
  276. <TriggerEventCheckBox
  277. checkbox="secondary"
  278. event={TriggerEventType.POST}
  279. checked={triggerEvents.has(TriggerEventType.POST)}
  280. onChange={() => onChangeTriggerEvents(TriggerEventType.POST)}
  281. >
  282. <span className="badge rounded-pill bg-primary">
  283. <div className="d-flex align-items-center">
  284. <span className="material-symbols-outlined me-1">bubble_chart</span>POST
  285. </div>
  286. </span>
  287. </TriggerEventCheckBox>
  288. </div>
  289. </div>
  290. </div>
  291. </div>
  292. <AdminUpdateButtonRow
  293. onClick={updateButtonClickedHandler}
  294. disabled={false}
  295. />
  296. </>
  297. );
  298. };
  299. export default ManageGlobalNotification;