import React, { useCallback, useMemo, useEffect, useState, } from 'react'; import { useTranslation } from 'next-i18next'; import Link from 'next/link'; import { useRouter } from 'next/router'; import { NotifyType, TriggerEventType } from '~/client/interfaces/global-notification'; import { apiv3Post } from '~/client/util/apiv3-client'; import { toastError } from '~/client/util/toastr'; import { useIsMailerSetup } from '~/stores/context'; import { useSWRxGlobalNotification } from '~/stores/global-notification'; import loggerFactory from '~/utils/logger'; import AdminUpdateButtonRow from '../Common/AdminUpdateButtonRow'; import TriggerEventCheckBox from './TriggerEventCheckBox'; const logger = loggerFactory('growi:manageGlobalNotification'); type Props = { globalNotificationId?: string, } const ManageGlobalNotification = (props: Props): JSX.Element => { const [triggerPath, setTriggerPath] = useState(''); const [notifyType, setNotifyType] = useState(NotifyType.Email); 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 === NotifyType.Email) { 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], }; try { if (props.globalNotificationId != null) { await updateGlobalNotification(requestParams); router.push('/admin/notification'); } else { await apiv3Post('/notification-setting/global-notification', requestParams); router.push('/admin/notification'); } } catch (err) { toastError(err); logger.error(err); } }, [emailToSend, notifyType, props.globalNotificationId, router, slackChannelToSend, triggerEvents, triggerPath, updateGlobalNotification]); const { data: isMailerSetup } = useIsMailerSetup(); const { t } = useTranslation('admin'); return ( <>
{t('notification_settings.back_to_list')}

{t('notification_settings.notification_detail')}

{ setTriggerPath(e.target.value) }} required />

{t('notification_settings.notify_to')}

{ setNotifyType(NotifyType.Email) }} />
{ setNotifyType(NotifyType.SLACK) }} />
{notifyType === NotifyType.Email ? ( <>
{ setEmailToSend(e.target.value) }} />

{/* eslint-disable-next-line react/no-danger */} {!isMailerSetup && } Hint: {t('notification_settings.email.ifttt_link')}

) : ( <>
{ setSlackChannelToSend(e.target.value) }} />

{/* eslint-disable-next-line react/no-danger */}

)}

{t('notification_settings.trigger_events')}

onChangeTriggerEvents(TriggerEventType.CREATE)} > CREATE
onChangeTriggerEvents(TriggerEventType.EDIT)} > EDIT
onChangeTriggerEvents(TriggerEventType.MOVE)} > MOVE
onChangeTriggerEvents(TriggerEventType.DELETE)} > DELETE
onChangeTriggerEvents(TriggerEventType.LIKE)} > LIKE
onChangeTriggerEvents(TriggerEventType.POST)} > POST
); }; export default ManageGlobalNotification;