fix email notification settings (#2051)

This commit is contained in:
Saturnino Abril 2018-08-30 02:25:09 +08:00 committed by Harrison Healey
parent 4b6bd70db5
commit 320f1d9c91
8 changed files with 77 additions and 68 deletions

View file

@ -1,36 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {updateMe} from 'mattermost-redux/actions/users';
import {Preferences} from 'mattermost-redux/constants';
import {savePreferences} from 'mattermost-redux/actions/preferences';
import {getConfig} from 'mattermost-redux/selectors/entities/general';
export function handleUpdateUserNotifyProps(notifyProps) {
return async (dispatch, getState) => {
const state = getState();
const config = getConfig(state);
const {currentUserId} = state.entities.users;
const {interval, user_id: userId, ...otherProps} = notifyProps;
const email = notifyProps.email;
if (config.EnableEmailBatching === 'true' && email !== 'false') {
const emailInterval = [{
user_id: userId,
category: Preferences.CATEGORY_NOTIFICATIONS,
name: Preferences.EMAIL_INTERVAL,
value: interval,
}];
savePreferences(currentUserId, emailInterval)(dispatch, getState);
}
const props = {...otherProps, email};
try {
await updateMe({notify_props: props})(dispatch, getState);
} catch (error) {
// do nothing
}
};
}

View file

@ -9,7 +9,7 @@ import {getConfig} from 'mattermost-redux/selectors/entities/general';
import {getMyPreferences, getTheme} from 'mattermost-redux/selectors/entities/preferences';
import {isMinimumServerVersion} from 'mattermost-redux/utils/helpers';
import {handleUpdateUserNotifyProps} from 'app/actions/views/account_notifications';
import {updateMe} from 'mattermost-redux/actions/users';
import NotificationSettings from './notification_settings';
@ -34,7 +34,7 @@ function mapStateToProps(state) {
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators({
handleUpdateUserNotifyProps,
updateMe,
}, dispatch),
};
}

View file

@ -24,7 +24,7 @@ import {changeOpacity, makeStyleSheetFromTheme, setNavigatorStyles} from 'app/ut
class NotificationSettings extends PureComponent {
static propTypes = {
actions: PropTypes.shape({
handleUpdateUserNotifyProps: PropTypes.func.isRequired,
updateMe: PropTypes.func.isRequired,
}),
currentUser: PropTypes.object.isRequired,
intl: intlShape.isRequired,
@ -99,7 +99,7 @@ class NotificationSettings extends PureComponent {
screenBackgroundColor: theme.centerChannelBg,
},
passProps: {
currentUserId: currentUser.id,
currentUser,
},
});
};
@ -161,7 +161,7 @@ class NotificationSettings extends PureComponent {
};
if (!deepEqual(previousProps, notifyProps)) {
this.props.actions.handleUpdateUserNotifyProps(notifyProps);
this.props.actions.updateMe({notify_props: notifyProps});
}
};
@ -195,7 +195,7 @@ class NotificationSettings extends PureComponent {
}
if (this.shouldSaveAutoResponder(notifyProps)) {
this.props.actions.handleUpdateUserNotifyProps(notifyProps);
this.props.actions.updateMe({notify_props: notifyProps});
}
};

View file

@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`NotificationSettingsMobileAndroid should match snapshot 1`] = `
exports[`NotificationSettingsEmailAndroid should match snapshot 1`] = `
<sectionItem
action={[Function]}
actionType="default"
@ -25,7 +25,7 @@ exports[`NotificationSettingsMobileAndroid should match snapshot 1`] = `
/>
`;
exports[`NotificationSettingsMobileAndroid should match snapshot 2`] = `
exports[`NotificationSettingsEmailAndroid should match snapshot 2`] = `
<Component
animationType="slide"
hardwareAccelerated={false}

View file

@ -7,6 +7,7 @@ import {connect} from 'react-redux';
import {Preferences} from 'mattermost-redux/constants';
import {savePreferences} from 'mattermost-redux/actions/preferences';
import {updateMe} from 'mattermost-redux/actions/users';
import {getConfig} from 'mattermost-redux/selectors/entities/general';
import {
@ -40,6 +41,7 @@ function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators({
savePreferences,
updateMe,
}, dispatch),
};
}

View file

@ -7,17 +7,19 @@ import Adapter from 'enzyme-adapter-react-16';
configure({adapter: new Adapter()});
import {shallowWithIntl} from 'test/intl-test-helper';
import {emptyFunction} from 'app/utils/general';
import NotificationSettingsMobileAndroid from './notification_settings_email.android.js';
import NotificationSettingsEmailAndroid from './notification_settings_email.android.js';
describe('NotificationSettingsMobileAndroid', () => {
describe('NotificationSettingsEmailAndroid', () => {
const baseProps = {
currentUserId: 'current_user_id',
currentUser: {id: 'current_user_id'},
emailInterval: '30',
enableEmailBatching: false,
navigator: {setOnNavigatorEvent: () => {}}, // eslint-disable-line no-empty-function
navigator: {setOnNavigatorEvent: emptyFunction},
actions: {
savePreferences: () => {}, // eslint-disable-line no-empty-function
updateMe: jest.fn(),
savePreferences: jest.fn(),
},
sendEmailNotifications: true,
siteName: 'Mattermost',
@ -29,7 +31,7 @@ describe('NotificationSettingsMobileAndroid', () => {
test('should match snapshot', () => {
const wrapper = shallowWithIntl(
<NotificationSettingsMobileAndroid {...baseProps}/>
<NotificationSettingsEmailAndroid {...baseProps}/>
);
const style = {
@ -52,7 +54,7 @@ describe('NotificationSettingsMobileAndroid', () => {
test('should match state on setEmailNotifications', () => {
const wrapper = shallowWithIntl(
<NotificationSettingsMobileAndroid {...baseProps}/>
<NotificationSettingsEmailAndroid {...baseProps}/>
);
wrapper.setState({email: 'false', interval: '0'});
@ -68,7 +70,7 @@ describe('NotificationSettingsMobileAndroid', () => {
test('should match state on handleClose', () => {
const wrapper = shallowWithIntl(
<NotificationSettingsMobileAndroid {...baseProps}/>
<NotificationSettingsEmailAndroid {...baseProps}/>
);
wrapper.setState({showEmailNotificationsModal: true, interval: '30', newInterval: '3600'});
@ -79,7 +81,7 @@ describe('NotificationSettingsMobileAndroid', () => {
test('should saveEmailNotifyProps and handleClose on handleSaveEmailNotification', () => {
const wrapper = shallowWithIntl(
<NotificationSettingsMobileAndroid {...baseProps}/>
<NotificationSettingsEmailAndroid {...baseProps}/>
);
const instance = wrapper.instance();
@ -89,9 +91,27 @@ describe('NotificationSettingsMobileAndroid', () => {
expect(instance.saveEmailNotifyProps).toHaveBeenCalledTimes(1);
});
test('should call actions.updateMe and actions.savePreferences on saveEmailNotifyProps', () => {
const savePreferences = jest.fn();
const updateMe = jest.fn();
const props = {...baseProps, actions: {savePreferences, updateMe}};
const wrapper = shallowWithIntl(
<NotificationSettingsEmailAndroid {...props}/>
);
wrapper.setState({email: 'true', newInterval: 30});
wrapper.instance().saveEmailNotifyProps();
expect(updateMe).toHaveBeenCalledTimes(1);
expect(updateMe.mock.calls[0][0].notify_props.email).toBe('true');
expect(savePreferences).toHaveBeenCalledTimes(1);
expect(savePreferences).toBeCalledWith('current_user_id', [{category: 'notifications', name: 'email_interval', user_id: 'current_user_id', value: 30}]);
});
test('should match state on showEmailModal', () => {
const wrapper = shallowWithIntl(
<NotificationSettingsMobileAndroid {...baseProps}/>
<NotificationSettingsEmailAndroid {...baseProps}/>
);
wrapper.setState({showEmailNotificationsModal: false});
@ -101,7 +121,7 @@ describe('NotificationSettingsMobileAndroid', () => {
test('should match state on handleChange', () => {
const wrapper = shallowWithIntl(
<NotificationSettingsMobileAndroid {...baseProps}/>
<NotificationSettingsEmailAndroid {...baseProps}/>
);
wrapper.setState({newInterval: '3600'});

View file

@ -6,6 +6,8 @@ import {configure, shallow} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({adapter: new Adapter()});
import {emptyFunction} from 'app/utils/general';
import NotificationSettingsEmailIos from './notification_settings_email.ios.js';
jest.mock('app/utils/theme', () => {
@ -18,12 +20,13 @@ jest.mock('app/utils/theme', () => {
describe('NotificationSettingsEmailIos', () => {
const baseProps = {
currentUserId: 'current_user_id',
currentUser: {id: 'current_user_id'},
emailInterval: '30',
enableEmailBatching: false,
navigator: {setOnNavigatorEvent: () => {}}, // eslint-disable-line no-empty-function
navigator: {setOnNavigatorEvent: emptyFunction},
actions: {
savePreferences: () => {}, // eslint-disable-line no-empty-function
updateMe: jest.fn(),
savePreferences: jest.fn(),
},
sendEmailNotifications: true,
siteName: 'Mattermost',
@ -53,6 +56,24 @@ describe('NotificationSettingsEmailIos', () => {
expect(instance.saveEmailNotifyProps).toHaveBeenCalledTimes(1);
});
test('should call actions.updateMe and actions.savePreferences on saveEmailNotifyProps', () => {
const savePreferences = jest.fn();
const updateMe = jest.fn();
const props = {...baseProps, actions: {savePreferences, updateMe}};
const wrapper = shallow(
<NotificationSettingsEmailIos {...props}/>
);
wrapper.setState({email: 'true', newInterval: 30});
wrapper.instance().saveEmailNotifyProps();
expect(updateMe).toHaveBeenCalledTimes(1);
expect(updateMe.mock.calls[0][0].notify_props.email).toBe('true');
expect(savePreferences).toHaveBeenCalledTimes(1);
expect(savePreferences).toBeCalledWith('current_user_id', [{category: 'notifications', name: 'email_interval', user_id: 'current_user_id', value: 30}]);
});
test('should macth state on setEmailNotifications', () => {
const wrapper = shallow(
<NotificationSettingsEmailIos {...baseProps}/>
@ -70,7 +91,7 @@ describe('NotificationSettingsEmailIos', () => {
});
test('should call props.actions.savePreferences on saveUserNotifyProps', () => {
const props = {...baseProps, actions: {savePreferences: jest.fn()}};
const props = {...baseProps, actions: {savePreferences: jest.fn(), updateMe: jest.fn()}};
const wrapper = shallow(
<NotificationSettingsEmailIos {...props}/>
);
@ -80,10 +101,7 @@ describe('NotificationSettingsEmailIos', () => {
expect(props.actions.savePreferences).toHaveBeenCalledTimes(1);
expect(props.actions.savePreferences).toBeCalledWith(
'current_user_id',
[
{category: 'notifications', name: 'email', user_id: 'current_user_id', value: 'true'},
{category: 'notifications', name: 'email_interval', user_id: 'current_user_id', value: '3600'},
]
[{category: 'notifications', name: 'email_interval', user_id: 'current_user_id', value: '3600'}],
);
});
});

View file

@ -7,14 +7,16 @@ import PropTypes from 'prop-types';
import {Preferences} from 'mattermost-redux/constants';
import {getEmailInterval} from 'mattermost-redux/utils/notify_props';
import {getNotificationProps} from 'app/utils/notify_props';
import {setNavigatorStyles} from 'app/utils/theme';
export default class NotificationSettingsEmailBase extends PureComponent {
static propTypes = {
actions: PropTypes.shape({
savePreferences: PropTypes.func.isRequired,
updateMe: PropTypes.func.isRequired,
}),
currentUserId: PropTypes.string.isRequired,
currentUser: PropTypes.object.isRequired,
emailInterval: PropTypes.string.isRequired,
enableEmailBatching: PropTypes.bool.isRequired,
navigator: PropTypes.object,
@ -94,11 +96,14 @@ export default class NotificationSettingsEmailBase extends PureComponent {
};
saveEmailNotifyProps = () => {
const {currentUserId} = this.props;
const {actions, currentUser} = this.props;
const {email, newInterval} = this.state;
const emailNotify = {category: Preferences.CATEGORY_NOTIFICATIONS, user_id: currentUserId, name: 'email', value: email};
const emailInterval = {category: Preferences.CATEGORY_NOTIFICATIONS, user_id: currentUserId, name: Preferences.EMAIL_INTERVAL, value: newInterval};
this.props.actions.savePreferences(currentUserId, [emailNotify, emailInterval]);
const notifyProps = getNotificationProps(currentUser);
actions.updateMe({notify_props: {...notifyProps, email}});
const emailInterval = {category: Preferences.CATEGORY_NOTIFICATIONS, user_id: currentUser.id, name: Preferences.EMAIL_INTERVAL, value: newInterval};
actions.savePreferences(currentUser.id, [emailInterval]);
};
computeEmailInterval = (sendEmailNotifications, enableEmailBatching, emailInterval) => {