import React from 'react'; import { useTranslation } from 'next-i18next'; import PropTypes from 'prop-types'; import urljoin from 'url-join'; import AppContainer from '~/client/services/AppContainer'; import { toastError } from '~/client/util/apiNotification'; import { apiv3Post, apiv3Put } from '~/client/util/apiv3-client'; import loggerFactory from '~/utils/logger'; import { withUnstatedContainers } from '../../UnstatedUtils'; import AdminUpdateButtonRow from '../Common/AdminUpdateButtonRow'; import TriggerEventCheckBox from './TriggerEventCheckBox'; const logger = loggerFactory('growi:manageGlobalNotification'); class ManageGlobalNotification extends React.Component { constructor() { super(); let globalNotification; try { globalNotification = JSON.parse(document.getElementById('admin-global-notification-setting').getAttribute('data-global-notification')); } catch (err) { toastError(err); logger.error(err); } this.state = { globalNotificationId: globalNotification._id || null, triggerPath: globalNotification.triggerPath || '', notifyToType: globalNotification.__t || 'mail', emailToSend: globalNotification.toEmail || '', slackChannelToSend: globalNotification.slackChannels || '', triggerEvents: new Set(globalNotification.triggerEvents), }; this.submitHandler = this.submitHandler.bind(this); } onChangeTriggerPath(inputValue) { this.setState({ triggerPath: inputValue }); } onChangeNotifyToType(notifyToType) { this.setState({ notifyToType }); } onChangeEmailToSend(inputValue) { this.setState({ emailToSend: inputValue }); } onChangeSlackChannelToSend(inputValue) { this.setState({ slackChannelToSend: inputValue }); } onChangeTriggerEvents(triggerEvent) { const { triggerEvents } = this.state; if (triggerEvents.has(triggerEvent)) { triggerEvents.delete(triggerEvent); this.setState({ triggerEvents }); } else { triggerEvents.add(triggerEvent); this.setState({ triggerEvents }); } } async submitHandler() { const requestParams = { triggerPath: this.state.triggerPath, notifyToType: this.state.notifyToType, toEmail: this.state.emailToSend, slackChannels: this.state.slackChannelToSend, triggerEvents: [...this.state.triggerEvents], }; try { if (this.state.globalNotificationId != null) { await apiv3Put(`/notification-setting/global-notification/${this.state.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); } } render() { const { t, appContainer } = this.props; const { isMailerSetup } = appContainer.config; return ( {t('notification_setting.back_to_list')} {t('notification_setting.notification_detail')} {t('notification_setting.trigger_path')} {/* eslint-disable-next-line react/no-danger */} *') }} /> { this.onChangeTriggerPath(e.target.value) }} required /> {t('notification_setting.notify_to')} { this.onChangeNotifyToType('mail') }} /> Email { this.onChangeNotifyToType('slack') }} /> Slack {this.state.notifyToType === 'mail' ? ( <> { this.onChangeEmailToSend(e.target.value) }} /> {/* eslint-disable-next-line react/no-danger */} {!isMailerSetup && } Hint: {t('notification_setting.email.ifttt_link')} > ) : ( <> { this.onChangeSlackChannelToSend(e.target.value) }} /> {/* eslint-disable-next-line react/no-danger */} > )} {t('notification_setting.trigger_events')} this.onChangeTriggerEvents('pageCreate')} > CREATE this.onChangeTriggerEvents('pageEdit')} > EDIT this.onChangeTriggerEvents('pageMove')} > MOVE this.onChangeTriggerEvents('pageDelete')} > DELETE this.onChangeTriggerEvents('pageLike')} > LIKE this.onChangeTriggerEvents('comment')} > POST ); } } ManageGlobalNotification.propTypes = { t: PropTypes.func.isRequired, // i18next appContainer: PropTypes.instanceOf(AppContainer).isRequired, }; const ManageGlobalNotificationWrapperFC = (props) => { const { t } = useTranslation(); return ; }; const ManageGlobalNotificationWrapper = withUnstatedContainers(ManageGlobalNotificationWrapperFC, [AppContainer]); export default ManageGlobalNotificationWrapper;
Email
Slack
{/* eslint-disable-next-line react/no-danger */} {!isMailerSetup && } Hint: {t('notification_setting.email.ifttt_link')}
{/* eslint-disable-next-line react/no-danger */}