From 1d79746eff5806a1998818288297a4b590f73edf Mon Sep 17 00:00:00 2001 From: Djorkaeff Alexandre Date: Tue, 11 Feb 2020 17:28:40 -0300 Subject: [PATCH] [IMPROVEMENT] Reset app when language is changed (#1702) --- app/views/LanguageView/index.js | 56 ++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/app/views/LanguageView/index.js b/app/views/LanguageView/index.js index ca2efb866..76190a05a 100644 --- a/app/views/LanguageView/index.js +++ b/app/views/LanguageView/index.js @@ -2,7 +2,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import { FlatList } from 'react-native'; import { connect } from 'react-redux'; -import { SafeAreaView, NavigationActions } from 'react-navigation'; +import { SafeAreaView } from 'react-navigation'; import RocketChat from '../../lib/rocketchat'; import I18n from '../../i18n'; @@ -18,7 +18,9 @@ import Separator from '../../containers/Separator'; import { themes } from '../../constants/colors'; import { withTheme } from '../../theme'; import { themedHeader } from '../../utils/navigation'; +import { appStart as appStartAction } from '../../actions'; import { getUserSelector } from '../../selectors/login'; +import database from '../../lib/database'; const LANGUAGES = [ { @@ -58,23 +60,23 @@ class LanguageView extends React.Component { }) static propTypes = { - userLanguage: PropTypes.string, - navigation: PropTypes.object, + user: PropTypes.object, setUser: PropTypes.func, + appStart: PropTypes.func, theme: PropTypes.string } constructor(props) { super(props); this.state = { - language: props.userLanguage ? props.userLanguage : 'en', + language: props.user ? props.user.language : 'en', saving: false }; } shouldComponentUpdate(nextProps, nextState) { const { language, saving } = this.state; - const { userLanguage, theme } = this.props; + const { user, theme } = this.props; if (nextProps.theme !== theme) { return true; } @@ -84,15 +86,15 @@ class LanguageView extends React.Component { if (nextState.saving !== saving) { return true; } - if (nextProps.userLanguage !== userLanguage) { + if (nextProps.user.language !== user.language) { return true; } return false; } formIsChanged = (language) => { - const { userLanguage } = this.props; - return (userLanguage !== language); + const { user } = this.props; + return (user.language !== language); } submit = async(language) => { @@ -102,12 +104,12 @@ class LanguageView extends React.Component { this.setState({ saving: true }); - const { userLanguage, setUser, navigation } = this.props; + const { user, setUser, appStart } = this.props; const params = {}; // language - if (userLanguage !== language) { + if (user.language !== language) { params.language = language; } @@ -115,18 +117,27 @@ class LanguageView extends React.Component { await RocketChat.saveUserPreferences(params); setUser({ language: params.language }); - this.setState({ saving: false }); - setTimeout(() => { - navigation.reset([NavigationActions.navigate({ routeName: 'SettingsView' })], 0); - navigation.navigate('RoomsListView'); - }, 300); + const serversDB = database.servers; + const usersCollection = serversDB.collections.get('users'); + await serversDB.action(async() => { + try { + const userRecord = await usersCollection.find(user.id); + await userRecord.update((record) => { + record.language = params.language; + }); + } catch (e) { + // do nothing + } + }); + + await appStart('loading'); + await appStart('inside'); } catch (e) { - this.setState({ saving: false }); - setTimeout(() => { - showErrorAlert(I18n.t('There_was_an_error_while_action', { action: I18n.t('saving_preferences') })); - log(e); - }, 300); + showErrorAlert(I18n.t('There_was_an_error_while_action', { action: I18n.t('saving_preferences') })); + log(e); } + + this.setState({ saving: false }); } renderSeparator = () => { @@ -186,11 +197,12 @@ class LanguageView extends React.Component { } const mapStateToProps = state => ({ - userLanguage: getUserSelector(state).language + user: getUserSelector(state) }); const mapDispatchToProps = dispatch => ({ - setUser: params => dispatch(setUserAction(params)) + setUser: params => dispatch(setUserAction(params)), + appStart: params => dispatch(appStartAction(params)) }); export default connect(mapStateToProps, mapDispatchToProps)(withTheme(LanguageView));