|
|
@@ -1,12 +1,16 @@
|
|
|
-import React, { useCallback, useState } from 'react';
|
|
|
+import React, {
|
|
|
+ useCallback, useMemo, useEffect, useState,
|
|
|
+} from 'react';
|
|
|
|
|
|
import { useTranslation } from 'next-i18next';
|
|
|
+import { useRouter } from 'next/router';
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
import AdminNotificationContainer from '~/client/services/AdminNotificationContainer';
|
|
|
import { toastError } from '~/client/util/apiNotification';
|
|
|
import { apiv3Post, apiv3Put } from '~/client/util/apiv3-client';
|
|
|
import { useIsMailerSetup } from '~/stores/context';
|
|
|
+import { useSWRxGlobalNotification } from '~/stores/global-notification';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
|
|
|
@@ -18,18 +22,48 @@ import TriggerEventCheckBox from './TriggerEventCheckBox';
|
|
|
|
|
|
const logger = loggerFactory('growi:manageGlobalNotification');
|
|
|
|
|
|
-const ManageGlobalNotification = (props) => {
|
|
|
|
|
|
- let globalNotification;
|
|
|
- // TODO: securely fetch the data of globalNotification variable without using swig. URL https://redmine.weseek.co.jp/issues/103901
|
|
|
- // globalNotification = JSON.parse(document.getElementById('admin-global-notification-setting').getAttribute('data-global-notification'));
|
|
|
+const ManageGlobalNotification = (props) => {
|
|
|
|
|
|
- const [globalNotificationId, setGlobalNotificationId] = useState(null);
|
|
|
const [triggerPath, setTriggerPath] = useState('');
|
|
|
- const [notifyToType, setNotifyToType] = useState('mail');
|
|
|
+ const [notifyType, setNotifyType] = useState('mail');
|
|
|
const [emailToSend, setEmailToSend] = useState('');
|
|
|
const [slackChannelToSend, setSlackChannelToSend] = useState('');
|
|
|
- const [triggerEvents, setTriggerEvents] = useState(new Set(globalNotification?.triggerEvents));
|
|
|
+ const [triggerEvents, setTriggerEvents] = useState(new Set());
|
|
|
+ const { data: globalNotificationData, update: updateGlobalNotification } = useSWRxGlobalNotification(props.globalNotificationId);
|
|
|
+ const globalNotification = useMemo(() => globalNotificationData?.globalNotification, [globalNotificationData?.globalNotification]);
|
|
|
+
|
|
|
+ const router = useRouter();
|
|
|
+
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ if (globalNotification != null) {
|
|
|
+ const notifyType = globalNotification.__t;
|
|
|
+ setNotifyType(notifyType);
|
|
|
+
|
|
|
+ setTriggerPath(globalNotification.triggerPath);
|
|
|
+ setTriggerEvents(new Set(globalNotification.triggerEvents));
|
|
|
+
|
|
|
+ if (notifyType === 'mail') {
|
|
|
+ setEmailToSend(globalNotification.toEmail);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ setSlackChannelToSend(globalNotification.slackChannels);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }, [globalNotification]);
|
|
|
+
|
|
|
+ const isLoading = globalNotificationData === undefined;
|
|
|
+ const notExistsGlobalNotification = !isLoading && globalNotificationData == null;
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ if (notExistsGlobalNotification) {
|
|
|
+ router.push('/admin/notification');
|
|
|
+ }
|
|
|
+ }, [notExistsGlobalNotification, router]);
|
|
|
+
|
|
|
|
|
|
const onChangeTriggerEvents = useCallback((triggerEvent) => {
|
|
|
let newTriggerEvents;
|
|
|
@@ -44,29 +78,35 @@ const ManageGlobalNotification = (props) => {
|
|
|
}
|
|
|
}, [triggerEvents]);
|
|
|
|
|
|
- const updateButtonClickedHandler = useCallback(async() => {
|
|
|
|
|
|
+ const updateButtonClickedHandler = useCallback(async() => {
|
|
|
const requestParams = {
|
|
|
triggerPath,
|
|
|
- notifyToType,
|
|
|
+ notifyType,
|
|
|
toEmail: emailToSend,
|
|
|
slackChannels: slackChannelToSend,
|
|
|
triggerEvents: [...triggerEvents],
|
|
|
};
|
|
|
|
|
|
+ const { _id: globalNotificationId } = globalNotification;
|
|
|
+
|
|
|
try {
|
|
|
if (globalNotificationId != null) {
|
|
|
- await apiv3Put(`/notification-setting/global-notification/${globalNotificationId}`, requestParams);
|
|
|
+ await updateGlobalNotification(requestParams);
|
|
|
+ router.push('/admin/notification');
|
|
|
+ // await apiv3Put(`/notification-setting/global-notification/${globalNotificationId}`, requestParams);
|
|
|
}
|
|
|
else {
|
|
|
await apiv3Post('/notification-setting/global-notification', requestParams);
|
|
|
+ router.push('/admin/notification');
|
|
|
}
|
|
|
}
|
|
|
catch (err) {
|
|
|
toastError(err);
|
|
|
logger.error(err);
|
|
|
}
|
|
|
- }, [emailToSend, globalNotificationId, notifyToType, slackChannelToSend, triggerEvents, triggerPath]);
|
|
|
+ }, [emailToSend, globalNotification, notifyType, router, slackChannelToSend, triggerEvents, triggerPath, updateGlobalNotification]);
|
|
|
+
|
|
|
|
|
|
const { data: isMailerSetup } = useIsMailerSetup();
|
|
|
const { adminNotificationContainer } = props;
|
|
|
@@ -110,10 +150,10 @@ const ManageGlobalNotification = (props) => {
|
|
|
className="custom-control-input"
|
|
|
type="radio"
|
|
|
id="mail"
|
|
|
- name="notifyToType"
|
|
|
+ name="notifyType"
|
|
|
value="mail"
|
|
|
- checked={notifyToType === 'mail'}
|
|
|
- onChange={() => { setNotifyToType('mail') }}
|
|
|
+ checked={notifyType === 'mail'}
|
|
|
+ onChange={() => { setNotifyType('mail') }}
|
|
|
/>
|
|
|
<label className="custom-control-label" htmlFor="mail">
|
|
|
<p className="font-weight-bold">Email</p>
|
|
|
@@ -124,10 +164,10 @@ const ManageGlobalNotification = (props) => {
|
|
|
className="custom-control-input"
|
|
|
type="radio"
|
|
|
id="slack"
|
|
|
- name="notifyToType"
|
|
|
+ name="notifyType"
|
|
|
value="slack"
|
|
|
- checked={notifyToType === 'slack'}
|
|
|
- onChange={() => { setNotifyToType('slack') }}
|
|
|
+ checked={notifyType === 'slack'}
|
|
|
+ onChange={() => { setNotifyType('slack') }}
|
|
|
/>
|
|
|
<label className="custom-control-label" htmlFor="slack">
|
|
|
<p className="font-weight-bold">Slack</p>
|
|
|
@@ -135,7 +175,7 @@ const ManageGlobalNotification = (props) => {
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
- {notifyToType === 'mail'
|
|
|
+ {notifyType === 'mail'
|
|
|
? (
|
|
|
<>
|
|
|
<div className="input-group notify-to-option" id="mail-input">
|
|
|
@@ -278,6 +318,7 @@ const ManageGlobalNotification = (props) => {
|
|
|
|
|
|
ManageGlobalNotification.propTypes = {
|
|
|
adminNotificationContainer: PropTypes.instanceOf(AdminNotificationContainer).isRequired,
|
|
|
+ globalNotificationId: PropTypes.string,
|
|
|
};
|
|
|
|
|
|
const ManageGlobalNotificationWrapper = withUnstatedContainers(ManageGlobalNotification, [AdminNotificationContainer]);
|