import React, { useCallback, useState } from 'react'; import { useTranslation } from 'next-i18next'; import PropTypes from 'prop-types'; import urljoin from 'url-join'; 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 loggerFactory from '~/utils/logger'; import { withUnstatedContainers } from '../../UnstatedUtils'; import AdminUpdateButtonRow from '../Common/AdminUpdateButtonRow'; 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 [globalNotificationId, setGlobalNotificationId] = useState(null); const [triggerPath, setTriggerPath] = useState(''); const [notifyToType, setNotifyToType] = useState('mail'); const [emailToSend, setEmailToSend] = useState(''); const [slackChannelToSend, setSlackChannelToSend] = useState(''); const [triggerEvents, setTriggerEvents] = useState(new Set(globalNotification?.triggerEvents)); const onChangeTriggerEvents = (triggerEvent) => { if (triggerEvents.has(triggerEvent)) { triggerEvents.delete(triggerEvent); setTriggerEvents(triggerEvents); } else { triggerEvents.add(triggerEvent); setTriggerEvents(triggerEvents); } }; const submitHandler = useCallback(async() => { const requestParams = { triggerPath, notifyToType, emailToSend, slackChannelToSend, triggerEvents, }; try { if (globalNotificationId != null) { await apiv3Put(`/notification-setting/global-notification/${globalNotificationId}`, requestParams); } else { await apiv3Post('/notification-setting/global-notification', requestParams); } window.location.href = urljoin(window.location.origin, '/admin/notification#global-notification'); } catch (err) { toastError(err); logger.error(err); } }, [emailToSend, globalNotificationId, notifyToType, slackChannelToSend, triggerEvents, triggerPath]); const { data: isMailerSetup } = useIsMailerSetup(); const { adminNotificationContainer } = props; const { t } = useTranslation('admin'); return ( <>
{/* eslint-disable-next-line react/no-danger */} {!isMailerSetup && } Hint: {t('notification_settings.email.ifttt_link')}
> ) : ( <>{/* eslint-disable-next-line react/no-danger */}
> )}