[MM-17746] Map currentUser from state to props in NotificationSettingsEmail (#3357)

* Map currentUser from state to props

* Fix ref warning

* Use optional chaining operator

* Use componentDidUpdate

* Don't forget to call super
This commit is contained in:
Miguel Alatzar 2019-10-27 00:39:15 -07:00 committed by Elias Nahum
parent 349dbf9194
commit ec0be38217
7 changed files with 27 additions and 19 deletions

View file

@ -70,7 +70,6 @@ export default class RadioButtonGroup extends PureComponent {
return (
<RadioButton
{...other}
ref={value}
name={name}
key={`${name}-${value}`}
value={value}

View file

@ -78,17 +78,14 @@ class NotificationSettings extends PureComponent {
};
goToNotificationSettingsEmail = () => {
const {currentUser, intl} = this.props;
const {intl} = this.props;
const screen = 'NotificationSettingsEmail';
const title = intl.formatMessage({
id: 'mobile.notification_settings.email_title',
defaultMessage: 'Email Notifications',
});
const passProps = {
currentUser,
};
goToScreen(screen, title, passProps);
goToScreen(screen, title);
};
goToNotificationSettingsMentions = () => {

View file

@ -9,15 +9,22 @@ import {Preferences} from 'mattermost-redux/constants';
import {savePreferences} from 'mattermost-redux/actions/preferences';
import {updateMe} from 'mattermost-redux/actions/users';
import {getCurrentUser} from 'mattermost-redux/selectors/entities/users';
import {getConfig} from 'mattermost-redux/selectors/entities/general';
import {
get as getPreference,
getTheme,
} from 'mattermost-redux/selectors/entities/preferences';
import {isLandscape} from 'app/selectors/device';
import {getNotificationProps} from 'app/utils/notify_props';
import NotificationSettingsEmail from './notification_settings_email';
function mapStateToProps(state) {
const currentUser = getCurrentUser(state) || {};
const notifyProps = getNotificationProps(currentUser);
const config = getConfig(state);
const sendEmailNotifications = config.SendEmailNotifications === 'true';
const enableEmailBatching = config.EnableEmailBatching === 'true';
@ -29,6 +36,8 @@ function mapStateToProps(state) {
);
return {
currentUser,
notifyProps,
enableEmailBatching,
emailInterval,
sendEmailNotifications,

View file

@ -20,6 +20,9 @@ jest.mock('Platform', () => {
describe('NotificationSettingsEmailAndroid', () => {
const baseProps = {
currentUser: {id: 'current_user_id'},
notifyProps: {
email: 'true',
},
emailInterval: '30',
enableEmailBatching: false,
actions: {

View file

@ -27,6 +27,9 @@ jest.mock('app/utils/theme', () => {
describe('NotificationSettingsEmailIos', () => {
const baseProps = {
currentUser: {id: 'current_user_id'},
notifyProps: {
email: 'true',
},
emailInterval: '30',
enableEmailBatching: false,
actions: {

View file

@ -9,7 +9,6 @@ import {Navigation} from 'react-native-navigation';
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 {
@ -20,6 +19,7 @@ export default class NotificationSettingsEmailBase extends PureComponent {
}),
componentId: PropTypes.string,
currentUser: PropTypes.object.isRequired,
notifyProps: PropTypes.object.isRequired,
emailInterval: PropTypes.string.isRequired,
enableEmailBatching: PropTypes.bool.isRequired,
sendEmailNotifications: PropTypes.bool.isRequired,
@ -31,14 +31,12 @@ export default class NotificationSettingsEmailBase extends PureComponent {
super(props);
const {
currentUser,
notifyProps,
emailInterval,
enableEmailBatching,
sendEmailNotifications,
} = props;
const notifyProps = getNotificationProps(currentUser);
this.state = {
emailInterval,
newInterval: this.computeEmailInterval(notifyProps?.email === 'true' && sendEmailNotifications, enableEmailBatching, emailInterval),
@ -56,7 +54,7 @@ export default class NotificationSettingsEmailBase extends PureComponent {
}
const {
currentUser,
notifyProps,
sendEmailNotifications,
enableEmailBatching,
emailInterval,
@ -65,10 +63,9 @@ export default class NotificationSettingsEmailBase extends PureComponent {
if (
this.props.sendEmailNotifications !== sendEmailNotifications ||
this.props.enableEmailBatching !== enableEmailBatching ||
this.props.emailInterval !== emailInterval
this.props.emailInterval !== emailInterval ||
this.props.notifyProps?.email !== notifyProps?.email
) {
const notifyProps = getNotificationProps(currentUser);
this.setState({
emailInterval,
newInterval: this.computeEmailInterval(notifyProps?.email === 'true' && sendEmailNotifications, enableEmailBatching, emailInterval),
@ -93,6 +90,7 @@ export default class NotificationSettingsEmailBase extends PureComponent {
const {
actions,
currentUser,
notifyProps,
sendEmailNotifications,
} = this.props;
@ -101,7 +99,6 @@ export default class NotificationSettingsEmailBase extends PureComponent {
email = 'true';
}
const notifyProps = getNotificationProps(currentUser);
actions.updateMe({notify_props: {...notifyProps, email}});
const emailIntervalPreference = {category: Preferences.CATEGORY_NOTIFICATIONS, user_id: currentUser.id, name: Preferences.EMAIL_INTERVAL, value: newInterval};

View file

@ -370,11 +370,11 @@ class NotificationSettingsMobileAndroid extends NotificationSettingsMobileBase {
);
}
componentWillReceiveProps(nextProps) {
super.componentWillReceiveProps(nextProps);
componentDidUpdate(prevProps) {
super.componentDidUpdate(prevProps);
const {updateMeRequest, intl} = nextProps;
if (this.props.updateMeRequest !== updateMeRequest && updateMeRequest.status === RequestStatus.FAILURE) {
const {updateMeRequest, intl} = this.props;
if (updateMeRequest !== prevProps.updateMeRequest && updateMeRequest.status === RequestStatus.FAILURE) {
Alert.alert(
intl.formatMessage({
id: 'mobile.notification_settings.save_failed_title',