fix email preference to not change value on cancel (#1912)

This commit is contained in:
Saturnino Abril 2018-07-12 05:06:36 +08:00 committed by Elias Nahum
parent 123e0e699f
commit 199a49b221
4 changed files with 58 additions and 30 deletions

View file

@ -26,18 +26,25 @@ class NotificationSettingsEmailAndroid extends NotificationSettingsEmailBase {
};
handleClose = () => {
this.setState({showEmailNotificationsModal: false});
this.setState({
newInterval: this.state.interval,
showEmailNotificationsModal: false,
});
}
handleSaveEmailNotification = () => {
this.setState({showEmailNotificationsModal: false});
this.saveEmailNotifyProps();
this.handleClose();
};
showEmailModal = () => {
this.setState({showEmailNotificationsModal: true});
};
handleChange = (value) => {
this.setState({newInterval: value});
}
renderEmailSection() {
const {
sendEmailNotifications,
@ -99,7 +106,7 @@ class NotificationSettingsEmailAndroid extends NotificationSettingsEmailBase {
sendEmailNotifications,
siteName,
} = this.props;
const {interval} = this.state;
const {newInterval} = this.state;
let helpText;
if (sendEmailNotifications) {
@ -119,7 +126,7 @@ class NotificationSettingsEmailAndroid extends NotificationSettingsEmailBase {
defaultMessage: 'Immediately',
}),
value: Preferences.INTERVAL_IMMEDIATE.toString(),
checked: interval === Preferences.INTERVAL_IMMEDIATE.toString(),
checked: newInterval === Preferences.INTERVAL_IMMEDIATE.toString(),
}];
if (enableEmailBatching) {
@ -129,14 +136,14 @@ class NotificationSettingsEmailAndroid extends NotificationSettingsEmailBase {
defaultMessage: 'Every 15 minutes',
}),
value: Preferences.INTERVAL_FIFTEEN_MINUTES.toString(),
checked: interval === Preferences.INTERVAL_FIFTEEN_MINUTES.toString(),
checked: newInterval === Preferences.INTERVAL_FIFTEEN_MINUTES.toString(),
}, {
label: intl.formatMessage({
id: 'user.settings.notifications.email.everyHour',
defaultMessage: 'Every hour',
}),
value: Preferences.INTERVAL_HOUR.toString(),
checked: interval === Preferences.INTERVAL_HOUR.toString(),
checked: newInterval === Preferences.INTERVAL_HOUR.toString(),
});
}
@ -146,7 +153,7 @@ class NotificationSettingsEmailAndroid extends NotificationSettingsEmailBase {
defaultMessage: 'Never',
}),
value: Preferences.INTERVAL_NEVER.toString(),
checked: interval === Preferences.INTERVAL_NEVER.toString(),
checked: newInterval === Preferences.INTERVAL_NEVER.toString(),
});
return (
@ -176,7 +183,7 @@ class NotificationSettingsEmailAndroid extends NotificationSettingsEmailBase {
{sendEmailNotifications &&
<RadioButtonGroup
name='emailSettings'
onSelect={this.setEmailNotifications}
onSelect={this.handleChange}
options={emailOptions}
/>
}

View file

@ -71,9 +71,10 @@ describe('NotificationSettingsMobileAndroid', () => {
<NotificationSettingsMobileAndroid {...baseProps}/>
);
wrapper.setState({showEmailNotificationsModal: true});
wrapper.setState({showEmailNotificationsModal: true, interval: '30', newInterval: '3600'});
wrapper.instance().handleClose();
expect(wrapper.state('showEmailNotificationsModal')).toEqual(false);
expect(wrapper.state('newInterval')).toEqual('30');
});
test('should saveEmailNotifyProps and handleClose on handleSaveEmailNotification', () => {
@ -83,11 +84,9 @@ describe('NotificationSettingsMobileAndroid', () => {
const instance = wrapper.instance();
instance.saveEmailNotifyProps = jest.fn();
instance.handleClose = jest.fn();
instance.handleSaveEmailNotification();
expect(instance.saveEmailNotifyProps).toHaveBeenCalledTimes(1);
expect(instance.handleClose).toHaveBeenCalledTimes(1);
});
test('should match state on showEmailModal', () => {
@ -99,4 +98,14 @@ describe('NotificationSettingsMobileAndroid', () => {
wrapper.instance().showEmailModal();
expect(wrapper.state('showEmailNotificationsModal')).toEqual(true);
});
test('should match state on handleChange', () => {
const wrapper = shallowWithIntl(
<NotificationSettingsMobileAndroid {...baseProps}/>
);
wrapper.setState({newInterval: '3600'});
wrapper.instance().handleChange('30');
expect(wrapper.state('newInterval')).toEqual('30');
});
});

View file

@ -75,7 +75,7 @@ describe('NotificationSettingsEmailIos', () => {
<NotificationSettingsEmailIos {...props}/>
);
wrapper.setState({email: 'true', interval: '3600'});
wrapper.setState({email: 'true', newInterval: '3600'});
wrapper.instance().saveEmailNotifyProps();
expect(props.actions.savePreferences).toHaveBeenCalledTimes(1);
expect(props.actions.savePreferences).toBeCalledWith(

View file

@ -33,12 +33,11 @@ export default class NotificationSettingsEmailBase extends PureComponent {
sendEmailNotifications,
} = props;
const interval = this.computeEmailInterval(sendEmailNotifications, enableEmailBatching, emailInterval);
this.state = {
interval: getEmailInterval(
sendEmailNotifications,
enableEmailBatching,
parseInt(emailInterval, 10),
).toString(),
interval,
newInterval: interval,
showEmailNotificationsModal: false,
};
@ -50,17 +49,21 @@ export default class NotificationSettingsEmailBase extends PureComponent {
setNavigatorStyles(this.props.navigator, nextProps.theme);
}
const {
sendEmailNotifications,
enableEmailBatching,
emailInterval,
} = nextProps;
if (
this.props.sendEmailNotifications !== nextProps.sendEmailNotifications ||
this.props.enableEmailBatching !== nextProps.enableEmailBatching ||
this.props.emailInterval !== nextProps.emailInterval
this.props.sendEmailNotifications !== sendEmailNotifications ||
this.props.enableEmailBatching !== enableEmailBatching ||
this.props.emailInterval !== emailInterval
) {
const interval = this.computeEmailInterval(sendEmailNotifications, enableEmailBatching, emailInterval);
this.setState({
interval: getEmailInterval(
nextProps.sendEmailNotifications,
nextProps.enableEmailBatching,
parseInt(nextProps.emailInterval, 10),
).toString(),
interval,
newInterval: interval,
});
}
}
@ -75,25 +78,34 @@ export default class NotificationSettingsEmailBase extends PureComponent {
}
};
setEmailNotifications = (interval) => {
setEmailNotifications = (value) => {
const {sendEmailNotifications} = this.props;
let email = 'false';
if (sendEmailNotifications && interval !== Preferences.INTERVAL_NEVER.toString()) {
if (sendEmailNotifications && value !== Preferences.INTERVAL_NEVER.toString()) {
email = 'true';
}
this.setState({
email,
interval,
interval: value,
newInterval: value,
});
};
saveEmailNotifyProps = () => {
const {currentUserId} = this.props;
const {email, interval} = this.state;
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: interval};
const emailInterval = {category: Preferences.CATEGORY_NOTIFICATIONS, user_id: currentUserId, name: Preferences.EMAIL_INTERVAL, value: newInterval};
this.props.actions.savePreferences(currentUserId, [emailNotify, emailInterval]);
};
computeEmailInterval = (sendEmailNotifications, enableEmailBatching, emailInterval) => {
return getEmailInterval(
sendEmailNotifications,
enableEmailBatching,
parseInt(emailInterval, 10),
).toString();
}
}