import React from 'react'; import PropTypes from 'prop-types'; import { withTranslation } from 'react-i18next'; import urljoin from 'url-join'; import loggerFactory from '@alias/logger'; import { toastError } from '~/client/util/apiNotification'; import TriggerEventCheckBox from './TriggerEventCheckBox'; import AdminUpdateButtonRow from '../Common/AdminUpdateButtonRow'; import AppContainer from '~/client/services/AppContainer'; import { withUnstatedContainers } from '../../UnstatedUtils'; 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 this.props.appContainer.apiv3.put(`/notification-setting/global-notification/${this.state.globalNotificationId}`, requestParams); } else { await this.props.appContainer.apiv3.post('/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') }} />
{ this.onChangeNotifyToType('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
); } } const ManageGlobalNotificationWrapper = withUnstatedContainers(ManageGlobalNotification, [AppContainer]); ManageGlobalNotification.propTypes = { t: PropTypes.func.isRequired, // i18next appContainer: PropTypes.instanceOf(AppContainer).isRequired, }; export default withTranslation()(ManageGlobalNotificationWrapper);