From 650aa6aae284713dfd302cd6c3edfb26d5316f19 Mon Sep 17 00:00:00 2001 From: Renato Peterman Date: Sat, 15 Jun 2019 15:29:21 -0300 Subject: [PATCH] [MM-14917] RN Android: Push notification settings are only saved when closing settings page (#2735) * Add required action and props * Allow callback param to setModilePush and setMobilePushStats * Added saveNotificationProps method to save data on alert save action * Fix import order * Fix import path * Fix import order * Added defaultProps currentUser to NotificationSettingsMobileAndroid * Added missing trailing comma --- .../notification_settings_mobile/index.js | 26 ++++++- .../notification_settings_mobile.android.js | 75 ++++++++++++++++++- .../notification_settings_mobile_base.js | 11 ++- 3 files changed, 102 insertions(+), 10 deletions(-) diff --git a/app/screens/settings/notification_settings_mobile/index.js b/app/screens/settings/notification_settings_mobile/index.js index a096b27fc..f7087364e 100644 --- a/app/screens/settings/notification_settings_mobile/index.js +++ b/app/screens/settings/notification_settings_mobile/index.js @@ -1,19 +1,37 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. +import {bindActionCreators} from 'redux'; import {connect} from 'react-redux'; import {getConfig} from 'mattermost-redux/selectors/entities/general'; - import {getTheme} from 'mattermost-redux/selectors/entities/preferences'; +import {getCurrentUser} from 'mattermost-redux/selectors/entities/users'; + +import {updateMe} from 'mattermost-redux/actions/users'; import NotificationSettingsMobile from './notification_settings_mobile'; function mapStateToProps(state) { + const config = getConfig(state); + const theme = getTheme(state); + const updateMeRequest = state.requests.users.updateMe; + const currentUser = getCurrentUser(state); + return { - config: getConfig(state), - theme: getTheme(state), + config, + theme, + updateMeRequest, + currentUser, }; } -export default connect(mapStateToProps)(NotificationSettingsMobile); +function mapDispatchToProps(dispatch) { + return { + actions: bindActionCreators({ + updateMe, + }, dispatch), + }; +} + +export default connect(mapStateToProps, mapDispatchToProps)(NotificationSettingsMobile); diff --git a/app/screens/settings/notification_settings_mobile/notification_settings_mobile.android.js b/app/screens/settings/notification_settings_mobile/notification_settings_mobile.android.js index 4db6fc045..bda1c76b0 100644 --- a/app/screens/settings/notification_settings_mobile/notification_settings_mobile.android.js +++ b/app/screens/settings/notification_settings_mobile/notification_settings_mobile.android.js @@ -4,6 +4,7 @@ import React from 'react'; import {injectIntl} from 'react-intl'; import { + Alert, Modal, Platform, ScrollView, @@ -12,6 +13,11 @@ import { View, } from 'react-native'; +import deepEqual from 'deep-equal'; +import PropTypes from 'prop-types'; + +import {RequestStatus} from 'mattermost-redux/constants'; + import FormattedText from 'app/components/formatted_text'; import RadioButtonGroup from 'app/components/radio_button'; import NotificationPreferences from 'app/notification_preferences'; @@ -19,10 +25,20 @@ import PushNotifications from 'app/push_notifications'; import StatusBar from 'app/components/status_bar'; import SectionItem from 'app/screens/settings/section_item'; import {changeOpacity, makeStyleSheetFromTheme} from 'app/utils/theme'; +import {getNotificationProps} from 'app/utils/notify_props'; import NotificationSettingsMobileBase from './notification_settings_mobile_base'; class NotificationSettingsMobileAndroid extends NotificationSettingsMobileBase { + static propTypes = { + ...NotificationSettingsMobileBase.propTypes, + updateMeRequest: PropTypes.object.isRequired, + } + + static defaultProps = { + currentUser: {}, + } + cancelMobilePushModal = () => { this.setState({ newPush: this.state.push, @@ -354,6 +370,24 @@ class NotificationSettingsMobileAndroid extends NotificationSettingsMobileBase { ); } + componentWillReceiveProps(nextProps) { + super.componentWillReceiveProps(nextProps); + + const {updateMeRequest, intl} = nextProps; + if (this.props.updateMeRequest !== updateMeRequest && updateMeRequest.status === RequestStatus.FAILURE) { + Alert.alert( + intl.formatMessage({ + id: 'mobile.notification_settings.save_failed_title', + defaultMessage: 'Connection issue', + }), + intl.formatMessage({ + id: 'mobile.notification_settings.save_failed_description', + defaultMessage: 'The notification settings failed to save due to a connection issue, please try again.', + }) + ); + } + } + renderMobilePushSection() { const {config, theme} = this.props; @@ -583,12 +617,49 @@ class NotificationSettingsMobileAndroid extends NotificationSettingsMobileBase { saveMobilePushModal = () => { this.setState({showMobilePushModal: false}); - this.setMobilePush(this.state.newPush); + this.setMobilePush(this.state.newPush, this.saveNotificationProps); }; saveMobilePushStatusModal = () => { this.setState({showMobilePushStatusModal: false}); - this.setMobilePushStatus(this.state.newPushStatus); + this.setMobilePushStatus(this.state.newPushStatus, this.saveNotificationProps); + }; + + saveNotificationProps = () => { + const { + channel, + comments, + desktop, + email, + first_name: firstName, + mention_keys: mentionKeys, + push, + push_status: pushStatus, + } = this.state; + + const {currentUser} = this.props; + + const notifyProps = { + channel, + comments, + desktop, + email, + first_name: firstName, + mention_keys: mentionKeys, + push, + push_status: pushStatus, + user_id: currentUser.id, + }; + + const {user_id: userId} = notifyProps; + const previousProps = { + ...getNotificationProps(currentUser), + user_id: userId, + }; + + if (!deepEqual(previousProps, notifyProps)) { + this.props.actions.updateMe({notify_props: notifyProps}); + } }; saveMobileSoundsModal = () => { diff --git a/app/screens/settings/notification_settings_mobile/notification_settings_mobile_base.js b/app/screens/settings/notification_settings_mobile/notification_settings_mobile_base.js index ad38701ee..80a6bddf7 100644 --- a/app/screens/settings/notification_settings_mobile/notification_settings_mobile_base.js +++ b/app/screens/settings/notification_settings_mobile/notification_settings_mobile_base.js @@ -11,6 +11,9 @@ import {setNavigatorStyles} from 'app/utils/theme'; export default class NotificationSettingsMobileBase extends PureComponent { static propTypes = { + actions: PropTypes.shape({ + updateMe: PropTypes.func.isRequired, + }), config: PropTypes.object.isRequired, currentUser: PropTypes.object.isRequired, intl: intlShape.isRequired, @@ -89,12 +92,12 @@ export default class NotificationSettingsMobileBase extends PureComponent { } }; - setMobilePush = (push) => { - this.setState({push}); + setMobilePush = (push, callback) => { + this.setState({push}, callback); }; - setMobilePushStatus = (value) => { - this.setState({push_status: value}); + setMobilePushStatus = (value, callback) => { + this.setState({push_status: value}, callback); }; saveUserNotifyProps = () => {