// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import React from 'react';
import PropTypes from 'prop-types';
import {savePreference} from 'actions/user_actions.jsx';
import PreferenceStore from 'stores/preference_store.jsx';
import {localizeMessage} from 'utils/utils.jsx';
import {FormattedMessage} from 'react-intl';
import SettingItemMin from 'components/setting_item_min.jsx';
import SettingItemMax from 'components/setting_item_max.jsx';
import {Preferences} from 'utils/constants.jsx';
export default class EmailNotificationSetting extends React.Component {
static propTypes = {
activeSection: PropTypes.string.isRequired,
updateSection: PropTypes.func.isRequired,
enableEmail: PropTypes.bool.isRequired,
onChange: PropTypes.func.isRequired,
onSubmit: PropTypes.func.isRequired,
serverError: PropTypes.string
};
constructor(props) {
super(props);
this.submit = this.submit.bind(this);
this.expand = this.expand.bind(this);
this.collapse = this.collapse.bind(this);
this.state = {
emailInterval: EmailNotificationSetting.getEmailInterval(props)
};
}
handleChange(enableEmail, emailInterval) {
this.props.onChange(enableEmail);
this.setState({emailInterval});
}
submit() {
// until the rest of the notification settings are moved to preferences, we have to do this separately
savePreference(Preferences.CATEGORY_NOTIFICATIONS, Preferences.EMAIL_INTERVAL, this.state.emailInterval.toString());
this.props.onSubmit();
}
expand() {
this.props.updateSection('email');
}
collapse() {
this.props.updateSection('');
}
render() {
if (global.window.mm_config.SendEmailNotifications !== 'true' && this.props.activeSection === 'email') {
const inputs = [];
inputs.push(
);
return (
);
}
if (this.props.activeSection !== 'email') {
let description;
if (global.window.mm_config.SendEmailNotifications !== 'true') {
description = (
);
} else if (this.props.enableEmail) {
switch (this.state.emailInterval) {
case Preferences.INTERVAL_IMMEDIATE:
description = (
);
break;
case Preferences.INTERVAL_HOUR:
description = (
);
break;
default:
description = (
);
}
} else {
description = (
);
}
return (
);
}
let batchingOptions = null;
let batchingInfo = null;
if (window.mm_config.EnableEmailBatching === 'true') {
batchingOptions = (
);
batchingInfo = (
);
}
return (
{batchingOptions}
{' '}
{batchingInfo}
]}
submit={this.submit}
server_error={this.props.serverError}
updateSection={this.collapse}
/>
);
}
static getEmailInterval(props) {
const validValuesWithEmailBatching = [Preferences.INTERVAL_IMMEDIATE, Preferences.INTERVAL_FIFTEEN_MINUTES, Preferences.INTERVAL_HOUR];
const validValuesWithoutEmailBatching = [Preferences.INTERVAL_IMMEDIATE];
let emailInterval;
if (global.mm_config.EnableEmailBatching === 'true') {
// when email batching is enabled, the default interval is 15 minutes
emailInterval = PreferenceStore.getInt(Preferences.CATEGORY_NOTIFICATIONS, Preferences.EMAIL_INTERVAL, Preferences.INTERVAL_FIFTEEN_MINUTES);
if (validValuesWithEmailBatching.indexOf(emailInterval) === -1) {
emailInterval = Preferences.INTERVAL_FIFTEEN_MINUTES;
}
} else {
// otherwise, the default interval is immediately
emailInterval = PreferenceStore.getInt(Preferences.CATEGORY_NOTIFICATIONS, Preferences.EMAIL_INTERVAL, Preferences.INTERVAL_IMMEDIATE);
if (validValuesWithoutEmailBatching.indexOf(emailInterval) === -1) {
emailInterval = Preferences.INTERVAL_IMMEDIATE;
}
}
if (!props.enableEmail) {
emailInterval = Preferences.INTERVAL_NEVER;
}
return emailInterval;
}
}