do not save email interval preference on cancel on Android device (#2633)

This commit is contained in:
Saturnino Abril 2019-03-09 13:27:07 +08:00
parent d22422840b
commit c7dc992ac0
5 changed files with 62 additions and 26 deletions

View file

@ -28,7 +28,7 @@ class NotificationSettingsEmailAndroid extends NotificationSettingsEmailBase {
handleClose = () => {
this.setState({
newInterval: this.state.interval,
newInterval: this.state.emailInterval,
showEmailNotificationsModal: false,
});
}
@ -47,11 +47,11 @@ class NotificationSettingsEmailAndroid extends NotificationSettingsEmailBase {
sendEmailNotifications,
theme,
} = this.props;
const {interval} = this.state;
const {newInterval} = this.state;
let i18nId;
let i18nMessage;
if (sendEmailNotifications) {
switch (interval) {
switch (newInterval) {
case Preferences.INTERVAL_IMMEDIATE.toString():
i18nId = t('user.settings.notifications.email.immediately');
i18nMessage = 'Immediately';

View file

@ -12,6 +12,12 @@ import RadioButtonGroup from 'app/components/radio_button';
import NotificationSettingsEmailAndroid from './notification_settings_email.android.js';
jest.mock('Platform', () => {
const Platform = require.requireActual('Platform');
Platform.OS = 'android';
return Platform;
});
describe('NotificationSettingsEmailAndroid', () => {
const baseProps = {
currentUser: {id: 'current_user_id'},
@ -139,4 +145,22 @@ describe('NotificationSettingsEmailAndroid', () => {
wrapper.instance().showEmailModal();
expect(wrapper.state('showEmailNotificationsModal')).toEqual(true);
});
test('should not save preference on back button on Android', () => {
const wrapper = shallowWithIntl(
<NotificationSettingsEmailAndroid {...baseProps}/>
);
const instance = wrapper.instance();
instance.saveEmailNotifyProps = jest.fn();
// should not save preference on back button on Android
// saving email preference on Android is done via Save button
instance.onNavigatorEvent({type: 'ScreenChangedEvent', id: 'willDisappear'});
expect(instance.saveEmailNotifyProps).toHaveBeenCalledTimes(0);
wrapper.setState({newInterval: '0'});
instance.onNavigatorEvent({type: 'ScreenChangedEvent', id: 'willDisappear'});
expect(instance.saveEmailNotifyProps).toHaveBeenCalledTimes(0);
});
});

View file

@ -28,7 +28,7 @@ class NotificationSettingsEmailIos extends NotificationSettingsEmailBase {
siteName,
theme,
} = this.props;
const {interval} = this.state;
const {newInterval} = this.state;
const style = getStyleSheet(theme);
return (
@ -53,7 +53,7 @@ class NotificationSettingsEmailIos extends NotificationSettingsEmailBase {
action={this.setEmailNotifications}
actionType='select'
actionValue={Preferences.INTERVAL_IMMEDIATE.toString()}
selected={interval === Preferences.INTERVAL_IMMEDIATE.toString()}
selected={newInterval === Preferences.INTERVAL_IMMEDIATE.toString()}
theme={theme}
/>
<View style={style.separator}/>
@ -69,7 +69,7 @@ class NotificationSettingsEmailIos extends NotificationSettingsEmailBase {
action={this.setEmailNotifications}
actionType='select'
actionValue={Preferences.INTERVAL_FIFTEEN_MINUTES.toString()}
selected={interval === Preferences.INTERVAL_FIFTEEN_MINUTES.toString()}
selected={newInterval === Preferences.INTERVAL_FIFTEEN_MINUTES.toString()}
theme={theme}
/>
<View style={style.separator}/>
@ -83,7 +83,7 @@ class NotificationSettingsEmailIos extends NotificationSettingsEmailBase {
action={this.setEmailNotifications}
actionType='select'
actionValue={Preferences.INTERVAL_HOUR.toString()}
selected={interval === Preferences.INTERVAL_HOUR.toString()}
selected={newInterval === Preferences.INTERVAL_HOUR.toString()}
theme={theme}
/>
<View style={style.separator}/>
@ -99,7 +99,7 @@ class NotificationSettingsEmailIos extends NotificationSettingsEmailBase {
action={this.setEmailNotifications}
actionType='select'
actionValue={Preferences.INTERVAL_NEVER.toString()}
selected={interval === Preferences.INTERVAL_NEVER.toString()}
selected={newInterval === Preferences.INTERVAL_NEVER.toString()}
theme={theme}
/>
</View>

View file

@ -12,6 +12,12 @@ import SectionItem from 'app/screens/settings/section_item';
import NotificationSettingsEmailIos from './notification_settings_email.ios.js';
jest.mock('Platform', () => {
const Platform = require.requireActual('Platform');
Platform.OS = 'ios';
return Platform;
});
jest.mock('app/utils/theme', () => {
const original = require.requireActual('app/utils/theme');
return {
@ -43,16 +49,23 @@ describe('NotificationSettingsEmailIos', () => {
expect(wrapper.instance().renderEmailSection()).toMatchSnapshot();
});
test('should call saveEmailNotifyProps on onNavigatorEvent', () => {
test('should save preference on back button only if email interval has changed', () => {
const wrapper = shallow(
<NotificationSettingsEmailIos {...baseProps}/>
);
const instance = wrapper.instance();
instance.saveEmailNotifyProps = jest.fn();
instance.onNavigatorEvent({type: 'ScreenChangedEvent', id: 'willDisappear'});
expect(instance.saveEmailNotifyProps).toHaveBeenCalledTimes(1);
// should not save preference if email interval has not changed.
instance.onNavigatorEvent({type: 'ScreenChangedEvent', id: 'willDisappear'});
expect(baseProps.actions.updateMe).toHaveBeenCalledTimes(0);
expect(baseProps.actions.savePreferences).toHaveBeenCalledTimes(0);
// should save preference if email interval has changed.
wrapper.setState({newInterval: '0'});
instance.onNavigatorEvent({type: 'ScreenChangedEvent', id: 'willDisappear'});
expect(baseProps.actions.updateMe).toHaveBeenCalledTimes(1);
expect(baseProps.actions.savePreferences).toHaveBeenCalledTimes(1);
});
test('should call actions.updateMe and actions.savePreferences on saveEmailNotifyProps', () => {

View file

@ -2,6 +2,7 @@
// See LICENSE.txt for license information.
import {PureComponent} from 'react';
import {Platform} from 'react-native';
import PropTypes from 'prop-types';
import {Preferences} from 'mattermost-redux/constants';
@ -35,11 +36,9 @@ export default class NotificationSettingsEmailBase extends PureComponent {
sendEmailNotifications,
} = props;
const interval = this.computeEmailInterval(sendEmailNotifications, enableEmailBatching, emailInterval);
this.state = {
interval,
newInterval: interval,
emailInterval,
newInterval: this.computeEmailInterval(sendEmailNotifications, enableEmailBatching, emailInterval),
showEmailNotificationsModal: false,
};
@ -62,16 +61,15 @@ export default class NotificationSettingsEmailBase extends PureComponent {
this.props.enableEmailBatching !== enableEmailBatching ||
this.props.emailInterval !== emailInterval
) {
const interval = this.computeEmailInterval(sendEmailNotifications, enableEmailBatching, emailInterval);
this.setState({
interval,
newInterval: interval,
emailInterval,
newInterval: this.computeEmailInterval(sendEmailNotifications, enableEmailBatching, emailInterval),
});
}
}
onNavigatorEvent = (event) => {
if (event.type === 'ScreenChangedEvent') {
if (Platform.OS === 'ios' && event.type === 'ScreenChangedEvent') {
switch (event.id) {
case 'willDisappear':
this.saveEmailNotifyProps();
@ -90,20 +88,21 @@ export default class NotificationSettingsEmailBase extends PureComponent {
this.setState({
email,
interval: value,
newInterval: value,
});
};
saveEmailNotifyProps = () => {
const {actions, currentUser} = this.props;
const {email, newInterval} = this.state;
const {email, emailInterval, newInterval} = this.state;
const notifyProps = getNotificationProps(currentUser);
actions.updateMe({notify_props: {...notifyProps, email}});
if (emailInterval !== newInterval) {
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]);
const emailIntervalPreference = {category: Preferences.CATEGORY_NOTIFICATIONS, user_id: currentUser.id, name: Preferences.EMAIL_INTERVAL, value: newInterval};
actions.savePreferences(currentUser.id, [emailIntervalPreference]);
}
};
computeEmailInterval = (sendEmailNotifications, enableEmailBatching, emailInterval) => {