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'; import { withUnstatedContainers } from '../../UnstatedUtils'; import AdminUpdateButtonRow from '../Common/AdminUpdateButtonRow'; import TriggerEventCheckBox from './TriggerEventCheckBox'; const logger = loggerFactory('growi:manageGlobalNotification'); const ManageGlobalNotification = (props) => { const [triggerPath, setTriggerPath] = useState(''); const [notifyType, setNotifyType] = useState('mail'); const [emailToSend, setEmailToSend] = useState(''); const [slackChannelToSend, setSlackChannelToSend] = useState(''); 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; if (triggerEvents.has(triggerEvent)) { newTriggerEvents = ([...triggerEvents].filter(item => item !== triggerEvent)); setTriggerEvents(new Set(newTriggerEvents)); } else { newTriggerEvents = [...triggerEvents, triggerEvent]; setTriggerEvents(new Set(newTriggerEvents)); } }, [triggerEvents]); const updateButtonClickedHandler = useCallback(async() => { const requestParams = { triggerPath, notifyType, toEmail: emailToSend, slackChannels: slackChannelToSend, triggerEvents: [...triggerEvents], }; const { _id: globalNotificationId } = globalNotification; try { if (globalNotificationId != null) { 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, globalNotification, notifyType, router, slackChannelToSend, triggerEvents, triggerPath, updateGlobalNotification]); 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 */}
> )}