import React from 'react'; import { useTranslation } from 'next-i18next'; import PropTypes from 'prop-types'; import urljoin from 'url-join'; import AdminNotificationContainer from '~/client/services/AdminNotificationContainer'; import { apiv3Put } from '~/client/util/apiv3-client'; import { toastSuccess, toastError } from '~/client/util/toastr'; import loggerFactory from '~/utils/logger'; import { withUnstatedContainers } from '../../UnstatedUtils'; import NotificationDeleteModal from './NotificationDeleteModal'; import { NotificationTypeIcon } from './NotificationTypeIcon'; const logger = loggerFactory('growi:GolobalNotificationList'); class GlobalNotificationList extends React.Component { constructor(props) { super(props); this.state = { isConfirmationModalOpen: false, notificationForConfiguration: null, }; this.openConfirmationModal = this.openConfirmationModal.bind(this); this.closeConfirmationModal = this.closeConfirmationModal.bind(this); this.onClickSubmit = this.onClickSubmit.bind(this); } async toggleIsEnabled(notification) { const { t } = this.props; const isEnabled = !notification.isEnabled; try { await apiv3Put(`/notification-setting/global-notification/${notification._id}/enabled`, { isEnabled, }); toastSuccess(t('notification_settings.toggle_notification', { path: notification.triggerPath })); await this.props.adminNotificationContainer.retrieveNotificationData(); } catch (err) { toastError(err); logger.error(err); } } openConfirmationModal(notification) { this.setState({ isConfirmationModalOpen: true, notificationForConfiguration: notification }); } closeConfirmationModal() { this.setState({ isConfirmationModalOpen: false, notificationForConfiguration: null }); } async onClickSubmit() { const { t, adminNotificationContainer } = this.props; try { const deletedNotificaton = await adminNotificationContainer.deleteGlobalNotificationPattern(this.state.notificationForConfiguration._id); toastSuccess(t('notification_settings.delete_notification_pattern', { path: deletedNotificaton.triggerPath })); } catch (err) { toastError(err); logger.error(err); } this.setState({ isConfirmationModalOpen: false }); } render() { const { t, adminNotificationContainer } = this.props; const { globalNotifications } = adminNotificationContainer.state; return ( {globalNotifications.map((notification) => { return (
this.toggleIsEnabled(notification)} />
{notification.triggerPath} { notification.__t === 'mail' && notification.toEmail } { notification.__t === 'slack' && notification.slackChannels }
note {t('Edit')}
); })} {this.state.notificationForConfiguration != null && ( )}
); } } GlobalNotificationList.propTypes = { t: PropTypes.func.isRequired, // i18next adminNotificationContainer: PropTypes.instanceOf(AdminNotificationContainer).isRequired, }; const GlobalNotificationListWrapperFC = (props) => { const { t } = useTranslation('admin'); return ; }; const GlobalNotificationListWrapper = withUnstatedContainers(GlobalNotificationListWrapperFC, [AdminNotificationContainer]); export default GlobalNotificationListWrapper;